use of com.zhizuqiu.nettyrestful.annotation.HttpMap in project mesosFramework by zhizuqiu.
the class RestHandler method stopTask.
/**
* 停止任务,如url?id=/2048/1
*
* @param mapParam 请求参数
* @param response 请求响应
* @return 返回参数
*/
@HttpMap(path = "/tasks", paramType = HttpMap.ParamType.URL_DATA, method = HttpMap.Method.DELETE)
public Task stopTask(Map<String, String> mapParam, DefaultFullHttpResponse response) {
Task task = new Task();
if (mapParam == null || mapParam.isEmpty()) {
response.setStatus(BAD_REQUEST);
return task;
}
String id = mapParam.get("id");
if (id == null || id.trim().isEmpty()) {
response.setStatus(BAD_REQUEST);
return task;
}
Task taskTemp = stopOneTask(id);
if (taskTemp != null) {
task = taskTemp;
}
return task;
}
use of com.zhizuqiu.nettyrestful.annotation.HttpMap in project mesosFramework by zhizuqiu.
the class RestHandler method stopTasks.
/**
* 停止多个
*
* @param jsonParam 请求参数
* @param response 请求响应
* @return 返回参数
*/
@HttpMap(path = "/tasks", paramType = HttpMap.ParamType.JSON, method = HttpMap.Method.DELETE)
public List<Task> stopTasks(String jsonParam, DefaultFullHttpResponse response) {
List<Task> list = new ArrayList<>();
if (jsonParam == null) {
return list;
}
BaseIds ids = null;
try {
ids = new Gson().fromJson(jsonParam, BaseIds.class);
} catch (JsonSyntaxException e) {
response.setStatus(INTERNAL_SERVER_ERROR);
}
if (ids == null) {
return list;
}
for (String id : ids.getIds()) {
Task task = stopOneTask(id);
if (task != null) {
list.add(task);
}
}
return list;
}
use of com.zhizuqiu.nettyrestful.annotation.HttpMap in project mesosFramework by zhizuqiu.
the class RestHandler method deleteJobs.
/**
* 删除多个任务
*
* @param jsonParam 请求参数
* @param response 请求响应
* @return 返回参数
*/
@HttpMap(path = "/jobs", paramType = HttpMap.ParamType.JSON, method = HttpMap.Method.DELETE)
public List<Task> deleteJobs(String jsonParam, DefaultFullHttpResponse response) {
List<Task> list = new ArrayList<>();
if (jsonParam == null) {
return list;
}
BaseIds ids = null;
try {
ids = new Gson().fromJson(jsonParam, BaseIds.class);
} catch (JsonSyntaxException e) {
response.setStatus(INTERNAL_SERVER_ERROR);
}
if (ids == null) {
return list;
}
for (String id : ids.getIds()) {
Task task = deleteOneJob(id);
if (task != null) {
list.add(task);
}
}
return list;
}
use of com.zhizuqiu.nettyrestful.annotation.HttpMap in project mesosFramework by zhizuqiu.
the class RestHandler method startTasks.
/**
* 启动多个
*
* @param jsonParam 请求参数
* @param response 请求响应
* @return 返回参数
*/
@HttpMap(path = "/tasks", paramType = HttpMap.ParamType.JSON, method = HttpMap.Method.PUT)
public List<Task> startTasks(String jsonParam, DefaultFullHttpResponse response) {
List<Task> list = new ArrayList<>();
if (jsonParam == null) {
return list;
}
BaseIds ids = null;
try {
ids = new Gson().fromJson(jsonParam, BaseIds.class);
} catch (JsonSyntaxException e) {
response.setStatus(INTERNAL_SERVER_ERROR);
}
if (ids == null) {
return list;
}
for (String id : ids.getIds()) {
Task task = startOneTask(id);
if (task != null) {
list.add(task);
}
}
// 判断是否还有任务未下发,是则请求offer
if (!SchedulerService.checkAllAppsIsAccept()) {
AppDataStore.getSchedulerDriver().reviveOffers();
logger.info("reviveOffers");
}
return list;
}
use of com.zhizuqiu.nettyrestful.annotation.HttpMap 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;
}
Aggregations