From 71a16c9bc81c24061f69a45f1cd84eca46f7b3a4 Mon Sep 17 00:00:00 2001 From: TheConfusedCat Date: Fri, 18 Oct 2024 10:22:11 +0200 Subject: [PATCH] Added support for timezones in cron expressions --- .../deployment/model/JobCronScheduling.java | 26 ++++++++++++------- .../processing/QuartzScheduling.java | 13 ++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/job-scheduling-deployment-model/src/tribefire/extension/job_scheduling/deployment/model/JobCronScheduling.java b/job-scheduling-deployment-model/src/tribefire/extension/job_scheduling/deployment/model/JobCronScheduling.java index 3d1d050..715f4d7 100644 --- a/job-scheduling-deployment-model/src/tribefire/extension/job_scheduling/deployment/model/JobCronScheduling.java +++ b/job-scheduling-deployment-model/src/tribefire/extension/job_scheduling/deployment/model/JobCronScheduling.java @@ -15,26 +15,24 @@ // ============================================================================ package tribefire.extension.job_scheduling.deployment.model; +import java.util.TimeZone; + import com.braintribe.model.descriptive.HasCredentials; -import com.braintribe.model.extensiondeployment.ServiceProcessor; -import com.braintribe.model.extensiondeployment.Worker; -import com.braintribe.model.generic.annotation.Abstract; import com.braintribe.model.generic.reflection.EntityType; import com.braintribe.model.generic.reflection.EntityTypes; /** *

- * This interface is designed to provide an extension point for running scheduled {@link Job Jobs} in an interval - * defined by {@link #setCronExpression(String)}. + * This interface is designed to provide an extension point for running scheduled {@link Job Jobs} in an interval defined by + * {@link #setCronExpression(String)}. * *

- * As {@link HasCredentials} is defined, meaning that user credentials can be configured to define the execution scope - * (in case user and password credentials are left empty, the internal default user session scope is used). + * As {@link HasCredentials} is defined, meaning that user credentials can be configured to define the execution scope (in case user and password + * credentials are left empty, the internal default user session scope is used). * *

- * In case the scheduler is unable to execute scheduled jobs (e.g. the scheduler shut down), - * {@link #setCoalescing(boolean)} defines, if job executions are coalesced to avoid execution several times in - * succession. + * In case the scheduler is unable to execute scheduled jobs (e.g. the scheduler shut down), {@link #setCoalescing(boolean)} defines, if job + * executions are coalesced to avoid execution several times in succession. */ public interface JobCronScheduling extends JobScheduling { @@ -45,4 +43,12 @@ public interface JobCronScheduling extends JobScheduling { */ void setCronExpression(String value); String getCronExpression(); + + /** + * The time zone ID to be used for the cron expression. If not specified, the system's default time zone will be used. See + * {@link TimeZone#getTimeZone(String)} for details on the possible values. + */ + void setTimeZoneId(String timeZoneId); + String getTimeZoneId(); + } diff --git a/job-scheduling-quartz-processor/src/tribefire/extension/job_scheduling/processing/QuartzScheduling.java b/job-scheduling-quartz-processor/src/tribefire/extension/job_scheduling/processing/QuartzScheduling.java index 5e504de..da8fc1c 100644 --- a/job-scheduling-quartz-processor/src/tribefire/extension/job_scheduling/processing/QuartzScheduling.java +++ b/job-scheduling-quartz-processor/src/tribefire/extension/job_scheduling/processing/QuartzScheduling.java @@ -16,6 +16,7 @@ package tribefire.extension.job_scheduling.processing; import java.util.Map; +import java.util.TimeZone; import java.util.concurrent.ConcurrentHashMap; import org.quartz.CronExpression; @@ -47,6 +48,7 @@ import com.braintribe.model.securityservice.credentials.UserPasswordCredentials; import com.braintribe.model.securityservice.credentials.identification.UserNameIdentification; import com.braintribe.model.service.api.ServiceRequest; +import com.braintribe.utils.StringTools; import tribefire.extension.job_scheduling.api.api.JobRequest; import tribefire.extension.job_scheduling.api.api.JobResponse; @@ -102,6 +104,12 @@ public void start(WorkerContext workerContext) throws WorkerException { CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(deployable.getCronExpression()); + String timeZoneId = deployable.getTimeZoneId(); + if (!StringTools.isBlank(timeZoneId)) { + TimeZone tz = TimeZone.getTimeZone(timeZoneId); + cronSchedule = cronSchedule.inTimeZone(tz); + } + trigger = TriggerBuilder.newTrigger().withSchedule(cronSchedule).forJob(jobDetail).build(); scheduler.scheduleJob(jobDetail, trigger); @@ -246,9 +254,10 @@ public void postConstruct() { if (deployable.getJobRequestProcessor() == null) { throw new IllegalStateException("Deployment not successful, because jobRequestProcessor is not set on: " + this); } - + if (deployable.getJobRequestProcessor().getExternalId() == null) { - throw new IllegalStateException("Deployment not successful, because jobRequestProcessor.externalId is not set on: " + deployable.getJobRequestProcessor()); + throw new IllegalStateException( + "Deployment not successful, because jobRequestProcessor.externalId is not set on: " + deployable.getJobRequestProcessor()); } } }