private Schedule getSchedule() { return Schedule.forInterval(SCHEDULE_INTERVAL, null); }
/** * Creates a new schedule for the given cron expression. The cron expression is not verified immediately, * but invalid expressions will cause {@link SchedulerService#scheduleJobWithGeneratedId(JobConfig)} to fail. The system's * {@link TimeZone#getDefault() default time zone} is assumed. * * @param cronExpression the cron expression to use for the schedule * @return a schedule for running jobs when the given cron expression is satisfied */ public static Schedule forCronExpression(String cronExpression) { return forCronExpression(cronExpression, null); }
@Nonnull public String getSchedule() { final Schedule schedule = jobDetails.getSchedule(); switch(schedule.getType()) { case CRON_EXPRESSION: return schedule.getCronScheduleInfo().getCronExpression(); case INTERVAL: final long intervalInMillis = schedule.getIntervalScheduleInfo().getIntervalInMillis(); return intervalInMillis == 0 ? getText("admin.schedulerdetails.run.once") : formatDuration(intervalInMillis); default: return schedule.toString(); } }
public Trigger buildTrigger(Schedule schedule) throws SchedulerServiceException { switch (schedule.getType()) { case INTERVAL: return getSimpleTrigger(schedule.getIntervalScheduleInfo()); case CRON_EXPRESSION: return getCronTrigger(schedule.getCronScheduleInfo()); } throw new IllegalStateException("type=" + schedule.getType()); }
@Nullable private static String getCronExpression(final Schedule schedule) { final String cronExpression; if (schedule.getCronScheduleInfo() != null) { cronExpression = schedule.getCronScheduleInfo().getCronExpression(); } else if (schedule.getIntervalScheduleInfo() != null) { final long interval = schedule.getIntervalScheduleInfo().getIntervalInMillis(); cronExpression = new SimpleToCronTriggerConverter().convertToCronString(new Date(), interval).cronString; } else { cronExpression = null; } return cronExpression; }
@Nonnull @Override protected Schedule getSchedule(Trigger trigger) { if (trigger instanceof CronTrigger) { final CronTrigger cron = (CronTrigger) trigger; return Schedule.forCronExpression(cron.getCronExpression(), cron.getTimeZone()); } if (trigger instanceof SimpleTrigger) { final SimpleTrigger simple = (SimpleTrigger) trigger; return Schedule.forInterval(simple.getRepeatInterval(), simple.getStartTime()); } throw new SchedulerRuntimeException("The job with jobId '" + trigger.getName() + "' has an unsupported trigger class: " + trigger.getClass().getName()); }
@Override public void scheduleJob(@Nonnull JobId jobId, @Nonnull JobConfig jobConfig) throws SchedulerServiceException { if (jobConfig.getSchedule().getType() == Schedule.Type.CRON_EXPRESSION) { throw new IllegalArgumentException("The fallback scheduler does not support CRON expressions"); } // always unschedule the job - any exceptions in case the job is not scheduled are swallowed unscheduleJob(jobId); scheduledJobs.put(jobId, jobConfig); IntervalScheduleInfo interval = jobConfig.getSchedule().getIntervalScheduleInfo(); Map<String, Object> contextMap = ImmutableMap.of( KEY_JOB_CONFIG, jobConfig, KEY_JOB_ID, jobId, KEY_SCHEDULER_SERVICE, this); scheduler.scheduleJob(jobId.toString(), JobRunnerAdapter.class, contextMap, interval.getFirstRunTime(), interval.getIntervalInMillis()); }
private JobConfig(JobRunnerKey jobRunnerKey, @Nullable RunMode runMode, @Nullable Schedule schedule, Map<String, Serializable> parameters) { this.jobRunnerKey = jobRunnerKey; this.runMode = (runMode != null) ? runMode : RUN_ONCE_PER_CLUSTER; this.schedule = (schedule != null) ? schedule : Schedule.runOnce(null); // handled by withParameters and would be redundant elsewhere //noinspection AssignmentToCollectionOrArrayFieldFromParameter this.parameters = parameters; }
protected String getFrequencyName() { if (intervalMillis >= 0) { return frequencyToName(intervalMillis); } if (schedule == null || schedule.getIntervalScheduleInfo() == null) { return FREQUENCY_DAILY; } return frequencyToName(schedule.getIntervalScheduleInfo().getIntervalInMillis()); }
public JobRunnerWrapper(final JobRunnerKey jobRunnerKey) { this.jobRunnerKey = jobRunnerKey; for (JobDetails jobDetails : schedulerService.getJobsByJobRunnerKey(jobRunnerKey)) { jobs.add(new JobDetailsWrapper(jobDetails, schedulerHistoryService.getLastRunForJob(jobDetails.getJobId()))); switch(jobDetails.getSchedule().getType()) { case CRON_EXPRESSION: schedule.add(getText("admin.schedulerdetails.schedule.cron")); break; case INTERVAL: schedule.add(getText("admin.schedulerdetails.schedule.interval")); break; default: throw new IllegalArgumentException("unknown scheduler type " + jobDetails.getSchedule().getType()); } } this.numberOfFailedJobs = countNumberOfFailedJobs(jobs); }
private static Schedule getSchedule(final GenericValue genericValue) { final Type type = toScheduleType(genericValue.getString(SCHED_TYPE)); switch (type) { case INTERVAL: return Schedule.forInterval( genericValue.getLong(INTERVAL_MILLIS), toDate(genericValue.getLong(FIRST_RUN))); case CRON_EXPRESSION: return Schedule.forCronExpression( genericValue.getString(CRON_EXPRESSION), toTimeZone(genericValue.getString(TIME_ZONE))); } throw badType(type); }
private static void addScheduleInfo(FieldMap fields, Schedule schedule) { switch (schedule.getType()) { case INTERVAL: fields.add(SCHED_TYPE, TYPE_INTERVAL); fields.add(INTERVAL_MILLIS, schedule.getIntervalScheduleInfo().getIntervalInMillis()); fields.add(FIRST_RUN, toLong(schedule.getIntervalScheduleInfo().getFirstRunTime())); return; case CRON_EXPRESSION: fields.add(SCHED_TYPE, TYPE_CRON); fields.add(CRON_EXPRESSION, schedule.getCronScheduleInfo().getCronExpression()); fields.add(TIME_ZONE, toTimeZoneId(schedule.getCronScheduleInfo().getTimeZone())); return; } throw badType(schedule.getType()); }
public void setSchedule(Schedule schedule) { if (schedule == null) { deleteProperty(PROP_FREQUENCY); deleteProperty(PROP_START_TIME); } else { checkArgument(schedule.getType() == Schedule.Type.INTERVAL, "only interval schedules are supported"); IntervalScheduleInfo info = schedule.getIntervalScheduleInfo(); setProperty(PROP_FREQUENCY, String.valueOf(info.getIntervalInMillis())); if (info.getFirstRunTime() != null) { Calendar cal = new GregorianCalendar(); cal.setTime(info.getFirstRunTime()); setProperty(PROP_START_TIME, cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE)); } else { deleteProperty(PROP_START_TIME); } } }
/** * Creates a new job configuration for the specified job runner key. * <p> * By default, the job configuration will assume: * </p> * <ul> * <li><code>{@link #withRunMode(RunMode) withRunMode}({@link RunMode#RUN_ONCE_PER_CLUSTER})</code></li> * <li><code>{@link #withSchedule(Schedule) withSchedule}({@link Schedule#runOnce(Date) Schedule.runOnce}(new Date()))</code></li> * <li><code>{@link #withParameters(Map) withParameters}(null)</code></li> * </ul> * <p> * Any of which may be overridden by calling the appropriate method. Note that chaining the * methods is recommended, as these methods return an altered copy rather than modifiying the * original {@code JobConfig} in place. For example, use: * </p> * <pre><code> * JobConfig config = JobConfig.forJobRunnerKey("myJobToDoImportantThings") * .withSchedule(Schedule.forInterval(Date, long)) * .withRunMode(RunMode.RUN_LOCALLY); * </code></pre> * * @param jobRunnerKey the unique identifier used to * {@link SchedulerService#registerJobRunner(JobRunnerKey, JobRunner) register} * the {@link JobRunner} * @return a job configuration for the specified job runner key that will use the default settings */ public static JobConfig forJobRunnerKey(JobRunnerKey jobRunnerKey) { requireNonNull(jobRunnerKey, "jobRunnerKey"); return new JobConfig(jobRunnerKey, RUN_ONCE_PER_CLUSTER, Schedule.runOnce(null), NO_PARAMETERS); }
private void getSchedulerDetails(Schedule schedule) { if (schedule != null && schedule.getIntervalScheduleInfo() != null) { IntervalScheduleInfo scheduleInfo = schedule.getIntervalScheduleInfo(); if (scheduleInfo.getFirstRunTime() != null) { Calendar cal = new GregorianCalendar(); cal.setTime(scheduleInfo.getFirstRunTime()); startHour = cal.get(Calendar.HOUR_OF_DAY); startMinute = cal.get(Calendar.MINUTE); intervalMillis = scheduleInfo.getIntervalInMillis(); } } }
/** * Creates a new schedule that will run once at the specified time. Jobs that are scheduled to run once * are not guaranteed to remain in the system after they are attempted, regardless of the {@link RunOutcome}. * Jobs scheduled as {@code runOnce} will be purged automatically after they have run; that is, at some * point after they have run (possibly immediately), {@link SchedulerService#getJobDetails(JobId)} will * no longer return the job's information. * * @param runTime when the job should run; may be {@code null} to indicate that the job should run * as soon as possible * @return a schedule for running once at the given time */ public static Schedule runOnce(@Nullable Date runTime) { return forInterval(0L, runTime); }
@Override public FilterSubscription createSubscription(final ApplicationUser user, final Long filterId, String groupName, final String cronExpression, final Boolean emailOnEmpty) { Schedule schedule = Schedule.forCronExpression(cronExpression); return createSubscription(user, filterId, groupName, emailOnEmpty, schedule); }
private void queueDelayedEvent(final JiraHomeChangeEvent event) { delayedFileReplicationQueue.add(event); JobConfig jobConfig = JobConfig.forJobRunnerKey(DELAYED_REPLICATION_KEY) .withRunMode(RunMode.RUN_LOCALLY) .withSchedule(Schedule.runOnce(DateTime.now().plusSeconds(IDLE_SECONDS).toDate())); try { schedulerService.scheduleJob(DELAYED_REPLICATION_ID, jobConfig); } catch (SchedulerServiceException e) { log.error("Failed to schedule delayed replication", e); } }
private void deleteIfRunOnce() { if (jobDetails != null) { final IntervalScheduleInfo info = jobDetails.getSchedule().getIntervalScheduleInfo(); if (info != null && info.getIntervalInMillis() == 0L) { LOG.debug("deleteIfRunOnce: deleting completed job: {}", jobId); schedulerService.unscheduleJob(jobId); } } }
@Test public void testRunLater() { // But here we're using an exact time and there shouldn't be any mismatch to it final long now = currentTimeMillis(); assertNextRunTime(forInterval(60000L, new Date(now + 15000L)), now + 15000L); }