Skip to content

Commit

Permalink
Added support for timezones in cron expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheConfusedCat committed Oct 18, 2024
1 parent 9a4740d commit 71a16c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>
* 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)}.
*
* <p>
* 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).
*
* <p>
* 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 {

Expand All @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 71a16c9

Please sign in to comment.