Skip to content

Commit

Permalink
Attached the job properties to the Job Type in the enum definition
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwilshire committed Oct 2, 2024
1 parent 367138e commit 312c4f2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<ScheduledJob> scheduledJobES = scheduledJobRepository.findAll();
List<ScheduledJob> 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<? extends Job> jobType = loadJobClass(scheduledJob.getJobType().getClassName());
Class<? extends IScheduledJob> jobType =
loadJobClass(scheduledJob.getJobType().getJobClassName());

JobDetail job = JobBuilder.newJob(jobType).withIdentity(jobKey).build();

Expand Down Expand Up @@ -175,12 +175,13 @@ public boolean triggerJob(String jobName, String jobGroup) throws SchedulerExcep
}
}

public Class<? extends Job> loadJobClass(String className) throws ClassNotFoundException {
public Class<? extends IScheduledJob> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends ScheduledJobProperties> propertiesClass;

ScheduledJobType(String className) {
this.className = className;
ScheduledJobType(String jobClassName, Class<? extends ScheduledJobProperties> propertiesClass) {
this.jobClassName = jobClassName;
this.propertiesClass = propertiesClass;
}

public String getClassName() {
return className;
public String getJobClassName() {
return jobClassName;
}

public Class<? extends ScheduledJobProperties> getPropertiesClass() {
return propertiesClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 312c4f2

Please sign in to comment.