diff --git a/src/main/java/org/avniproject/etl/controller/backgroundJob/EtlJobController.java b/src/main/java/org/avniproject/etl/controller/backgroundJob/EtlJobController.java index 61cc1e5..0bd9db3 100644 --- a/src/main/java/org/avniproject/etl/controller/backgroundJob/EtlJobController.java +++ b/src/main/java/org/avniproject/etl/controller/backgroundJob/EtlJobController.java @@ -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())); @@ -97,10 +101,27 @@ private ResponseEntity 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; } diff --git a/src/main/java/org/avniproject/etl/domain/quartz/ScheduledJobRun.java b/src/main/java/org/avniproject/etl/domain/quartz/ScheduledJobRun.java index 7251666..982ce5c 100644 --- a/src/main/java/org/avniproject/etl/domain/quartz/ScheduledJobRun.java +++ b/src/main/java/org/avniproject/etl/domain/quartz/ScheduledJobRun.java @@ -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));