From 7dfc5fa3f5d1c21f2352c29f28f32628dbe31188 Mon Sep 17 00:00:00 2001 From: gortiz-dotcms Date: Tue, 7 Jan 2025 16:44:51 -0300 Subject: [PATCH 1/3] fix(jobs): add an upgrade task to delete the job records from the database (#30933) --- .../Task250107RemoveEsReadOnlyMonitorJob.java | 66 +++++++++++++++++++ .../dotmarketing/util/TaskLocatorUtil.java | 4 +- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java diff --git a/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java new file mode 100644 index 000000000000..7a8c748c014d --- /dev/null +++ b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java @@ -0,0 +1,66 @@ +package com.dotmarketing.startup.runonce; + +import com.dotmarketing.common.db.DotConnect; +import com.dotmarketing.exception.DotDataException; +import com.dotmarketing.exception.DotRuntimeException; +import com.dotmarketing.startup.StartupTask; + +import java.util.ArrayList; + +public class Task250107RemoveEsReadOnlyMonitorJob implements StartupTask { + + + private final String JOB_NAME = "EsReadOnlyMonitorJob"; + private final String TRIGGER_NAME = "trigger29"; + private final String CRON_TABLE_NAME = "qrtz_excl_cron_triggers"; + private final String TRIGGER_TABLE_NAME = "qrtz_excl_triggers"; + private final String JOB_TABLE_NAME = "qrtz_excl_job_details"; + + /** + * Determines if the task should be forced to run. + * + * @return true if EsReadOnlyMonitorJob is found, false otherwise. + */ + @Override + public boolean forceRun() { + String sql=String.format("SELECT * FROM %s WHERE job_name=?", JOB_TABLE_NAME); + DotConnect dc=new DotConnect(); + dc.setSQL(sql); + + dc.addParam(JOB_NAME); + try { + ArrayList results=dc.loadResults(); + if (!results.isEmpty()){ + return true; + } + } catch (DotDataException e) { + e.printStackTrace(); + } + return false; + } + + @Override + public void executeUpgrade() throws DotDataException, DotRuntimeException { + removeCron(); + removeTrigger(); + removeJob(); + } + + private void removeJob() throws DotDataException { + DotConnect dc = new DotConnect(); + dc.setSQL(String.format("DELETE FROM %s WHERE job_name = %s", JOB_TABLE_NAME, JOB_NAME)); + dc.loadResult(); + } + + private void removeTrigger() throws DotDataException { + DotConnect dc = new DotConnect(); + dc.setSQL(String.format("DELETE FROM %s WHERE job_name = %s", TRIGGER_TABLE_NAME, JOB_NAME)); + dc.loadResult(); + } + + private void removeCron() throws DotDataException { + DotConnect dc = new DotConnect(); + dc.setSQL(String.format("DELETE FROM %s WHERE trigger_name = %s", CRON_TABLE_NAME, TRIGGER_NAME)); + dc.loadResult(); + } +} diff --git a/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java b/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java index e48669802f32..bc9dc6343c23 100644 --- a/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java +++ b/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java @@ -252,6 +252,7 @@ import com.dotmarketing.startup.runonce.Task241014AddTemplateValueOnContentletIndex; import com.dotmarketing.startup.runonce.Task241015ReplaceLanguagesWithLocalesPortlet; import com.dotmarketing.startup.runonce.Task241016AddCustomLanguageVariablesPortletToLayout; +import com.dotmarketing.startup.runonce.Task250107RemoveEsReadOnlyMonitorJob; import com.google.common.collect.ImmutableList; import java.util.ArrayList; @@ -576,7 +577,8 @@ public static List> getStartupRunOnceTaskClasses() { .add(Task241013RemoveFullPathLcColumnFromIdentifier.class) .add(Task241014AddTemplateValueOnContentletIndex.class) .add(Task241015ReplaceLanguagesWithLocalesPortlet.class) - .add(Task241016AddCustomLanguageVariablesPortletToLayout.class) + .add(Task241016AddCustomLanguageVariablesPortletToLayout.class) + .add(Task250107RemoveEsReadOnlyMonitorJob.class) .build(); return ret.stream().sorted(classNameComparator).collect(Collectors.toList()); } From 8f89214ef51114c6f6765801a87201aadd026359 Mon Sep 17 00:00:00 2001 From: gortiz-dotcms Date: Wed, 8 Jan 2025 12:36:59 -0300 Subject: [PATCH 2/3] chore: add doc and final modifier (#30993) --- .../Task250107RemoveEsReadOnlyMonitorJob.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java index 7a8c748c014d..df5dc1f46a47 100644 --- a/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java +++ b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJob.java @@ -6,7 +6,9 @@ import com.dotmarketing.startup.StartupTask; import java.util.ArrayList; - +/** + * Deletes the EsReadOnlyMonitorJob from the database and all the related records. + * */ public class Task250107RemoveEsReadOnlyMonitorJob implements StartupTask { @@ -22,15 +24,14 @@ public class Task250107RemoveEsReadOnlyMonitorJob implements StartupTask { * @return true if EsReadOnlyMonitorJob is found, false otherwise. */ @Override - public boolean forceRun() { + public final boolean forceRun() { String sql=String.format("SELECT * FROM %s WHERE job_name=?", JOB_TABLE_NAME); DotConnect dc=new DotConnect(); dc.setSQL(sql); dc.addParam(JOB_NAME); try { - ArrayList results=dc.loadResults(); - if (!results.isEmpty()){ + if (!dc.loadResults().isEmpty()){ return true; } } catch (DotDataException e) { @@ -40,7 +41,7 @@ public boolean forceRun() { } @Override - public void executeUpgrade() throws DotDataException, DotRuntimeException { + public final void executeUpgrade() throws DotDataException, DotRuntimeException { removeCron(); removeTrigger(); removeJob(); @@ -48,19 +49,22 @@ public void executeUpgrade() throws DotDataException, DotRuntimeException { private void removeJob() throws DotDataException { DotConnect dc = new DotConnect(); - dc.setSQL(String.format("DELETE FROM %s WHERE job_name = %s", JOB_TABLE_NAME, JOB_NAME)); + dc.setSQL(String.format("DELETE FROM %s WHERE job_name = ?", JOB_TABLE_NAME)); + dc.addParam(JOB_NAME); dc.loadResult(); } private void removeTrigger() throws DotDataException { DotConnect dc = new DotConnect(); - dc.setSQL(String.format("DELETE FROM %s WHERE job_name = %s", TRIGGER_TABLE_NAME, JOB_NAME)); + dc.setSQL(String.format("DELETE FROM %s WHERE job_name = ?", TRIGGER_TABLE_NAME)); + dc.addParam(JOB_NAME); dc.loadResult(); } private void removeCron() throws DotDataException { DotConnect dc = new DotConnect(); - dc.setSQL(String.format("DELETE FROM %s WHERE trigger_name = %s", CRON_TABLE_NAME, TRIGGER_NAME)); + dc.setSQL(String.format("DELETE FROM %s WHERE trigger_name = ?", CRON_TABLE_NAME)); + dc.addParam(TRIGGER_NAME); dc.loadResult(); } } From 0c2aa3b75ac9ba3693dab06f8a450cc885993d21 Mon Sep 17 00:00:00 2001 From: gortiz-dotcms Date: Wed, 8 Jan 2025 12:37:17 -0300 Subject: [PATCH 3/3] test: add test (#30993) --- ...k250107RemoveEsReadOnlyMonitorJobTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJobTest.java diff --git a/dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJobTest.java b/dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJobTest.java new file mode 100644 index 000000000000..4a0a5b47f899 --- /dev/null +++ b/dotcms-integration/src/test/java/com/dotmarketing/startup/runonce/Task250107RemoveEsReadOnlyMonitorJobTest.java @@ -0,0 +1,62 @@ +package com.dotmarketing.startup.runonce; + +import com.dotcms.util.IntegrationTestInitService; +import com.dotmarketing.common.db.DotConnect; +import com.dotmarketing.exception.DotDataException; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.Assert; + +import java.sql.SQLException; + +public class Task250107RemoveEsReadOnlyMonitorJobTest { + + + private final String JOB_NAME = "EsReadOnlyMonitorJob"; + private final String TRIGGER_NAME = "trigger29"; + private final String CRON_TABLE_NAME = "qrtz_excl_cron_triggers"; + private final String TRIGGER_TABLE_NAME = "qrtz_excl_triggers"; + private final String JOB_TABLE_NAME = "qrtz_excl_job_details"; + + + @BeforeClass + public static void prepare() throws Exception { + IntegrationTestInitService.getInstance().init(); + } + + + @Test + public void test_job() throws DotDataException, SQLException { + final DotConnect dc = new DotConnect(); + + Task250107RemoveEsReadOnlyMonitorJob upgradeTask = new Task250107RemoveEsReadOnlyMonitorJob(); + + if (!upgradeTask.forceRun()) { + dc.setSQL(String.format("INSERT INTO %s (job_name, job_group, job_class_name, is_durable, is_volatile, is_stateful, requests_recovery) VALUES (?, 'dotcms_jobs', 'com.dotmarketing.quartz.job.EsReadOnlyMonitorJob', 'f', 'f', 'f', 'f')", JOB_TABLE_NAME)); + dc.addParam(JOB_NAME); + dc.loadResults(); + + dc.setSQL(String.format("INSERT INTO %s (job_name, trigger_name, trigger_group, job_group, is_volatile, trigger_state, trigger_type, start_time) VALUES (?,?, 'group98', 'dotcms_jobs', 'f', 'WAITING', 'CRON', 17362" + + "84300000)", TRIGGER_TABLE_NAME)); + dc.addParam(JOB_NAME); + dc.addParam(TRIGGER_NAME); + dc.loadResults(); + + dc.setSQL(String.format("INSERT INTO %s (trigger_name, trigger_group, cron_expression) VALUES (?, 'group98', '0 */5 * ? * *')", CRON_TABLE_NAME)); + dc.addParam(TRIGGER_NAME); + dc.loadResults(); + + Assert.assertFalse(dc.setSQL("SELECT * FROM " + JOB_TABLE_NAME + " WHERE job_name = '" + JOB_NAME + "'").loadResults().isEmpty()); + } + + + Assert.assertTrue(upgradeTask.forceRun()); + + upgradeTask.executeUpgrade(); + + Assert.assertTrue(dc.setSQL("SELECT * FROM " + JOB_TABLE_NAME + " WHERE job_name = '" + JOB_NAME + "'").loadResults().isEmpty()); + + } + + +}