Search in sources :

Example 1 with JobConfig

use of com.vip.saturn.job.console.domain.JobConfig in project Saturn by vipshop.

the class ExecutorController method batchAddJobs.

@RequestMapping(value = "batchAddJobs", method = RequestMethod.POST)
public RequestResult batchAddJobs(MultipartHttpServletRequest request) {
    RequestResult result = new RequestResult();
    int successCount = 0;
    int failCount = 0;
    String failMessage = "";
    try {
        Iterator<String> fileNames = request.getFileNames();
        MultipartFile file = null;
        while (fileNames.hasNext()) {
            if (file != null) {
                result.setSuccess(false);
                result.setMessage("仅支持单文件导入");
                return result;
            }
            file = request.getFile(fileNames.next());
        }
        if (file == null) {
            result.setSuccess(false);
            result.setMessage("请选择导入的文件");
            return result;
        }
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || !originalFilename.endsWith(".xls")) {
            result.setSuccess(false);
            result.setMessage("仅支持.xls文件导入");
            return result;
        }
        Workbook workbook = Workbook.getWorkbook(file.getInputStream());
        Sheet[] sheets = workbook.getSheets();
        List<JobConfig> jobConfigList = new ArrayList<>();
        // 先获取数据并检测内容格式的正确性
        for (int i = 0; i < sheets.length; i++) {
            Sheet sheet = sheets[i];
            int rows = sheet.getRows();
            for (int row = 1; row < rows; row++) {
                Cell[] rowCells = sheet.getRow(row);
                // 如果这一行的表格全为空,则跳过这一行。
                if (!isBlankRow(rowCells)) {
                    jobConfigList.add(convertJobConfig(i + 1, row + 1, rowCells));
                }
            }
        }
        int maxJobNum = executorService.getMaxJobNum();
        if (executorService.jobIncExceeds(maxJobNum, jobConfigList.size())) {
            String errorMsg = String.format("总作业数超过最大限制(%d),导入失败", maxJobNum);
            result.setSuccess(false);
            result.setMessage(errorMsg);
            return result;
        }
        // 再进行添加
        for (JobConfig jobConfig : jobConfigList) {
            RequestResult addJobResult = executorService.addJobs(jobConfig);
            if (addJobResult.isSuccess()) {
                successCount++;
            } else {
                failCount++;
                failMessage += " [" + addJobResult.getMessage() + "]";
            }
        }
    } catch (SaturnJobConsoleException e) {
        result.setSuccess(false);
        result.setMessage("导入失败," + e.getMessage());
        return result;
    } catch (Exception e) {
        result.setSuccess(false);
        result.setMessage("导入失败,错误信息:" + e.toString());
        return result;
    }
    result.setSuccess(true);
    if (failCount > 0) {
        result.setMessage("共导入" + successCount + "个作业,忽略" + failCount + "个。错误信息:" + failMessage);
    } else {
        result.setMessage("共导入" + successCount + "个作业,忽略0个");
    }
    return result;
}
Also used : Workbook(jxl.Workbook) JobConfig(com.vip.saturn.job.console.domain.JobConfig) WriteException(jxl.write.WriteException) ParseException(java.text.ParseException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) RequestResult(com.vip.saturn.job.console.domain.RequestResult) MultipartFile(org.springframework.web.multipart.MultipartFile) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) Sheet(jxl.Sheet) Cell(jxl.Cell) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with JobConfig

use of com.vip.saturn.job.console.domain.JobConfig in project Saturn by vipshop.

the class JobOperationRestApiController method constructJobConfig.

private JobConfig constructJobConfig(String namespace, Map<String, Object> reqParams) throws SaturnJobConsoleException {
    JobConfig jobConfig = new JobConfig();
    checkMissingParameter("namespace", namespace);
    jobConfig.setNamespace(namespace);
    if (!reqParams.containsKey("jobConfig")) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(INVALID_REQUEST_MSG, "jobConfig", "cannot be blank"));
    }
    Map<String, Object> configParams = (Map<String, Object>) reqParams.get("jobConfig");
    jobConfig.setJobName(checkAndGetParametersValueAsString(reqParams, "jobName", true));
    jobConfig.setDescription(checkAndGetParametersValueAsString(reqParams, "description", false));
    jobConfig.setChannelName(checkAndGetParametersValueAsString(configParams, "channelName", false));
    jobConfig.setCron(checkAndGetParametersValueAsString(configParams, "cron", false));
    jobConfig.setJobClass(checkAndGetParametersValueAsString(configParams, "jobClass", false));
    jobConfig.setJobParameter(checkAndGetParametersValueAsString(configParams, "jobParameter", false));
    String jobType = checkAndGetParametersValueAsString(configParams, "jobType", true);
    if (JobBriefInfo.JobType.UNKOWN_JOB.equals(JobBriefInfo.JobType.getJobType(jobType))) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(INVALID_REQUEST_MSG, "jobType", "is malformed"));
    }
    jobConfig.setJobType(jobType);
    jobConfig.setLoadLevel(checkAndGetParametersValueAsInteger(configParams, "loadLevel", false));
    jobConfig.setLocalMode(checkAndGetParametersValueAsBoolean(configParams, "localMode", false));
    jobConfig.setPausePeriodDate(checkAndGetParametersValueAsString(configParams, "pausePeriodDate", false));
    jobConfig.setPausePeriodTime(checkAndGetParametersValueAsString(configParams, "pausePeriodTime", false));
    jobConfig.setPreferList(checkAndGetParametersValueAsString(configParams, "preferList", false));
    jobConfig.setQueueName(checkAndGetParametersValueAsString(configParams, "queueName", false));
    jobConfig.setShardingItemParameters(checkAndGetParametersValueAsString(configParams, "shardingItemParameters", true));
    jobConfig.setShardingTotalCount(checkAndGetParametersValueAsInteger(configParams, "shardingTotalCount", true));
    jobConfig.setTimeout4AlarmSeconds(checkAndGetParametersValueAsInteger(configParams, "timeout4AlarmSeconds", false));
    jobConfig.setTimeoutSeconds(checkAndGetParametersValueAsInteger(configParams, "timeout4Seconds", false));
    jobConfig.setUseDispreferList(checkAndGetParametersValueAsBoolean(configParams, "useDispreferList", false));
    jobConfig.setUseSerial(checkAndGetParametersValueAsBoolean(configParams, "useSerial", false));
    jobConfig.setJobDegree(checkAndGetParametersValueAsInteger(configParams, "jobDegree", false));
    jobConfig.setDependencies(checkAndGetParametersValueAsString(configParams, "dependencies", false));
    jobConfig.setTimeZone(checkAndGetParametersValueAsString(configParams, "timeZone", false));
    jobConfig.setTimeoutSeconds(checkAndGetParametersValueAsInteger(configParams, "timeoutSeconds", false));
    jobConfig.setProcessCountIntervalSeconds(checkAndGetParametersValueAsInteger(configParams, "processCountIntervalSeconds", false));
    jobConfig.setGroups(checkAndGetParametersValueAsString(configParams, "groups", false));
    jobConfig.setShowNormalLog(checkAndGetParametersValueAsBoolean(configParams, "showNormalLog", false));
    return jobConfig;
}
Also used : SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) Map(java.util.Map) JobConfig(com.vip.saturn.job.console.domain.JobConfig)

Example 3 with JobConfig

use of com.vip.saturn.job.console.domain.JobConfig in project Saturn by vipshop.

the class JobOperationRestApiController method create.

@RequestMapping(value = "/{namespace}/jobs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Object> create(@PathVariable("namespace") String namespace, @RequestBody Map<String, Object> reqParams) throws SaturnJobConsoleException {
    try {
        JobConfig jobConfig = constructJobConfig(namespace, reqParams);
        restApiService.createJob(namespace, jobConfig);
        return new ResponseEntity<>(HttpStatus.CREATED);
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Exception e) {
        throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) JobConfig(com.vip.saturn.job.console.domain.JobConfig) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with JobConfig

use of com.vip.saturn.job.console.domain.JobConfig in project Saturn by vipshop.

the class DownStreamIT method test.

@Test
public void test() throws Exception {
    startOneNewExecutorList();
    JobB.count = 0;
    // add downStream firstly
    JobConfig jobB = new JobConfig();
    jobB.setJobName("downStreamITJobB");
    jobB.setJobType(JobType.PASSIVE_JAVA_JOB.toString());
    jobB.setJobClass(JobB.class.getCanonicalName());
    jobB.setShardingTotalCount(1);
    jobB.setShardingItemParameters("0=0");
    addJob(jobB);
    Thread.sleep(1000);
    JobConfig jobA = new JobConfig();
    jobA.setJobName("downStreamITJobA");
    jobA.setCron("9 9 9 9 9 ? 2099");
    jobA.setJobType(JobType.JAVA_JOB.toString());
    jobA.setJobClass(JobA.class.getCanonicalName());
    jobA.setShardingTotalCount(1);
    jobA.setShardingItemParameters("0=0");
    jobA.setDownStream(jobB.getJobName());
    addJob(jobA);
    Thread.sleep(1000);
    enableJob(jobA.getJobName());
    enableJob(jobB.getJobName());
    Thread.sleep(1000);
    runAtOnce(jobA.getJobName());
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return JobB.count == 1;
        }
    }, 10);
    disableJob(jobA.getJobName());
    disableJob(jobB.getJobName());
    Thread.sleep(1000);
    UpdateJobConfigVo updateJobConfigVo = new UpdateJobConfigVo();
    updateJobConfigVo.setJobName(jobA.getJobName());
    updateJobConfigVo.setDownStream("");
    updateJob(updateJobConfigVo);
    Thread.sleep(1000);
    removeJob(jobA.getJobName());
    removeJob(jobB.getJobName());
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) JobB(com.vip.saturn.it.job.downStream.JobB) JobA(com.vip.saturn.it.job.downStream.JobA) UpdateJobConfigVo(com.vip.saturn.job.console.vo.UpdateJobConfigVo) JobConfig(com.vip.saturn.job.console.domain.JobConfig) Test(org.junit.Test)

Example 5 with JobConfig

use of com.vip.saturn.job.console.domain.JobConfig in project Saturn by vipshop.

the class ExecutionIT method test_B_executionStatus.

@Test
public void test_B_executionStatus() throws Exception {
    startExecutorList(1);
    final String jobName = "test_B_executionStatus";
    final int shardCount = 1;
    for (int i = 0; i < shardCount; i++) {
        String key = jobName + "_" + i;
        LongtimeJavaJob.JobStatus status = new LongtimeJavaJob.JobStatus();
        status.runningCount = 0;
        status.sleepSeconds = 20;
        status.finished = false;
        status.timeout = false;
        LongtimeJavaJob.statusMap.put(key, status);
    }
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    // 间隔大于5分钟,会上报状态,failover
    jobConfig.setCron("0 */6 * * * ? 2099");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(LongtimeJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setTimeoutSeconds(0);
    jobConfig.setShardingItemParameters("0=0");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(2000);
    assertThat(zkGetJobNode(jobName, "execution/0/running")).isEqualTo("executorName0");
    zkRemoveJobNode(jobName, "execution/0/running");
    Thread.sleep(1000);
    // itself take over 0 item
    assertThat(zkGetJobNode(jobName, "execution/0/failover")).isEqualTo("executorName0");
    // wait the last finish, until the failover lifecycle begin
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return regCenter.isExisted(JobNodePath.getNodeFullPath(jobName, "execution/0/running"));
        }
    }, 30);
    assertThat(zkGetJobNode(jobName, "execution/0/running")).isEqualTo("executorName0");
    // wait the failover lifecycle finish
    waitForFinish(new FinishCheck() {

        @Override
        public boolean isOk() {
            return regCenter.isExisted(JobNodePath.getNodeFullPath(jobName, "execution/0/completed"));
        }
    }, 30);
    assertThat(zkGetJobNode(jobName, "execution/0/completed")).isEqualTo("executorName0");
    disableJob(jobName);
    Thread.sleep(1000);
    removeJob(jobName);
    Thread.sleep(2000);
    LongtimeJavaJob.statusMap.clear();
    stopExecutorListGracefully();
}
Also used : FinishCheck(com.vip.saturn.it.base.FinishCheck) JobConfig(com.vip.saturn.job.console.domain.JobConfig) LongtimeJavaJob(com.vip.saturn.it.job.LongtimeJavaJob) Test(org.junit.Test)

Aggregations

JobConfig (com.vip.saturn.job.console.domain.JobConfig)89 FinishCheck (com.vip.saturn.it.base.FinishCheck)45 SimpleJavaJob (com.vip.saturn.it.job.SimpleJavaJob)37 Test (org.junit.Test)35 Main (com.vip.saturn.job.executor.Main)31 LongtimeJavaJob (com.vip.saturn.it.job.LongtimeJavaJob)12 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)10 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)9 List (java.util.List)6 Collection (java.util.Collection)5 Map (java.util.Map)4 InitByGroupsJob (com.vip.saturn.it.job.InitByGroupsJob)3 CuratorRepository (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository)3 ArrayList (java.util.ArrayList)3 ResponseEntity (org.springframework.http.ResponseEntity)3 Audit (com.vip.saturn.job.console.aop.annotation.Audit)2 JobDiffInfo (com.vip.saturn.job.console.domain.JobDiffInfo)2 RestApiJobConfig (com.vip.saturn.job.console.domain.RestApiJobConfig)2 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)2 ParseException (java.text.ParseException)2