in spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-schedulerx/src/main/java/com/alibaba/cloud/scheduling/schedulerx/service/JobSyncService.java [340:422]
private void updateJob(DefaultAcsClient client, JobConfigInfo jobConfigInfo, JobProperty jobProperty, String namespaceSource) throws Exception {
String executeMode = jobProperty.getJobModel();
if (SchedulerxConstants.JOB_MODEL_MAPREDUCE_ALIAS.equals(jobProperty.getJobModel())) {
executeMode = ExecuteMode.BATCH.getKey();
}
int timeType;
String timeExpression = null;
if (StringUtils.isNotEmpty(jobProperty.getCron()) && StringUtils.isNotEmpty(jobProperty.getOneTime())) {
throw new IOException("cron and oneTime shouldn't set together");
}
if (StringUtils.isNotEmpty(jobProperty.getCron())) {
CronExpression cronExpression = new CronExpression(jobProperty.getCron());
Date now = new Date();
Date nextData = cronExpression.getTimeAfter(now);
Date next2Data = cronExpression.getTimeAfter(nextData);
if (nextData != null && next2Data != null) {
long interval = TimeUnit.MILLISECONDS.toSeconds((next2Data.getTime() - nextData.getTime()));
if (interval < SchedulerxConstants.SECOND_DELAY_MAX_VALUE) {
timeType = TimeType.SECOND_DELAY.getValue();
timeExpression = String.valueOf(interval < SchedulerxConstants.SECOND_DELAY_MIN_VALUE ?
SchedulerxConstants.SECOND_DELAY_MIN_VALUE : interval);
}
else {
timeType = TimeType.CRON.getValue();
timeExpression = jobProperty.getCron();
}
}
else {
timeType = TimeType.CRON.getValue();
timeExpression = jobProperty.getCron();
}
}
else if (StringUtils.isNotEmpty(jobProperty.getOneTime())) {
timeType = TimeType.ONE_TIME.getValue();
timeExpression = jobProperty.getOneTime();
}
else {
timeType = TimeType.API.getValue();
}
if (!jobConfigInfo.getDescription().equals(jobProperty.getDescription())
|| !jobConfigInfo.getClassName().equals(jobProperty.getClassName())
|| !jobConfigInfo.getParameters().equals(jobProperty.getJobParameter())
|| !jobConfigInfo.getExecuteMode().equals(executeMode)
|| jobConfigInfo.getTimeConfig().getTimeType() != timeType
|| !jobConfigInfo.getTimeConfig().getTimeExpression().equals(timeExpression)) {
UpdateJobRequest request = new UpdateJobRequest();
request.setNamespace(properties.getNamespace());
request.setNamespaceSource(namespaceSource);
request.setGroupId(properties.getGroupId());
request.setJobId(jobConfigInfo.getJobId());
request.setName(jobConfigInfo.getName());
request.setParameters(jobProperty.getJobParameter());
//java任务
if (jobProperty.getJobType().equals(JobType.JAVA.getKey())) {
request.setClassName(jobProperty.getClassName());
}
request.setExecuteMode(executeMode);
if (StringUtils.isNotEmpty(jobProperty.getDescription())) {
request.setDescription(jobProperty.getDescription());
}
request.setTimeType(timeType);
request.setTimeExpression(timeExpression);
request.setTimeoutEnable(true);
request.setTimeoutKillEnable(true);
request.setSendChannel(SchedulerxConstants.JOB_ALARM_CHANNEL_DEFAULT);
request.setFailEnable(true);
request.setTimeout(SchedulerxConstants.JOB_TIMEOUT_DEFAULT);
request.setMaxAttempt(SchedulerxConstants.JOB_RETRY_COUNT_DEFAULT);
request.setAttemptInterval(SchedulerxConstants.JOB_RETRY_INTERVAL_DEFAULT);
UpdateJobResponse response = client.getAcsResponse(request);
if (response.getSuccess()) {
logger.info("update schedulerx job successfully, jobId={}, jobName={}", jobConfigInfo.getJobId(), jobConfigInfo.getName());
}
else {
throw new IOException("update schedulerx job failed, jobName=" + jobConfigInfo.getName() + ", message=" + response.getMessage());
}
}
}