use of org.springframework.scheduling.support.CronSequenceGenerator in project irida by phac-nml.
the class ProjectEventEmailScheduledTaskImpl method getPriorDateFromCronString.
/**
* Get the last time the job was run from the given cron string
*
* @param cron the cron string
* @return A Date of the last time the job was run
*/
public static Date getPriorDateFromCronString(String cron) {
// find the number of milliseconds in the configured time
long timeInMillis = Calendar.getInstance().getTimeInMillis();
CronSequenceGenerator gen = new CronSequenceGenerator(cron);
long futureTime = gen.next(new Date()).getTime();
long difference = futureTime - timeInMillis;
return new Date(timeInMillis - difference);
}
use of org.springframework.scheduling.support.CronSequenceGenerator in project cloudbreak by hortonworks.
the class DateUtils method isTrigger.
public boolean isTrigger(TimeAlert alert, long monitorUpdateRate) {
try {
String timeZone = alert.getTimeZone();
CronSequenceGenerator cronExpression = getCronExpression(alert.getCron());
ZonedDateTime currentTime = dateTimeUtils.getDefaultZonedDateTime();
ZonedDateTime zonedCurrentTime = dateTimeUtils.getZonedDateTime(currentTime.toInstant(), timeZone);
LocalDateTime startTimeOfTheMonitorInterval = zonedCurrentTime.toLocalDateTime().minus(monitorUpdateRate, ChronoUnit.MILLIS);
Date startDate = Date.from(startTimeOfTheMonitorInterval.toInstant(currentTime.getOffset()));
Date nextTime = cronExpression.next(startDate);
ZonedDateTime zonedNextTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(nextTime.getTime()), currentTime.getZone()).atZone(ZoneId.of(timeZone));
long interval = (zonedCurrentTime.toEpochSecond() - zonedNextTime.toEpochSecond()) * SECOND_TO_MILLISEC;
LOGGER.info("Time alert '{}' next firing at '{}' compared to current time '{}' in timezone '{}'", alert.getName(), zonedNextTime, zonedCurrentTime, timeZone);
return interval >= 0 && interval < monitorUpdateRate;
} catch (ParseException e) {
LOGGER.warn("Invalid cron expression, {}", e.getMessage());
return false;
}
}
use of org.springframework.scheduling.support.CronSequenceGenerator in project cuba by cuba-platform.
the class Scheduling method calculateNextCronDate.
protected long calculateNextCronDate(ScheduledTask task, long date, long currentDate, long frame) {
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(task.getCron(), getCurrentTimeZone());
// if last start = 0 (task never has run) or to far in the past, we use (NOW - FRAME) timestamp for pivot time
// this approach should work fine cause cron works with absolute time
long pivotPreviousTime = Math.max(date, currentDate - frame);
Date currentStart = null;
Date nextDate = cronSequenceGenerator.next(new Date(pivotPreviousTime));
while (nextDate.getTime() < currentDate) {
// if next date is in past try to find next date nearest to now
currentStart = nextDate;
nextDate = cronSequenceGenerator.next(nextDate);
}
if (currentStart == null) {
currentStart = nextDate;
}
log.trace("{}\n now={} frame={} currentStart={} lastStart={} cron={}", task, currentDate, frame, currentStart, task.getCron());
return currentStart.getTime();
}
use of org.springframework.scheduling.support.CronSequenceGenerator in project cuba by cuba-platform.
the class CronValidator method validate.
@Override
public void validate(Object value) throws ValidationException {
if (value != null) {
ServerInfoService serverInfoService = AppBeans.get(ServerInfoService.NAME);
Messages messages = AppBeans.get(Messages.NAME);
try {
new CronSequenceGenerator(value.toString(), serverInfoService.getTimeZone());
} catch (Exception e) {
throw new ValidationException(messages.getMessage(CronValidator.class, "validation.cronInvalid"));
}
}
}
use of org.springframework.scheduling.support.CronSequenceGenerator in project commons by terran4j.
the class DSchedulingAspect method isValidTime.
boolean isValidTime(JobExeInfo lastInfo, Scheduled scheduled, DScheduling distributedScheduling, Logger log) {
// 之前没有任务执行,视为有效的执行时间。
if (lastInfo == null) {
return true;
}
// 无效的任务信息,视为无效的执行时间。
if (lastInfo.getBeginTime() == null || lastInfo.getEndTime() == null) {
if (log.isInfoEnabled()) {
log.info("Invalid lastInfo(beginTime OR endTime is null): {}", lastInfo);
}
return false;
}
long currentTime = System.currentTimeMillis();
long lastBeginTime = lastInfo.getBeginTime();
long lastEndTime = lastInfo.getEndTime();
long tolerableTime = tolerableTimeDeviation();
StringValueResolver resolver = getResolver();
// 最早开始的时间点。
Long point = null;
String cornText = scheduled.cron();
if (StringUtils.hasText(cornText)) {
cornText = resolver.resolveStringValue(cornText);
String zone = scheduled.zone();
TimeZone timeZone;
if (StringUtils.hasText(zone)) {
zone = resolver.resolveStringValue(zone);
timeZone = StringUtils.parseTimeZoneString(zone);
} else {
timeZone = TimeZone.getDefault();
}
CronSequenceGenerator corn = new CronSequenceGenerator(cornText, timeZone);
Long currentPoint = corn.next(new Date(lastEndTime)).getTime();
point = getMinPoint(point, currentPoint);
}
long fixedDelay = scheduled.fixedDelay();
if (fixedDelay >= 0) {
Long currentPoint = lastEndTime + fixedDelay;
point = getMinPoint(point, currentPoint);
}
String fixedDelayString = scheduled.fixedDelayString();
if (StringUtils.hasText(fixedDelayString)) {
fixedDelayString = resolver.resolveStringValue(fixedDelayString);
Long currentPoint = lastEndTime + Long.parseLong(fixedDelayString);
point = getMinPoint(point, currentPoint);
}
long fixedRate = scheduled.fixedRate();
if (fixedRate > 0) {
Long currentPoint = lastBeginTime + fixedRate;
point = getMinPoint(point, currentPoint);
}
String fixedRateString = scheduled.fixedRateString();
if (StringUtils.hasText(fixedRateString)) {
fixedRateString = resolver.resolveStringValue(fixedRateString);
Long currentPoint = lastBeginTime + fixedRate;
point = getMinPoint(point, currentPoint);
}
Assert.isTrue(point != null, "Invalid scheduled: " + scheduled);
return point - tolerableTime <= currentTime;
}
Aggregations