use of com.vip.saturn.job.console.utils.CronExpression in project Saturn by vipshop.
the class UtilsServiceImpl method checkAndForecastCron.
@Override
public ForecastCronResult checkAndForecastCron(final String timeZone, final String cron) throws SaturnJobConsoleException {
if (StringUtils.isBlank(timeZone)) {
throw new SaturnJobConsoleException("timeZone不能为空");
}
if (StringUtils.isBlank(cron)) {
throw new SaturnJobConsoleException("cron不能为空");
}
String timeZoneTrim = timeZone.trim();
String cronTrim = cron.trim();
if (!SaturnConstants.TIME_ZONE_IDS.contains(timeZoneTrim)) {
throw new SaturnJobConsoleException(String.format("timeZone(%s)无效", timeZoneTrim));
}
TimeZone tz = TimeZone.getTimeZone(timeZoneTrim);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(tz);
CronExpression cronExpression = null;
try {
cronExpression = new CronExpression(cronTrim);
} catch (ParseException e) {
throw new SaturnJobConsoleException(String.format("cron(%s)格式错误:%s", cronTrim, e.getMessage()));
}
cronExpression.setTimeZone(tz);
ForecastCronResult forecastCronResult = new ForecastCronResult();
forecastCronResult.setTimeZone(timeZoneTrim);
forecastCronResult.setCron(cronTrim);
forecastCronResult.setNextFireTimes(new ArrayList<String>());
Date now = new Date();
for (int i = 0; i < 10; i++) {
Date next = cronExpression.getNextValidTimeAfter(now);
if (next != null) {
forecastCronResult.getNextFireTimes().add(dateFormat.format(next));
now = next;
}
}
if (forecastCronResult.getNextFireTimes().isEmpty()) {
throw new SaturnJobConsoleException(String.format("cron(%s)可能描述的是一个过去的时间,将不会被执行", cronTrim));
}
return forecastCronResult;
}
use of com.vip.saturn.job.console.utils.CronExpression in project Saturn by vipshop.
the class OutdatedNoRunningJobAnalyzer method getNextFireTimeAfterSpecifiedTimeExcludePausePeriod.
private Long getNextFireTimeAfterSpecifiedTimeExcludePausePeriod(long nextFireTimeAfterThis, String jobName, CuratorRepository.CuratorFrameworkOp curatorFrameworkOp) {
String cronPath = JobNodePath.getConfigNodePath(jobName, "cron");
String cronVal = curatorFrameworkOp.getData(cronPath);
CronExpression cronExpression = null;
try {
cronExpression = new CronExpression(cronVal);
} catch (ParseException e) {
log.error(e.getMessage(), e);
return null;
}
String timeZoneStr = getTimeZone(jobName, curatorFrameworkOp);
TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr);
cronExpression.setTimeZone(timeZone);
Date nextFireTime = cronExpression.getTimeAfter(new Date(nextFireTimeAfterThis));
String pausePeriodDatePath = JobNodePath.getConfigNodePath(jobName, "pausePeriodDate");
String pausePeriodDate = curatorFrameworkOp.getData(pausePeriodDatePath);
String pausePeriodTimePath = JobNodePath.getConfigNodePath(jobName, "pausePeriodTime");
String pausePeriodTime = curatorFrameworkOp.getData(pausePeriodTimePath);
while (nextFireTime != null && isInPausePeriod(nextFireTime, pausePeriodDate, pausePeriodTime, timeZone)) {
nextFireTime = cronExpression.getTimeAfter(nextFireTime);
}
if (null == nextFireTime) {
return null;
}
return nextFireTime.getTime();
}
use of com.vip.saturn.job.console.utils.CronExpression in project Saturn by vipshop.
the class JobDimensionServiceImpl method getNextFireTimeAfterSpecifiedTimeExcludePausePeriod.
@Override
public Long getNextFireTimeAfterSpecifiedTimeExcludePausePeriod(long nextFireTimeAfterThis, String jobName, CuratorRepository.CuratorFrameworkOp curatorFrameworkOp) {
String cronPath = JobNodePath.getConfigNodePath(jobName, "cron");
String cronVal = curatorFrameworkOp.getData(cronPath);
CronExpression cronExpression = null;
try {
cronExpression = new CronExpression(cronVal);
} catch (ParseException e) {
log.error(e.getMessage(), e);
return null;
}
String timeZoneStr = getTimeZone(jobName, curatorFrameworkOp);
TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr);
cronExpression.setTimeZone(timeZone);
Date nextFireTime = cronExpression.getTimeAfter(new Date(nextFireTimeAfterThis));
String pausePeriodDatePath = JobNodePath.getConfigNodePath(jobName, "pausePeriodDate");
String pausePeriodDate = curatorFrameworkOp.getData(pausePeriodDatePath);
String pausePeriodTimePath = JobNodePath.getConfigNodePath(jobName, "pausePeriodTime");
String pausePeriodTime = curatorFrameworkOp.getData(pausePeriodTimePath);
while (nextFireTime != null && isInPausePeriod(nextFireTime, pausePeriodDate, pausePeriodTime, timeZone)) {
nextFireTime = cronExpression.getTimeAfter(nextFireTime);
}
if (null == nextFireTime) {
return null;
}
return nextFireTime.getTime();
}
use of com.vip.saturn.job.console.utils.CronExpression in project Saturn by vipshop.
the class JobOperationServiceImpl method getEnabledReport.
/**
* 对于定时作业,根据cron和INTERVAL_TIME_OF_ENABLED_REPORT来计算是否需要上报状态 see #286
*/
private boolean getEnabledReport(String jobType, String cron, String timeZone) {
if (!jobType.equals(JobBriefInfo.JobType.JAVA_JOB.name()) && !jobType.equals(JobBriefInfo.JobType.SHELL_JOB.name())) {
return false;
}
try {
Integer intervalTimeConfigured = systemConfigService.getIntegerValue(SystemConfigProperties.INTERVAL_TIME_OF_ENABLED_REPORT, DEFAULT_INTERVAL_TIME_OF_ENABLED_REPORT);
if (intervalTimeConfigured == null) {
log.debug("System config INTERVAL_TIME_OF_ENABLED_REPORT is null");
intervalTimeConfigured = DEFAULT_INTERVAL_TIME_OF_ENABLED_REPORT;
}
CronExpression cronExpression = new CronExpression(cron);
cronExpression.setTimeZone(TimeZone.getTimeZone(timeZone));
// 基于当前时间的下次调度时间
Date lastNextTime = cronExpression.getNextValidTimeAfter(new Date());
if (lastNextTime != null) {
for (int i = 0; i < CALCULATE_NEXT_FIRE_TIME_MAX_TIMES; i++) {
Date nextTime = cronExpression.getNextValidTimeAfter(lastNextTime);
// no next fire time
if (nextTime == null) {
return true;
}
long interval = nextTime.getTime() - lastNextTime.getTime();
if (interval < intervalTimeConfigured * 1000L) {
return false;
}
lastNextTime = nextTime;
}
}
} catch (ParseException e) {
log.warn(e.getMessage(), e);
}
return true;
}
use of com.vip.saturn.job.console.utils.CronExpression in project Saturn by vipshop.
the class JobController method checkAndForecastCron.
@RequestMapping(value = "checkAndForecastCron", method = RequestMethod.POST)
public RequestResult checkAndForecastCron(final String timeZone, final String cron, HttpServletRequest request) {
RequestResult result = new RequestResult();
if (timeZone == null || timeZone.trim().isEmpty()) {
result.setSuccess(false);
result.setMessage("timeZone cannot be null or empty");
return result;
}
if (cron == null || cron.trim().isEmpty()) {
result.setSuccess(false);
result.setMessage("cron cannot be null or empty");
return result;
}
String timeZoneTrim = timeZone.trim();
String cronTrim = cron.trim();
if (!SaturnConstants.TIME_ZONE_IDS.contains(timeZoneTrim)) {
result.setSuccess(false);
result.setMessage("timeZone is not available");
return result;
}
try {
TimeZone tz = TimeZone.getTimeZone(timeZoneTrim);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(tz);
CronExpression cronExpression = new CronExpression(cronTrim);
cronExpression.setTimeZone(tz);
Map<String, String> obj = new HashMap<>();
obj.put("timeZone", timeZoneTrim);
StringBuilder sb = new StringBuilder(100);
Date now = new Date();
for (int i = 0; i < 10; i++) {
Date next = cronExpression.getNextValidTimeAfter(now);
if (next != null) {
sb.append(dateFormat.format(next)).append("<br>");
now = next;
}
}
if (sb.length() == 0) {
obj.put("times", "Cron maybe describe the past time, the job will never be executed");
} else {
if (sb.toString().split("<br>") != null && sb.toString().split("<br>").length >= 10) {
sb.append("......");
}
obj.put("times", sb.toString());
}
result.setSuccess(true);
result.setObj(obj);
} catch (ParseException e) {
result.setSuccess(false);
result.setMessage(e.toString());
return result;
}
return result;
}
Aggregations