use of io.jpom.model.data.OutGivingModel 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.OutGivingModel 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.OutGivingModel in project Jpom by dromara.
the class OutGivingProjectController method check.
private OutGivingModel check(String id) {
OutGivingModel outGivingModel = outGivingServer.getByKey(id, getRequest());
Assert.notNull(outGivingModel, "上传失败,没有找到对应的分发项目");
// 检查状态
Integer statusCode = outGivingModel.getStatus();
OutGivingModel.Status status = BaseEnum.getEnum(OutGivingModel.Status.class, statusCode, OutGivingModel.Status.NO);
Assert.state(status != OutGivingModel.Status.ING, "当前还在分发中,请等待分发结束");
return outGivingModel;
}
use of io.jpom.model.data.OutGivingModel in project Jpom by dromara.
the class OutGivingProjectController method remoteDownload.
/**
* 远程下载节点分发文件
*
* @param id 分发id
* @param afterOpt 之后的操作
* @return json
*/
@RequestMapping(value = "remote_download", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.REMOTE_DOWNLOAD)
public String remoteDownload(String id, String afterOpt, String clearOld, String url, String autoUnzip) {
OutGivingModel outGivingModel = this.check(id);
AfterOpt afterOpt1 = BaseEnum.getEnum(AfterOpt.class, Convert.toInt(afterOpt, 0));
Assert.notNull(afterOpt1, "请选择分发后的操作");
// 验证远程 地址
ServerWhitelist whitelist = outGivingWhitelistService.getServerWhitelistData(getRequest());
Set<String> allowRemoteDownloadHost = whitelist.getAllowRemoteDownloadHost();
Assert.state(CollUtil.isNotEmpty(allowRemoteDownloadHost), "还没有配置运行的远程地址");
List<String> collect = allowRemoteDownloadHost.stream().filter(s -> StrUtil.startWith(url, s)).collect(Collectors.toList());
Assert.state(CollUtil.isNotEmpty(collect), "不允许下载当前地址的文件");
try {
// outGivingModel = outGivingServer.getItem(id);
outGivingModel.setClearOld(Convert.toBool(clearOld, false));
outGivingModel.setAfterOpt(afterOpt1.getCode());
outGivingServer.update(outGivingModel);
// 下载
File file = FileUtil.file(ServerConfigBean.getInstance().getUserTempPath(), ServerConfigBean.OUTGIVING_FILE, id);
FileUtil.mkdir(file);
File downloadFile = HttpUtil.downloadFileFromUrl(url, file);
boolean unzip = BooleanUtil.toBoolean(autoUnzip);
//
this.checkZip(downloadFile, unzip);
// 开启
OutGivingRun.startRun(outGivingModel.getId(), downloadFile, getUser(), unzip);
return JsonMessage.getString(200, "分发成功");
} catch (Exception e) {
DefaultSystemLog.getLog().error("下载远程文件异常", e);
return JsonMessage.getString(500, "下载远程文件失败:" + e.getMessage());
}
}
use of io.jpom.model.data.OutGivingModel in project Jpom by dromara.
the class OutGivingController method updateGiving.
private String updateGiving(String id) {
OutGivingModel outGivingModel = outGivingServer.getByKey(id);
Assert.notNull(outGivingModel, "没有找到对应的分发id");
doData(outGivingModel);
outGivingServer.update(outGivingModel);
return JsonMessage.getString(200, "修改成功");
}
Aggregations