Search in sources :

Example 41 with JobConfig

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

the class JobOperationRestApiControllerTest method convert2JobConfig.

private JobConfig convert2JobConfig(String namespace, JobEntity jobEntity) {
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobEntity.getJobName());
    jobConfig.setCron((String) jobEntity.getConfig("cron"));
    jobConfig.setJobType((String) jobEntity.getConfig("jobType"));
    jobConfig.setShardingTotalCount((Integer) jobEntity.getConfig("shardingTotalCount"));
    jobConfig.setShardingItemParameters((String) jobEntity.getConfig("shardingItemParameters"));
    jobConfig.setDescription(jobEntity.getDescription());
    jobConfig.setLocalMode(null);
    jobConfig.setUseSerial(null);
    return jobConfig;
}
Also used : RestApiJobConfig(com.vip.saturn.job.console.domain.RestApiJobConfig) JobConfig(com.vip.saturn.job.console.domain.JobConfig)

Example 42 with JobConfig

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

the class NamespaceServiceImplTest method testFailToImportJobs.

@Test
public void testFailToImportJobs() throws Exception {
    List<JobConfig> jobConfigs = new ArrayList<>();
    jobConfigs.add(new JobConfig());
    when(jobService.getUnSystemJobs("saturn.vip.vip.com")).thenReturn(jobConfigs);
    doThrow(new RuntimeException()).when(jobService).addJob(anyString(), any(JobConfig.class), anyString());
    Exception exception = null;
    try {
        namespaceService.importJobsFromNamespaceToNamespace("saturn.vip.vip.com", "saturn.vip.vip.com_tt", "ray.leung");
    } catch (Exception e) {
        exception = e;
    }
    Assert.assertNotNull(exception);
}
Also used : ArrayList(java.util.ArrayList) JobConfig(com.vip.saturn.job.console.domain.JobConfig) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) Test(org.junit.Test)

Example 43 with JobConfig

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

the class NamespaceServiceImpl method importJobsFromNamespaceToNamespace.

@Override
public Map<String, List> importJobsFromNamespaceToNamespace(String srcNamespace, String destNamespace, String createdBy) throws SaturnJobConsoleException {
    if (StringUtils.isBlank(srcNamespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), "srcNamespace should not be null");
    }
    if (StringUtils.isBlank(destNamespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), "destNamespace should not be null");
    }
    if (StringUtils.equals(srcNamespace, destNamespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), "srcNamespace and destNamespace should be difference");
    }
    try {
        List<String> successfullyImportedJobs = new ArrayList<>();
        List<String> failedJobs = new ArrayList<>();
        Map<String, List> result = new HashMap<>();
        result.put("success", successfullyImportedJobs);
        result.put("fail", failedJobs);
        List<JobConfig> jobConfigs = jobService.getUnSystemJobs(srcNamespace);
        List<JobConfig> jobConfigUpdatedList = new ArrayList<>();
        for (JobConfig jobConfig : jobConfigs) {
            String jobName = jobConfig.getJobName();
            try {
                // 如果存在上下游关联关系,直接导入会检验不通过;需要先解除关联关系,创建成功后再更新关联关系
                JobConfig jobConfigUpdated = null;
                if (StringUtils.isBlank(jobConfig.getUpStream()) || StringUtils.isBlank(jobConfig.getDownStream())) {
                    jobConfigUpdated = new JobConfig();
                    jobConfigUpdated.setJobName(jobName);
                    jobConfigUpdated.setUpStream(jobConfig.getUpStream());
                    jobConfigUpdated.setDownStream(jobConfig.getDownStream());
                    jobConfig.setUpStream(null);
                    jobConfig.setDownStream(null);
                }
                jobService.addJob(destNamespace, jobConfig, createdBy);
                if (jobConfigUpdated != null) {
                    jobConfigUpdatedList.add(jobConfigUpdated);
                }
                successfullyImportedJobs.add(jobName);
            } catch (SaturnJobConsoleException e) {
                log.warn("fail to import job {} from {} to {}", jobName, srcNamespace, destNamespace, e);
                failedJobs.add(jobName);
            }
        }
        for (JobConfig jobConfig : jobConfigUpdatedList) {
            String jobName = jobConfig.getJobName();
            try {
                jobService.updateJobConfig(destNamespace, jobConfig, createdBy);
            } catch (SaturnJobConsoleException e) {
                log.warn("fail to update job upStream or downStream, namespace is {} jobName is {}", destNamespace, jobName, e);
                failedJobs.add(jobName);
                successfullyImportedJobs.remove(jobName);
            }
        }
        return result;
    } catch (SaturnJobConsoleException e) {
        log.warn("import jobs from {} to {} fail", srcNamespace, destNamespace, e);
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) JobConfig(com.vip.saturn.job.console.domain.JobConfig)

Example 44 with JobConfig

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

the class MarathonServiceImpl method addContainerScaleJob.

@Override
public void addContainerScaleJob(String namespace, String taskId, String jobDesc, int instances, String timeZone, String cron) throws SaturnJobConsoleException {
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
    String jobName = SaturnConstants.SYSTEM_SCALE_JOB_PREFEX + System.currentTimeMillis();
    ContainerToken containerToken = getContainerToken(namespace);
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setDescription(jobDesc);
    jobConfig.setTimeZone(timeZone);
    jobConfig.setCron(cron);
    jobConfig.setJobMode(JobMode.system_scale);
    jobConfig.setJobType(JobType.SHELL_JOB.name());
    jobConfig.setPreferList("@" + taskId);
    jobConfig.setShardingTotalCount(1);
    jobConfig.setShardingItemParameters(getContainerScaleJobShardingItemParameters(containerToken, taskId, instances));
    jobConfig.setUseDispreferList(false);
    jobConfig.setTimeout4AlarmSeconds(30);
    jobConfig.setTimeoutSeconds(30);
    jobConfig.setJobParameter("");
    jobConfig.setQueueName("");
    jobConfig.setChannelName("");
    jobConfig.setPausePeriodDate("");
    jobConfig.setPausePeriodTime("");
    jobService.addJob(namespace, jobConfig, "");
    ContainerScaleJobConfig containerScaleJobConfig = new ContainerScaleJobConfig();
    containerScaleJobConfig.setJobName(jobName);
    containerScaleJobConfig.setJobDesc(jobDesc);
    containerScaleJobConfig.setInstances(instances);
    containerScaleJobConfig.setTimeZone(timeZone);
    containerScaleJobConfig.setCron(cron);
    try {
        String containerScaleJobStr = JSON.toJSONString(containerScaleJobConfig);
        String dcosTaskScaleJobNodePath = ContainerNodePath.getDcosTaskScaleJobNodePath(taskId, jobName);
        curatorFrameworkOp.fillJobNodeIfNotExist(dcosTaskScaleJobNodePath, containerScaleJobStr);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e.getMessage(), e);
    }
}
Also used : CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) JobConfig(com.vip.saturn.job.console.domain.JobConfig) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 45 with JobConfig

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

the class ZkDBDiffServiceImpl method diffByNamespace.

@Override
public List<JobDiffInfo> diffByNamespace(String namespace) throws SaturnJobConsoleException {
    long startTime = System.currentTimeMillis();
    List<JobDiffInfo> jobDiffInfos = Lists.newArrayList();
    CuratorRepository.CuratorFrameworkOp zkClient;
    try {
        List<JobConfig4DB> dbJobConfigList = currentJobConfigService.findConfigsByNamespace(namespace);
        if (dbJobConfigList == null || dbJobConfigList.isEmpty()) {
            return jobDiffInfos;
        }
        zkClient = initCuratorClient(namespace);
        if (zkClient == null) {
            return jobDiffInfos;
        }
        Set<String> jobNamesInDb = getAllJobNames(dbJobConfigList);
        for (JobConfig4DB dbJobConfig : dbJobConfigList) {
            String jobName = dbJobConfig.getJobName();
            log.info("start to diff job:{}@{}", jobName, namespace);
            if (!checkJobIsExsitInZk(jobName, zkClient)) {
                jobDiffInfos.add(new JobDiffInfo(namespace, jobName, JobDiffInfo.DiffType.DB_ONLY, Lists.<JobDiffInfo.ConfigDiffInfo>newArrayList()));
                continue;
            }
            JobConfig jobConfigFromZK = jobService.getJobConfigFromZK(namespace, jobName);
            JobDiffInfo jobDiffInfo = diff(namespace, dbJobConfig, jobConfigFromZK, false);
            if (jobDiffInfo != null) {
                jobDiffInfos.add(jobDiffInfo);
            }
        }
        List<JobDiffInfo> jobsInZkOnly = getJobNamesWhichInZKOnly(namespace, jobNamesInDb);
        if (jobsInZkOnly != null && !jobsInZkOnly.isEmpty()) {
            jobDiffInfos.addAll(jobsInZkOnly);
        }
    } catch (SaturnJobConsoleException e) {
        log.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        log.error("exception throws during diff by namespace [{}]", namespace, e);
        throw new SaturnJobConsoleException(e);
    } finally {
        log.info("Finish diff namespace:{} which cost {}ms", namespace, System.currentTimeMillis() - startTime);
    }
    return jobDiffInfos;
}
Also used : JobDiffInfo(com.vip.saturn.job.console.domain.JobDiffInfo) JobConfig4DB(com.vip.saturn.job.console.mybatis.entity.JobConfig4DB) JobConfig(com.vip.saturn.job.console.domain.JobConfig) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

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