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 periodically with the given interval. * <p> * <strong>WARNING</strong>: Implementations are not required to honour the time interval at millisecond * precision. The actual resolution that is permitted is implementation-defined. Implementations should * round the requested interval up and persist the rounded value so that the interval reported by * later calls to {@link Schedule#getIntervalScheduleInfo()} will be consistent with the behaviour that the * scheduler actually provides. * </p> * * @param intervalInMillis the minimum time interval (in milliseconds) between runs. If the * value {@code 0L} is specified, then the job will not repeat (this is equivalent to using * {@link #runOnce(Date) runOnce(firstRunTime)}). Negative values are not permitted. * @param firstRunTime when the job should run for the first time; 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 forInterval(long intervalInMillis, @Nullable Date firstRunTime) { return new Schedule(Type.INTERVAL, new IntervalScheduleInfo(firstRunTime, intervalInMillis), null); }
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); } } }
private boolean storeSettings(Schedule newSchedule) { settings = scanService.getReportSettings(); LogScanReportSettings newSettings = new LogScanReportSettings.Builder() .enabled(enabled) .recipients(recipients) .schedule(newSchedule) .build(); if (newSettings.equals(settings)) { // no change validationLog.addFeedback("stp.scheduler.task.unchanged"); return false; } scanService.setReportSettings(newSettings); settings = newSettings; if (enabled) { validationLog.addFeedback("stp.scheduler.task.enabled", frequency, newSchedule.getIntervalScheduleInfo().getFirstRunTime()); } else { validationLog.addFeedback("stp.scheduler.task.disabled"); } return true; }
protected String getFrequencyName() { if (intervalMillis >= 0) { return frequencyToName(intervalMillis); } if (schedule == null || schedule.getIntervalScheduleInfo() == null) { return FREQUENCY_DAILY; } return frequencyToName(schedule.getIntervalScheduleInfo().getIntervalInMillis()); }
private static Trigger getSimpleTrigger(final IntervalScheduleInfo info) { final Date startTime = (info.getFirstRunTime() != null) ? info.getFirstRunTime() : new Date(); final SimpleTrigger trigger = new SimpleTrigger(); trigger.setStartTime(startTime); if (info.getIntervalInMillis() == 0L) { trigger.setRepeatCount(0); } else { trigger.setRepeatCount(REPEAT_INDEFINITELY); trigger.setRepeatInterval(quantizeToMinutes(info.getIntervalInMillis())); } return trigger; }
@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; }
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); } } }
@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(); } }
@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 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()); }