Skip to content

Commit

Permalink
#110 | Fix start_date logging in scheduled_job_run to be in line with…
Browse files Browse the repository at this point in the history
… actual job execution and add a higher priority trigger for first run of ETL Sync job for an org
  • Loading branch information
1t5j0y committed Dec 13, 2024
1 parent c67edd2 commit 3acfb02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public ResponseEntity createJob(@RequestBody JobScheduleRequest jobScheduleReque

JobDetailImpl jobDetail = getJobDetail(jobScheduleRequest, organisationIdentity, organisationIdentitiesInGroup);
scheduler.addJob(jobDetail, false);
if (jobScheduleRequest.getJobGroup().equals(JobGroup.Sync)) {
Trigger triggerForFirstRun = getHigherPriorityTriggerForFirstRun(jobScheduleRequest, jobDetail);
scheduler.scheduleJob(triggerForFirstRun);
}
Trigger trigger = getTrigger(jobScheduleRequest, jobDetail);
scheduler.scheduleJob(trigger);
logger.info(String.format("%s type job Scheduled for %s:%s", jobScheduleRequest.getJobGroup(), jobScheduleRequest.getJobEntityType(), jobScheduleRequest.getEntityUUID()));
Expand All @@ -97,10 +101,27 @@ private ResponseEntity<String> validateRequest(JobScheduleRequest jobScheduleReq
return null;
}

private Trigger getTrigger(JobScheduleRequest jobScheduleRequest, JobDetailImpl jobDetail) {
SimpleScheduleBuilder scheduleBuilder = simpleSchedule().withIntervalInMinutes(jobScheduleRequest.getJobGroup().equals(JobGroup.Sync) ? scheduledJobConfig.getSyncRepeatIntervalInMinutes() : scheduledJobConfig.getMediaAnalysisRepeatIntervalInMinutes()).repeatForever();
private Trigger getHigherPriorityTriggerForFirstRun(JobScheduleRequest jobScheduleRequest, JobDetailImpl jobDetail) {
Trigger trigger = newTrigger()
.withIdentity(scheduledJobConfig.getTriggerKey(jobScheduleRequest.getEntityUUID() + "run1", jobScheduleRequest.getJobGroup()))
.forJob(jobDetail)
.startAt(DateTimeUtil.nowPlusSeconds(5))
.withPriority(6) //higher than default (5)
.build();
return trigger;
}

Trigger trigger = newTrigger().withIdentity(scheduledJobConfig.getTriggerKey(jobScheduleRequest.getEntityUUID(), jobScheduleRequest.getJobGroup())).forJob(jobDetail).withSchedule(scheduleBuilder).startAt(DateTimeUtil.nowPlusSeconds(5)).build();
private Trigger getTrigger(JobScheduleRequest jobScheduleRequest, JobDetailImpl jobDetail) {
int repeatIntervalMinutes = jobScheduleRequest.getJobGroup().equals(JobGroup.Sync) ? scheduledJobConfig.getSyncRepeatIntervalInMinutes() : scheduledJobConfig.getMediaAnalysisRepeatIntervalInMinutes();
int startAtNowPlusSeconds = jobScheduleRequest.getJobGroup().equals(JobGroup.Sync) ? repeatIntervalMinutes * 60 : 5;
SimpleScheduleBuilder scheduleBuilder = simpleSchedule().withIntervalInMinutes(repeatIntervalMinutes).repeatForever();

Trigger trigger = newTrigger()
.withIdentity(scheduledJobConfig.getTriggerKey(jobScheduleRequest.getEntityUUID(), jobScheduleRequest.getJobGroup()))
.forJob(jobDetail)
.withSchedule(scheduleBuilder)
.startAt(DateTimeUtil.nowPlusSeconds(startAtNowPlusSeconds))
.build();
return trigger;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ public class ScheduledJobRun {
public static ScheduledJobRun create(JobDetail jobDetail, Trigger trigger) {
ScheduledJobRun scheduledJobRun = new ScheduledJobRun();
scheduledJobRun.jobName = jobDetail.getKey().getName();
scheduledJobRun.startedAt = trigger.getNextFireTime();
scheduledJobRun.startedAt = new Date();
return scheduledJobRun;
}

public void ended(JobExecutionException jobException, Trigger trigger) {
this.startedAt = trigger.getPreviousFireTime();
this.endedAt = new Date();
if (jobException != null)
errorMessage = ExceptionUtil.getStackTraceAsString(getAppException(jobException));
Expand Down

0 comments on commit 3acfb02

Please sign in to comment.