use of io.jpom.service.node.OutGivingServer in project Jpom by dromara.
the class LoadJsonConfigToDb method loadOutgivinInfo.
public void loadOutgivinInfo() {
File backupOldData = FileUtil.file(ConfigBean.getInstance().getDataPath(), "backup_old_data");
// 读取 outgiving 文件内容
File file = FileUtil.file(ConfigBean.getInstance().getDataPath(), ServerConfigBean.OUTGIVING);
if (!FileUtil.exist(file)) {
return;
}
try {
JSON json = JsonFileUtil.readJson(file.getAbsolutePath());
JSONArray jsonArray = JsonFileUtil.formatToArray((JSONObject) json);
List<OutGivingModel> outGivingModels = jsonArray.toJavaList(OutGivingModel.class);
if (outGivingModels == null) {
return;
}
OutGivingServer outGivingServer = SpringUtil.getBean(OutGivingServer.class);
outGivingServer.insert(outGivingModels);
// 将 json 文件转移到备份目录
FileUtil.move(file, FileUtil.mkdir(backupOldData), true);
DefaultSystemLog.getLog().info("{} mv to {}", FileUtil.getAbsolutePath(file), FileUtil.getAbsolutePath(backupOldData));
} catch (Exception e) {
DefaultSystemLog.getLog().error("load OUTGIVING error ", e);
}
}
use of io.jpom.service.node.OutGivingServer 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.service.node.OutGivingServer 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