Fixed Patch Task causing issues on disable #695

This commit is contained in:
Rsl1122 2018-08-22 10:38:44 +03:00
parent 8fb54ed905
commit 8ca16a1aa9
2 changed files with 62 additions and 26 deletions

View File

@ -0,0 +1,56 @@
package com.djrapitops.plan.system.database.databases.sql;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.system.database.databases.sql.patches.Patch;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.locale.lang.PluginLang;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import java.util.function.Supplier;
/**
* Task that is in charge on patching the database when the database enables.
*
* @author Rsl1122
*/
public class PatchTask extends AbsRunnable {
private final Patch[] patches;
private final Supplier<Locale> locale;
public PatchTask(Patch[] patches, Supplier<Locale> locale) {
this.patches = patches;
this.locale = locale;
}
@Override
public void run() {
try {
boolean didApply = applyPatches();
Log.info(locale.get().getString(
didApply ? PluginLang.DB_APPLIED_PATCHES : PluginLang.DB_APPLIED_PATCHES_ALREADY
));
} catch (Exception e) {
Log.error("----------------------------------------------------");
Log.error(locale.get().getString(PluginLang.ENABLE_FAIL_DB_PATCH));
Log.error("----------------------------------------------------");
Log.toLog(this.getClass(), e);
PlanPlugin.getInstance().onDisable();
}
}
private boolean applyPatches() {
boolean didApply = false;
for (Patch patch : patches) {
if (!patch.hasBeenApplied()) {
String patchName = patch.getClass().getSimpleName();
Log.info(locale.get().getString(PluginLang.DB_APPLY_PATCH, patchName));
patch.apply();
didApply = true;
}
}
return didApply;
}
}

View File

@ -1,6 +1,5 @@
package com.djrapitops.plan.system.database.databases.sql;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.system.database.databases.Database;
@ -157,31 +156,12 @@ public abstract class SQLDB extends Database {
new VersionTableRemovalPatch(this)
};
RunnableFactory.createNew("Database Patch", new AbsRunnable() {
@Override
public void run() {
try {
boolean applied = false;
for (Patch patch : patches) {
if (!patch.hasBeenApplied()) {
String patchName = patch.getClass().getSimpleName();
Log.info(locale.get().getString(PluginLang.DB_APPLY_PATCH, patchName));
patch.apply();
applied = true;
}
}
Log.info(locale.get().getString(
applied ? PluginLang.DB_APPLIED_PATCHES : PluginLang.DB_APPLIED_PATCHES_ALREADY
));
} catch (Exception e) {
Log.error("----------------------------------------------------");
Log.error(locale.get().getString(PluginLang.ENABLE_FAIL_DB_PATCH));
Log.error("----------------------------------------------------");
Log.toLog(this.getClass(), e);
PlanPlugin.getInstance().onDisable();
}
}
}).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 5L);
try {
RunnableFactory.createNew("Database Patch", new PatchTask(patches, locale))
.runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 5L);
} catch (Exception ignore) {
// Task failed to register because plugin is being disabled
}
} catch (DBOpException e) {
throw new DBInitException("Failed to set-up Database", e);
}