From 312c4f20cdc58fdf8c71b1711dd4997fe586218d Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 2 Oct 2024 17:33:31 +0100 Subject: [PATCH] Attached the job properties to the Job Type in the enum definition --- .../box/l10n/mojito/entity/ScheduledJob.java | 15 +++++++------- .../scheduledjob/ScheduledJobManager.java | 19 +++++++++--------- .../scheduledjob/ScheduledJobType.java | 20 +++++++++++++------ .../jobs/ScheduledThirdPartySync.java | 18 ++++++++++++----- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/webapp/src/main/java/com/box/l10n/mojito/entity/ScheduledJob.java b/webapp/src/main/java/com/box/l10n/mojito/entity/ScheduledJob.java index db725b2fa7..38fc787ffa 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/entity/ScheduledJob.java +++ b/webapp/src/main/java/com/box/l10n/mojito/entity/ScheduledJob.java @@ -5,7 +5,6 @@ import com.box.l10n.mojito.service.scheduledjob.ScheduledJobProperties; import com.box.l10n.mojito.service.scheduledjob.ScheduledJobStatus; import com.box.l10n.mojito.service.scheduledjob.ScheduledJobType; -import com.box.l10n.mojito.service.scheduledjob.jobs.ScheduledThirdPartySyncProperties; import com.fasterxml.jackson.annotation.JsonView; import jakarta.persistence.Basic; import jakarta.persistence.Column; @@ -58,14 +57,15 @@ public class ScheduledJob extends BaseEntity { @PostLoad public void deserializeProperties() { ObjectMapper objectMapper = new ObjectMapper(); - try { - if (jobType == ScheduledJobType.THIRD_PARTY_SYNC) { - this.properties = - objectMapper.readValue(propertiesString, ScheduledThirdPartySyncProperties.class); - } + this.properties = objectMapper.readValue(propertiesString, jobType.getPropertiesClass()); } catch (Exception e) { - throw new RuntimeException("Failed to deserialize properties", e); + throw new RuntimeException( + "Failed to deserialize properties '" + + propertiesString + + "' for class: " + + jobType.getPropertiesClass().getName(), + e); } } @@ -113,7 +113,6 @@ public void setProperties(ScheduledJobProperties properties) { ObjectMapper objectMapper = new ObjectMapper(); try { this.propertiesString = objectMapper.writeValueAsString(this.properties); - System.out.println(propertiesString); } catch (Exception e) { throw new RuntimeException("Failed to serialize properties", e); } diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobManager.java b/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobManager.java index d4cd214cf8..073d26e84f 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobManager.java +++ b/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobManager.java @@ -9,7 +9,6 @@ import jakarta.annotation.PostConstruct; import java.util.List; import org.quartz.CronScheduleBuilder; -import org.quartz.Job; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.JobKey; @@ -74,7 +73,7 @@ public void cleanupDB() throws SchedulerException { for (JobKey jobKey : getScheduler().getJobKeys(GroupMatcher.anyGroup())) { if (scheduledJobRepository.findByJobKey(jobKey) == null) { logger.info( - "Removing {} job for repo ID: {} as it is no longer the scheduled_job table.", + "Removing {} job for repo ID: {} as it's no longer the 'scheduled_job' table.", jobKey.getGroup(), jobKey.getName()); getScheduler().deleteJob(jobKey); @@ -112,19 +111,20 @@ public void pushJobToDB(ThirdPartySyncJobConfig jobConfig) { scheduledJobRepository.save(scheduledJob); } catch (DataIntegrityViolationException e) { // Attempted to insert another scheduled job into the table with the same repo and job type, - // this can happen in a clustered quartz environment, just ignore + // this can happen in a clustered quartz environment, don't need to display the error. } } public void scheduleJobs() throws ClassNotFoundException, SchedulerException { - List scheduledJobES = scheduledJobRepository.findAll(); + List scheduledJobs = scheduledJobRepository.findAll(); - for (ScheduledJob scheduledJob : scheduledJobES) { + for (ScheduledJob scheduledJob : scheduledJobs) { JobKey jobKey = getJobKey(scheduledJob); TriggerKey triggerKey = getTriggerKey(scheduledJob); // Retrieve job class from enum value e.g. THIRD_PARTY_SYNC -> ScheduledThirdPartySync - Class jobType = loadJobClass(scheduledJob.getJobType().getClassName()); + Class jobType = + loadJobClass(scheduledJob.getJobType().getJobClassName()); JobDetail job = JobBuilder.newJob(jobType).withIdentity(jobKey).build(); @@ -175,12 +175,13 @@ public boolean triggerJob(String jobName, String jobGroup) throws SchedulerExcep } } - public Class loadJobClass(String className) throws ClassNotFoundException { + public Class loadJobClass(String className) + throws ClassNotFoundException { Class clazz = Class.forName(className); - // Check if the class implements the Job interface + // Reflection to check if the class implements the IScheduledJob interface if (!IScheduledJob.class.isAssignableFrom(clazz)) { throw new IllegalArgumentException( - "Class " + className + " does not implement Job interface."); + "Class " + className + " does not implement IScheduledJob interface."); } return clazz.asSubclass(IScheduledJob.class); } diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobType.java b/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobType.java index 08f23fdfee..998ffe7139 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobType.java +++ b/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/ScheduledJobType.java @@ -1,17 +1,25 @@ package com.box.l10n.mojito.service.scheduledjob; import com.box.l10n.mojito.service.scheduledjob.jobs.ScheduledThirdPartySync; +import com.box.l10n.mojito.service.scheduledjob.jobs.ScheduledThirdPartySyncProperties; public enum ScheduledJobType { - THIRD_PARTY_SYNC(ScheduledThirdPartySync.class.getName()); + THIRD_PARTY_SYNC( + ScheduledThirdPartySync.class.getName(), ScheduledThirdPartySyncProperties.class); - final String className; + final String jobClassName; + final Class propertiesClass; - ScheduledJobType(String className) { - this.className = className; + ScheduledJobType(String jobClassName, Class propertiesClass) { + this.jobClassName = jobClassName; + this.propertiesClass = propertiesClass; } - public String getClassName() { - return className; + public String getJobClassName() { + return jobClassName; + } + + public Class getPropertiesClass() { + return propertiesClass; } } diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/jobs/ScheduledThirdPartySync.java b/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/jobs/ScheduledThirdPartySync.java index b4f40ffcb5..b7105519cc 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/jobs/ScheduledThirdPartySync.java +++ b/webapp/src/main/java/com/box/l10n/mojito/service/scheduledjob/jobs/ScheduledThirdPartySync.java @@ -4,6 +4,7 @@ import com.box.l10n.mojito.quartz.QuartzPollableTaskScheduler; import com.box.l10n.mojito.service.scheduledjob.IScheduledJob; import com.box.l10n.mojito.service.scheduledjob.ScheduledJobRepository; +import com.box.l10n.mojito.service.thirdparty.ThirdPartySyncJob; import com.box.l10n.mojito.service.thirdparty.ThirdPartySyncJobInput; import java.util.ArrayList; import java.util.List; @@ -24,9 +25,17 @@ public class ScheduledThirdPartySync implements IScheduledJob { @Autowired QuartzPollableTaskScheduler quartzPollableTaskScheduler; @Autowired private ScheduledJobRepository scheduledJobRepository; + public static int runAmount = 0; + @SuppressWarnings("unchecked") @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { + if (runAmount == 1) { + return; + } + + runAmount++; + ScheduledJob job = scheduledJobRepository.findByJobKey(jobExecutionContext.getJobDetail().getKey()); ScheduledThirdPartySyncProperties properties = @@ -58,11 +67,10 @@ public void execute(JobExecutionContext jobExecutionContext) throws JobExecution thirdPartySync.setOptions(options); try { - Thread.sleep(10000); - // quartzPollableTaskScheduler - // .scheduleJobWithCustomTimeout( - // ThirdPartySyncJob.class, thirdPartySync, "thirdPartySync", 3600L) - // .get(); + quartzPollableTaskScheduler + .scheduleJobWithCustomTimeout( + ThirdPartySyncJob.class, thirdPartySync, "thirdPartySync", 3600L) + .get(); } catch (Exception e) { throw new JobExecutionException(e); }