From fa1de68e223dcd8495f468846d1d521f3faf0020 Mon Sep 17 00:00:00 2001 From: himeshr Date: Tue, 22 Aug 2023 11:53:04 +0530 Subject: [PATCH] #67 | Modify Avni scheduled jobs configuration for repeatInterval and misfireThreshold --- .../backgroundJob/EtlJobController.java | 36 ++++++++++++------- .../resources/main-application.properties | 6 ++-- 2 files changed, 28 insertions(+), 14 deletions(-) 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 07fe923..b860358 100644 --- a/src/main/java/org/avniproject/etl/controller/backgroundJob/EtlJobController.java +++ b/src/main/java/org/avniproject/etl/controller/backgroundJob/EtlJobController.java @@ -15,6 +15,7 @@ import org.quartz.JobDataMap; import org.quartz.Scheduler; import org.quartz.SchedulerException; +import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.impl.JobDetailImpl; import org.springframework.beans.factory.annotation.Autowired; @@ -85,6 +86,28 @@ public ResponseEntity createJob(@RequestBody JobScheduleRequest jobScheduleReque if (latestJobRun != null) return ResponseEntity.badRequest().body("Job already present"); + JobDetailImpl jobDetail = getJobDetail(jobScheduleRequest, organisationIdentity, organisationIdentitiesInGroup); + scheduler.addJob(jobDetail, false); + Trigger trigger = getTrigger(jobScheduleRequest, jobDetail); + scheduler.scheduleJob(trigger); + logger.info(String.format("Job Scheduled for %s:%s", jobScheduleRequest.getJobEntityType(), jobScheduleRequest.getEntityUUID())); + return ResponseEntity.ok().body("Job Scheduled!"); + } + + private Trigger getTrigger(JobScheduleRequest jobScheduleRequest, JobDetailImpl jobDetail) { + SimpleScheduleBuilder scheduleBuilder = simpleSchedule() + .withIntervalInMinutes(scheduledJobConfig.getRepeatIntervalInMinutes()).repeatForever(); + + Trigger trigger = newTrigger() + .withIdentity(scheduledJobConfig.getTriggerKey(jobScheduleRequest.getEntityUUID())) + .forJob(jobDetail) + .withSchedule(scheduleBuilder) + .startAt(DateTimeUtil.nowPlusSeconds(5)) + .build(); + return trigger; + } + + private JobDetailImpl getJobDetail(JobScheduleRequest jobScheduleRequest, OrganisationIdentity organisationIdentity, List organisationIdentitiesInGroup) { JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setJobClass(EtlJob.class); jobDetail.setDurability(true); @@ -95,18 +118,7 @@ public ResponseEntity createJob(@RequestBody JobScheduleRequest jobScheduleReque jobDetail.setName(jobScheduleRequest.getEntityUUID()); JobDataMap jobDataMap = scheduledJobConfig.createJobData(jobScheduleRequest.getJobEntityType()); jobDetail.setJobDataMap(jobDataMap); - scheduler.addJob(jobDetail, false); - - Trigger trigger = newTrigger() - .withIdentity(scheduledJobConfig.getTriggerKey(jobScheduleRequest.getEntityUUID())) - .forJob(jobDetail) - .withSchedule(simpleSchedule().withIntervalInMinutes(scheduledJobConfig.getRepeatIntervalInMinutes()).repeatForever()) - .startAt(DateTimeUtil.nowPlusSeconds(5)) - .build(); - - scheduler.scheduleJob(trigger); - logger.info(String.format("Job Scheduled for %s:%s", jobScheduleRequest.getJobEntityType(), jobScheduleRequest.getEntityUUID())); - return ResponseEntity.ok().body("Job Scheduled!"); + return jobDetail; } @PreAuthorize("hasAnyAuthority('admin')") diff --git a/src/main/resources/main-application.properties b/src/main/resources/main-application.properties index b1630a5..1e82838 100644 --- a/src/main/resources/main-application.properties +++ b/src/main/resources/main-application.properties @@ -13,6 +13,10 @@ spring.quartz.scheduler-name=${app.name} spring.quartz.job-store-type=jdbc spring.quartz.properties.org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate spring.quartz.properties.org.quartz.threadPool.threadCount=${ETL_JOB_THREAD_COUNT:1} +spring.quartz.properties.org.quartz.jobStore.misfireThreshold = ${AVNI_SCHEDULED_JOB_TRIGGER_MISFIRE_THRESHOLD_IN_MILLISECONDS:3600000} + +# Internal Scheduler config +avni.scheduledJob.repeatIntervalInMinutes=${AVNI_SCHEDULED_JOB_REPEAT_INTERVAL_IN_MINUTES:90} #S3 Parameters avni.bucket.name=${OPENCHS_BUCKET_NAME:dummy} @@ -43,8 +47,6 @@ avni.keycloak.realms=%s/realms/%s avni.keycloak.realm=On-premise avni.idp.type=${AVNI_IDP_TYPE:none} -avni.scheduledJob.repeatIntervalInMinutes=${AVNI_SCHEDULED_JOB_REPEAT_INTERVAL_IN_MINUTES:60} - avni.security.allowedOrigins=${AVNI_SECURITY_ALLOWED_ORIGINS:http://localhost:6010} spring.security.user.name=admin