From 8ca16a1aa99737a36f6b3aa477996d94c4681f56 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Wed, 22 Aug 2018 10:38:44 +0300 Subject: [PATCH] Fixed Patch Task causing issues on disable #695 --- .../database/databases/sql/PatchTask.java | 56 +++++++++++++++++++ .../system/database/databases/sql/SQLDB.java | 32 ++--------- 2 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/PatchTask.java diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/PatchTask.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/PatchTask.java new file mode 100644 index 000000000..3a5f79711 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/PatchTask.java @@ -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; + + public PatchTask(Patch[] patches, Supplier 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; + } + +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java index 2e1656591..46b376125 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java @@ -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); }