Search in sources :

Example 1 with Jobs

use of com.chinaunicom.rundocker.bean.returnparm.Jobs in project mesosFramework by zhizuqiu.

the class SchedulerService method getJobsFromFile.

/**
 * 从file中获取任务,内存中如果有status记录,还是以内存为准,不存在再new一个
 */
public static Jobs getJobsFromFile() {
    Long logTime = System.currentTimeMillis();
    logger.info("[" + logTime + "]" + "---------------------");
    logger.info("[" + logTime + "]" + "start getJobsFromFile");
    if (AppDataStore.getConfig().getStoreFilePath() == null) {
        logger.info("[" + logTime + "]" + "getStoreFilePath is null , skip");
        return new Jobs();
    }
    Jobs jobs;
    try {
        byte[] data = Files.readAllBytes(Paths.get(AppDataStore.getConfig().getStoreFilePath()));
        String fileData = new String(data, "UTF-8");
        jobs = new Gson().fromJson(fileData, Jobs.class);
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "getJobsFromFile->Exception->" + e.getMessage());
        return new Jobs();
    }
    return jobs;
}
Also used : Jobs(com.chinaunicom.rundocker.bean.returnparm.Jobs) Gson(com.google.gson.Gson) ByteString(com.google.protobuf.ByteString) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Example 2 with Jobs

use of com.chinaunicom.rundocker.bean.returnparm.Jobs in project mesosFramework by zhizuqiu.

the class SchedulerService method getJobsFromMustache.

/**
 * 从模板中获取jobs,并用环境变量填充
 */
public static Jobs getJobsFromMustache() {
    Long logTime = System.currentTimeMillis();
    if (AppDataStore.getConfig().getStoreMustachePath() == null) {
        logger.info("[" + logTime + "]" + "getStoreMustachePath is null , skip");
        return null;
    }
    String mustachePath = AppDataStore.getConfig().getStoreMustachePath();
    Map<String, String> envs = System.getenv();
    Jobs jobs;
    String json = Tools.initMustache(envs, mustachePath);
    if (json == null) {
        logger.info("initMustache json is null");
        return null;
    } else {
        logger.info(json);
    }
    try {
        jobs = new Gson().fromJson(json, Jobs.class);
    } catch (JsonSyntaxException e) {
        logger.error("fromJson  exception->" + e.getMessage());
        return null;
    }
    return jobs;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Jobs(com.chinaunicom.rundocker.bean.returnparm.Jobs) Gson(com.google.gson.Gson) ByteString(com.google.protobuf.ByteString)

Example 3 with Jobs

use of com.chinaunicom.rundocker.bean.returnparm.Jobs in project mesosFramework by zhizuqiu.

the class RestHandler method postJobs.

/**
 * 添加任务,如果任务存在,会覆盖
 *
 * @param jsonParam 请求参数
 * @param response  请求响应
 * @return 返回参数
 */
@HttpMap(path = "/jobs", paramType = HttpMap.ParamType.JSON, method = HttpMap.Method.POST)
public Jobs postJobs(String jsonParam, DefaultFullHttpResponse response) {
    Long logTime = System.currentTimeMillis();
    Jobs jobs = new Gson().fromJson(jsonParam, Jobs.class);
    List<DockerJob> unSuccessJobs = new ArrayList<>();
    for (DockerJob dockerJob : jobs.getApps()) {
        String id = dockerJob.getId();
        if (id == null) {
            unSuccessJobs.add(dockerJob);
            continue;
        }
        // 设置id
        dockerJob.setId(Tools.getStringByJsonpathRecursion(id));
        // todo 检查配置是否正确
        String key = Tools.getStringByJsonpathRecursion(id.replaceAll("/", "."));
        DockerStatus dockerStatus;
        if (AppDataStore.statusGet(key) == null) {
            // 生成dockerStatus
            dockerStatus = DockerStatus.DockerStatusBuilder.newBuilder().defaultCreate(Tools.getStringByJsonpathRecursion(id)).setPersistenceStatusNew(dockerJob.hasPersistence()).builder();
        } else {
            dockerStatus = AppDataStore.statusGet(key);
            // 如果之前使用了持久卷,不能修改
            if (!dockerStatus.get_persistenceInfo().getPersistenceStatus().equals(DockerStatus.PersistenceStatus.NONE)) {
                unSuccessJobs.add(dockerJob);
                logger.info("[" + logTime + "]" + "pre task is use persistence volumes ,please delete task first");
                continue;
            }
            // 如果在running或running途中,认为失败
            if (dockerStatus.getStatus() == JobState.RUNNING || dockerStatus.getStatus() == JobState.STAGING) {
                unSuccessJobs.add(dockerJob);
                logger.info("[" + logTime + "]" + "task is " + dockerStatus.getStatus() + " ,please stop task first");
                continue;
            }
            dockerStatus.setSubmitted(false);
            dockerStatus.setTryCount(0);
        }
        AppDataStore.JobAndStatusTemp jobAndStatusTemp = new AppDataStore.JobAndStatusTemp(key, dockerJob, dockerStatus);
        // 原子的插入job和status,返回值用于etcd操作失败时的回退
        AppDataStore.JobAndStatusTemp jobAndStatusTempResult = AppDataStore.postJobsAndStatus(jobAndStatusTemp);
        // 原子的插入job和status到etcd
        Boolean setSuccess = etcdService.setJobAndStatusAtomic(logTime, key, jobAndStatusTemp);
        if (!setSuccess) {
            // 回滚
            AppDataStore.postJobsAndStatus(jobAndStatusTempResult);
            unSuccessJobs.add(dockerJob);
        }
    }
    jobs.getApps().removeAll(unSuccessJobs);
    // 判断是否还有任务未下发,是则请求offer
    if (!SchedulerService.checkAllAppsIsAccept()) {
        AppDataStore.getSchedulerDriver().reviveOffers();
        logger.info("reviveOffers");
    }
    return jobs;
}
Also used : Jobs(com.chinaunicom.rundocker.bean.returnparm.Jobs) ArrayList(java.util.ArrayList) DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus) Gson(com.google.gson.Gson) AppDataStore(com.chinaunicom.rundocker.store.AppDataStore) DockerJob(com.chinaunicom.rundocker.bean.DockerJob) HttpMap(com.zhizuqiu.nettyrestful.annotation.HttpMap)

Aggregations

Jobs (com.chinaunicom.rundocker.bean.returnparm.Jobs)3 Gson (com.google.gson.Gson)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 ByteString (com.google.protobuf.ByteString)2 DockerJob (com.chinaunicom.rundocker.bean.DockerJob)1 DockerStatus (com.chinaunicom.rundocker.bean.DockerStatus)1 AppDataStore (com.chinaunicom.rundocker.store.AppDataStore)1 HttpMap (com.zhizuqiu.nettyrestful.annotation.HttpMap)1 ArrayList (java.util.ArrayList)1