Skip to content

Commit

Permalink
#458 增加更新作业的rest接口
Browse files Browse the repository at this point in the history
  • Loading branch information
leewcc committed Jul 17, 2018
1 parent fc9cb60 commit b65e017
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.vip.saturn.job.console.aop.annotation.Audit;
import com.vip.saturn.job.console.aop.annotation.AuditType;
import com.vip.saturn.job.console.controller.AbstractController;
import com.vip.saturn.job.console.domain.JobConfig;
import com.vip.saturn.job.console.domain.JobType;
import com.vip.saturn.job.console.domain.RestApiJobInfo;
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
import com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException;
import com.vip.saturn.job.console.service.RestApiService;

import com.vip.saturn.job.console.utils.SaturnBeanUtils;
import com.vip.saturn.job.console.vo.UpdateJobConfigVo;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
Expand Down Expand Up @@ -170,6 +171,21 @@ public ResponseEntity<Object> delete(@PathVariable("namespace") String namespace
}
}

@Audit(type = AuditType.REST)
@RequestMapping(value = "/{namespace}/jobs/{jobName}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Object> update(@PathVariable("namespace") String namespace, @PathVariable String jobName,
@RequestBody Map<String, Object> reqParams) throws SaturnJobConsoleException {
try {
UpdateJobConfigVo updateJobConfigVo = constructUpdateJobConfig(namespace, reqParams);
restApiService.updateJob(namespace, jobName, updateJobConfigVo);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (SaturnJobConsoleException e) {
throw e;
} catch (Exception e) {
throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e);
}
}

private JobConfig constructJobConfig(String namespace, Map<String, Object> reqParams) throws SaturnJobConsoleException {
checkMissingParameter("namespace", namespace);
if (!reqParams.containsKey("jobConfig")) {
Expand Down Expand Up @@ -239,4 +255,16 @@ private JobConfig constructJobConfig(String namespace, Map<String, Object> reqPa
return jobConfig;
}

private UpdateJobConfigVo constructUpdateJobConfig(String namespace, Map<String, Object> reqParams)
throws SaturnJobConsoleException {
JobConfig jobConfig = constructJobConfig(namespace, reqParams);
UpdateJobConfigVo updateJobConfigVo = new UpdateJobConfigVo();
SaturnBeanUtils.copyProperties(jobConfig, updateJobConfigVo);

Map<String, Object> configParams = (Map<String, Object>) reqParams.get("jobConfig");
updateJobConfigVo
.setOnlyUsePreferList(checkAndGetParametersValueAsBoolean(configParams, "onlyUseDispreferList", false));
return updateJobConfigVo;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.vip.saturn.job.console.service;

import com.vip.saturn.job.console.domain.JobConfig;
import com.vip.saturn.job.console.domain.NamespaceDomainInfo;
import com.vip.saturn.job.console.domain.RestApiJobInfo;
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
import com.vip.saturn.job.console.vo.UpdateJobConfigVo;
import com.vip.saturn.job.integrate.entity.AlarmInfo;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -96,4 +96,9 @@ void raiseAlarm(String namespace, String jobName, String executorName, Integer s
*/
void raiseExecutorRestartAlarm(String namespace, String executorName, AlarmInfo alarmInfo)
throws SaturnJobConsoleException;

/**
* update the job
*/
void updateJob(String namespace, String jobName, UpdateJobConfigVo jobConfig) throws SaturnJobConsoleException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.vip.saturn.job.console.service.helper.ReuseUtils;
import com.vip.saturn.job.console.utils.JobNodePath;
import com.vip.saturn.job.console.utils.SaturnConstants;
import com.vip.saturn.job.console.vo.UpdateJobConfigVo;
import com.vip.saturn.job.integrate.entity.AlarmInfo;
import com.vip.saturn.job.integrate.exception.ReportAlarmException;
import com.vip.saturn.job.integrate.service.ReportAlarmService;
Expand Down Expand Up @@ -633,4 +634,23 @@ private void checkUpdateStatusToDisableAllowed(long lastMtime) throws SaturnJobC
}
}

@Override
public void updateJob(final String namespace, final String jobName, final UpdateJobConfigVo updateJobConfigVo) throws SaturnJobConsoleException {
ReuseUtils.reuse(namespace, jobName, registryCenterService, curatorRepository,
new ReuseCallBackWithoutReturn() {
@Override
public void call(CuratorRepository.CuratorFrameworkOp curatorFrameworkOp)
throws SaturnJobConsoleException {
JobStatus js = jobService.getJobStatus(namespace, jobName);

if (!JobStatus.STOPPED.equals(js)) {
throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(),
String.format(JOB_STATUS_NOT_CORRECT_TEMPATE, JobStatus.STOPPED.name()));
}
jobService.updateJobConfig(namespace, updateJobConfigVo, "");
log.info("job {} update done", jobName);
}
});
}

}

0 comments on commit b65e017

Please sign in to comment.