Skip to content

Commit

Permalink
Added Integration Tests, Unique key on UUID column
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwilshire committed Nov 4, 2024
1 parent 069b660 commit 78167c4
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.box.l10n.mojito.json.ObjectMapper;
import com.box.l10n.mojito.service.scheduledjob.ScheduledJobProperties;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
Expand All @@ -24,6 +25,7 @@ public class ScheduledJob {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Basic(optional = false)
@Column(name = "uuid")
private String uuid;

Expand All @@ -42,6 +44,7 @@ public class ScheduledJob {

@Transient private ScheduledJobProperties properties;

@Basic(optional = false)
@Column(name = "properties")
private String propertiesString;

Expand All @@ -55,6 +58,7 @@ public class ScheduledJob {
@Column(name = "end_date")
private ZonedDateTime endDate;

@Basic(optional = false)
@Column(name = "enabled")
private Boolean enabled = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
Expand Down Expand Up @@ -124,7 +125,11 @@ public void scheduleAllJobs() throws ClassNotFoundException, SchedulerException
scheduledJobStatusRepository.findByEnum(ScheduledJobStatus.SCHEDULED));
scheduledJob.setStartDate(null);
scheduledJob.setEndDate(null);
scheduledJobRepository.save(scheduledJob);
deadlockRetryTemplate.execute(
c -> {
scheduledJobRepository.save(scheduledJob);
return null;
});
}
}

Expand Down Expand Up @@ -178,21 +183,19 @@ public void pushJobsToDB() {
public void cleanQuartzJobs() throws SchedulerException {
// Clean Quartz jobs that don't exist in the UUID pool
logger.info("Performing Quartz scheduled jobs clean up");
List<String> jobTypes = Arrays.stream(ScheduledJobType.values()).map(Enum::toString).toList();
for (JobKey jobKey : getScheduler().getJobKeys(GroupMatcher.anyGroup())) {

// The job type may not be a Scheduled job (this is only the case if the configuration is set
// to use the default scheduler) so continue without deleting.
if (!jobTypes.contains(jobKey.getGroup())) continue;

for (JobKey jobKey : getAllJobKeys()) {
if (!uuidPool.contains(jobKey.getName())) {
if (getScheduler().checkExists(jobKey)) {
getScheduler().deleteJob(jobKey);
}

scheduledJobRepository
.findByUuid(jobKey.getName())
.ifPresent(scheduledJobRepository::delete);
deadlockRetryTemplate.execute(
c -> {
scheduledJobRepository
.findByUuid(jobKey.getName())
.ifPresent(scheduledJobRepository::delete);
return null;
});

logger.info(
"Removed job with id: '{}' as it is no longer in the data source.", jobKey.getName());
Expand Down Expand Up @@ -245,6 +248,21 @@ public Scheduler getScheduler() {
return schedulerManager.getScheduler(schedulerName);
}

/**
* Retrieve all jobs where the group is in the ScheduledJobType enums. Cannot assume all jobs
* under the scheduler is a scheduled job as the scheduler could be the default scheduler.
*/
public List<JobKey> getAllJobKeys() throws SchedulerException {
List<String> jobTypes = Arrays.stream(ScheduledJobType.values()).map(Enum::toString).toList();
return getScheduler().getJobKeys(GroupMatcher.anyGroup()).stream()
.filter(jobKey -> jobTypes.contains(jobKey.getGroup()))
.collect(Collectors.toList());
}

public List<String> getAllJobNames() throws SchedulerException {
return getAllJobKeys().stream().map(JobKey::getName).collect(Collectors.toList());
}

public Class<? extends IScheduledJob> loadJobClass(String className)
throws ClassNotFoundException {
Class<?> clazz = Class.forName(className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ CREATE TABLE scheduled_job_status_type (
name VARCHAR(255) NOT NULL
);

ALTER TABLE scheduled_job
ADD UNIQUE KEY UK__JOB_UUID(uuid);

ALTER TABLE scheduled_job
ADD CONSTRAINT FK__SCHEDULED_JOB__IMPORT_REPOSITORY__ID
FOREIGN KEY (repository_id) REFERENCES repository (id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,35 @@

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

@Component
public class NoOpScheduledJobTest implements IScheduledJob {
public static boolean throwException = false;
public static boolean successEvent = false;
public static boolean failureEvent = false;

@Override
public void onSuccess(JobExecutionContext context) {}
public void onSuccess(JobExecutionContext context) {
successEvent = true;
}

@Override
public void onFailure(JobExecutionContext context, JobExecutionException jobException) {}
public void onFailure(JobExecutionContext context, JobExecutionException jobException) {
failureEvent = true;
}

@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {}
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

if (throwException) {
throw new JobExecutionException();
}

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit 78167c4

Please sign in to comment.