use of io.jpom.model.data.OutGivingNodeProject in project Jpom by dromara.
the class OutGivingRun method startRun.
/**
* 开始异步执行分发任务
*
* @param id 分发id
* @param file 文件
* @param userModel 操作的用户
* @param unzip 解压
*/
public static void startRun(String id, File file, UserModel userModel, boolean unzip) {
OutGivingServer outGivingServer = SpringUtil.getBean(OutGivingServer.class);
OutGivingModel item = outGivingServer.getByKey(id);
Objects.requireNonNull(item, "不存在分发");
AfterOpt afterOpt = ObjectUtil.defaultIfNull(EnumUtil.likeValueOf(AfterOpt.class, item.getAfterOpt()), AfterOpt.No);
//
List<OutGivingNodeProject> outGivingNodeProjects = item.outGivingNodeProjectList();
// 开启线程
if (afterOpt == AfterOpt.Order_Restart || afterOpt == AfterOpt.Order_Must_Restart) {
ThreadUtil.execute(() -> {
// 截取睡眠时间
int sleepTime = ObjectUtil.defaultIfNull(item.getIntervalTime(), 10);
//
boolean cancel = false;
for (OutGivingNodeProject outGivingNodeProject : outGivingNodeProjects) {
if (cancel) {
String userId = userModel == null ? JpomApplication.SYSTEM_ID : userModel.getId();
OutGivingItemRun.updateStatus(null, id, outGivingNodeProject, OutGivingNodeProject.Status.Cancel, "前一个节点分发失败,取消分发", userId);
} else {
OutGivingItemRun outGivingRun = new OutGivingItemRun(item, outGivingNodeProject, file, userModel, unzip);
OutGivingNodeProject.Status status = outGivingRun.call();
if (status != OutGivingNodeProject.Status.Ok) {
if (afterOpt == AfterOpt.Order_Must_Restart) {
// 完整重启,不再继续剩余的节点项目
cancel = true;
}
}
// 休眠x秒 等待之前项目正常启动
ThreadUtil.sleep(sleepTime, TimeUnit.SECONDS);
}
}
});
} else if (afterOpt == AfterOpt.Restart || afterOpt == AfterOpt.No) {
outGivingNodeProjects.forEach(outGivingNodeProject -> ThreadUtil.execAsync(new OutGivingItemRun(item, outGivingNodeProject, file, userModel, unzip)));
} else {
//
throw new IllegalArgumentException("Not implemented " + afterOpt.getDesc());
}
}
use of io.jpom.model.data.OutGivingNodeProject in project Jpom by dromara.
the class OutGivingProjectController method getItemData.
@RequestMapping(value = "getItemData.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getItemData(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "id error") String id) {
HttpServletRequest request = getRequest();
String workspaceId = outGivingServer.getCheckUserWorkspace(request);
OutGivingModel outGivingServerItem = outGivingServer.getByKey(id, request);
Objects.requireNonNull(outGivingServerItem, "没有数据");
List<OutGivingNodeProject> outGivingNodeProjectList = outGivingServerItem.outGivingNodeProjectList();
List<JSONObject> collect = outGivingNodeProjectList.stream().map(outGivingNodeProject -> {
NodeModel nodeModel = nodeService.getByKey(outGivingNodeProject.getNodeId());
JSONObject jsonObject = new JSONObject();
jsonObject.put("nodeId", outGivingNodeProject.getNodeId());
jsonObject.put("projectId", outGivingNodeProject.getProjectId());
jsonObject.put("nodeName", nodeModel.getName());
jsonObject.put("id", BaseNodeModel.fullId(workspaceId, outGivingNodeProject.getNodeId(), outGivingNodeProject.getProjectId()));
// set projectStatus property
// NodeModel node = nodeService.getItem(outGivingNodeProject.getNodeId());
// Project Status: data.pid > 0 means running
JSONObject projectStatus = JsonMessage.toJson(200, "success");
if (nodeModel.isOpenStatus()) {
JSONObject projectInfo = null;
try {
projectInfo = projectInfoCacheService.getItem(nodeModel, outGivingNodeProject.getProjectId());
projectStatus = NodeForward.requestBySys(nodeModel, NodeUrl.Manage_GetProjectStatus, "id", outGivingNodeProject.getProjectId()).toJson();
} catch (Exception e) {
jsonObject.put("errorMsg", "error " + e.getMessage());
}
if (projectInfo != null) {
jsonObject.put("projectName", projectInfo.getString("name"));
}
} else {
jsonObject.put("errorMsg", "节点未启用");
}
JSONObject data = projectStatus.getJSONObject("data");
if (data != null && data.getInteger("pId") != null) {
jsonObject.put("projectStatus", data.getIntValue("pId") > 0);
} else {
jsonObject.put("projectStatus", false);
}
jsonObject.put("outGivingStatus", outGivingNodeProject.getStatusMsg());
jsonObject.put("outGivingResult", outGivingNodeProject.getResult());
jsonObject.put("lastTime", outGivingNodeProject.getLastOutGivingTime());
return jsonObject;
}).collect(Collectors.toList());
return JsonMessage.getString(200, "", collect);
}
use of io.jpom.model.data.OutGivingNodeProject in project Jpom by dromara.
the class OutGivingController method doData.
private void doData(OutGivingModel outGivingModel) {
outGivingModel.setName(getParameter("name"));
Assert.hasText(outGivingModel.getName(), "分发名称不能为空");
HttpServletRequest request = getRequest();
List<OutGivingModel> outGivingModels = outGivingServer.list();
//
Map<String, String> paramMap = ServletUtil.getParamMap(request);
List<OutGivingNodeProject> outGivingNodeProjects = paramMap.entrySet().stream().filter(stringStringEntry -> StrUtil.startWith(stringStringEntry.getKey(), "node_")).map(stringStringEntry -> {
int lastIndexOf = StrUtil.lastIndexOfIgnoreCase(stringStringEntry.getKey(), StrUtil.UNDERLINE);
int indexOf = StrUtil.indexOfIgnoreCase(stringStringEntry.getKey(), StrUtil.UNDERLINE) + 1;
String nodeId = StrUtil.sub(stringStringEntry.getKey(), indexOf, lastIndexOf);
//
String nodeIdProject = stringStringEntry.getValue();
NodeModel nodeModel = nodeService.getByKey(nodeId);
Assert.notNull(nodeModel, "不存在对应的节点");
//
boolean exists = projectInfoCacheService.exists(nodeModel.getWorkspaceId(), nodeModel.getId(), nodeIdProject);
Assert.state(exists, "没有找到对应的项目id:" + nodeIdProject);
//
OutGivingNodeProject outGivingNodeProject = outGivingModel.getNodeProject(nodeModel.getId(), nodeIdProject);
if (outGivingNodeProject == null) {
outGivingNodeProject = new OutGivingNodeProject();
}
outGivingNodeProject.setNodeId(nodeModel.getId());
outGivingNodeProject.setProjectId(nodeIdProject);
return outGivingNodeProject;
}).peek(outGivingNodeProject -> {
// 判断项目是否已经被使用过啦
if (outGivingModels != null) {
for (OutGivingModel outGivingModel1 : outGivingModels) {
if (outGivingModel1.getId().equalsIgnoreCase(outGivingModel.getId())) {
continue;
}
boolean checkContains = outGivingModel1.checkContains(outGivingNodeProject.getNodeId(), outGivingNodeProject.getProjectId());
Assert.state(!checkContains, "已经存在相同的分发项目:" + outGivingNodeProject.getProjectId());
}
}
}).collect(Collectors.toList());
Assert.state(CollUtil.size(outGivingNodeProjects) >= 2, "至少选择2个节点项目");
outGivingModel.outGivingNodeProjectList(outGivingNodeProjects);
//
String afterOpt = getParameter("afterOpt");
AfterOpt afterOpt1 = BaseEnum.getEnum(AfterOpt.class, Convert.toInt(afterOpt, 0));
Assert.notNull(afterOpt1, "请选择分发后的操作");
outGivingModel.setAfterOpt(afterOpt1.getCode());
//
int intervalTime = getParameterInt("intervalTime", 10);
outGivingModel.setIntervalTime(intervalTime);
//
outGivingModel.setClearOld(Convert.toBool(getParameter("clearOld"), false));
}
use of io.jpom.model.data.OutGivingNodeProject in project Jpom by dromara.
the class OutGivingController method releaseDel.
/**
* 删除分发信息
*
* @param id 分发id
* @return json
*/
@RequestMapping(value = "release_del.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public String releaseDel(String id) {
HttpServletRequest request = getRequest();
// 判断构建
boolean releaseMethod = buildService.checkReleaseMethod(id, request, BuildReleaseMethod.Outgiving);
Assert.state(!releaseMethod, "当前分发存在构建项,不能删除");
OutGivingModel outGivingServerItem = outGivingServer.getByKey(id, request);
UserModel userModel = getUser();
// 解除项目分发独立分发属性
List<OutGivingNodeProject> outGivingNodeProjectList = outGivingServerItem.outGivingNodeProjectList();
if (outGivingNodeProjectList != null) {
outGivingNodeProjectList.forEach(outGivingNodeProject -> {
NodeModel item = nodeService.getByKey(outGivingNodeProject.getNodeId());
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", outGivingNodeProject.getProjectId());
NodeForward.request(item, NodeUrl.Manage_ReleaseOutGiving, userModel, jsonObject);
});
}
int byKey = outGivingServer.delByKey(id, request);
if (byKey > 0) {
// 删除日志
Entity where = new Entity();
where.set("outGivingId", id);
dbOutGivingLogService.del(where);
}
return JsonMessage.getString(200, "操作成功");
}
use of io.jpom.model.data.OutGivingNodeProject in project Jpom by dromara.
the class OutGivingItemRun method updateStatus.
/**
* 更新状态
*
* @param logId 日志ID
* @param outGivingId 分发id
* @param outGivingNodeProjectItem 分发项
* @param status 状态
* @param msg 消息描述
*/
public static void updateStatus(String logId, String outGivingId, OutGivingNodeProject outGivingNodeProjectItem, OutGivingNodeProject.Status status, String msg, String userId) {
synchronized (outGivingId.intern()) {
OutGivingServer outGivingServer = SpringUtil.getBean(OutGivingServer.class);
OutGivingModel outGivingModel = outGivingServer.getByKey(outGivingId);
List<OutGivingNodeProject> outGivingNodeProjects = outGivingModel.outGivingNodeProjectList();
Assert.notEmpty(outGivingNodeProjects, "没有分发项目");
OutGivingNodeProject finOutGivingNodeProject = null;
//
for (OutGivingNodeProject outGivingNodeProject : outGivingNodeProjects) {
if (!outGivingNodeProject.getProjectId().equalsIgnoreCase(outGivingNodeProjectItem.getProjectId()) || !outGivingNodeProject.getNodeId().equalsIgnoreCase(outGivingNodeProjectItem.getNodeId())) {
continue;
}
outGivingNodeProject.setStatus(status.getCode());
outGivingNodeProject.setResult(msg);
outGivingNodeProject.setLastOutGivingTime(DateUtil.now());
//
finOutGivingNodeProject = outGivingNodeProject;
}
{
List<OutGivingNodeProject.Status> collect = outGivingNodeProjects.stream().map(outGivingNodeProject -> EnumUtil.likeValueOf(OutGivingNodeProject.Status.class, outGivingNodeProject.getStatus())).collect(Collectors.toList());
OutGivingModel.Status outGivingStatus = CollUtil.contains(collect, OutGivingNodeProject.Status.Ing) ? OutGivingModel.Status.ING : OutGivingModel.Status.DONE;
// 更新分发数据
OutGivingModel outGivingModel1 = new OutGivingModel();
outGivingModel1.setId(outGivingId);
outGivingModel1.setStatus(outGivingStatus.getCode());
outGivingModel1.outGivingNodeProjectList(outGivingNodeProjects);
outGivingServer.update(outGivingModel1);
}
// 更新日志数据
OutGivingLog outGivingLog = new OutGivingLog();
outGivingLog.setId(StrUtil.emptyToDefault(logId, IdUtil.fastSimpleUUID()));
if (finOutGivingNodeProject != null) {
outGivingLog.setNodeId(finOutGivingNodeProject.getNodeId());
outGivingLog.setProjectId(finOutGivingNodeProject.getProjectId());
}
outGivingLog.setModifyUser(userId);
outGivingLog.setOutGivingId(outGivingId);
outGivingLog.setResult(msg);
outGivingLog.setStatus(status.getCode());
DbOutGivingLogService dbOutGivingLogService = SpringUtil.getBean(DbOutGivingLogService.class);
if (status == OutGivingNodeProject.Status.Ing || status == OutGivingNodeProject.Status.Cancel) {
// 开始或者 取消都还没有记录
dbOutGivingLogService.insert(outGivingLog);
} else {
outGivingLog.setEndTime(SystemClock.now());
dbOutGivingLogService.update(outGivingLog);
}
}
}
Aggregations