Search in sources :

Example 1 with OutGivingServer

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);
    }
}
Also used : OutGivingServer(io.jpom.service.node.OutGivingServer) JSONArray(com.alibaba.fastjson.JSONArray) JSON(com.alibaba.fastjson.JSON) File(java.io.File)

Example 2 with OutGivingServer

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());
    }
}
Also used : NodeForward(io.jpom.common.forward.NodeForward) ObjectUtil(cn.hutool.core.util.ObjectUtil) EnumUtil(cn.hutool.core.util.EnumUtil) NodeModel(io.jpom.model.data.NodeModel) SpringUtil(cn.jiangzeyin.common.spring.SpringUtil) AfterOpt(io.jpom.model.AfterOpt) File(java.io.File) OutGivingModel(io.jpom.model.data.OutGivingModel) NodeUrl(io.jpom.common.forward.NodeUrl) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) JsonMessage(cn.jiangzeyin.common.JsonMessage) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) JpomApplication(io.jpom.JpomApplication) JSONObject(com.alibaba.fastjson.JSONObject) OutGivingServer(io.jpom.service.node.OutGivingServer) ThreadUtil(cn.hutool.core.thread.ThreadUtil) UserModel(io.jpom.model.data.UserModel) OutGivingServer(io.jpom.service.node.OutGivingServer) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) AfterOpt(io.jpom.model.AfterOpt) OutGivingModel(io.jpom.model.data.OutGivingModel)

Example 3 with OutGivingServer

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);
        }
    }
}
Also used : HttpStatus(cn.hutool.http.HttpStatus) OutGivingServer(io.jpom.service.node.OutGivingServer) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) OutGivingLog(io.jpom.model.log.OutGivingLog) OutGivingModel(io.jpom.model.data.OutGivingModel) List(java.util.List) DbOutGivingLogService(io.jpom.service.dblog.DbOutGivingLogService)

Aggregations

OutGivingServer (io.jpom.service.node.OutGivingServer)3 OutGivingModel (io.jpom.model.data.OutGivingModel)2 OutGivingNodeProject (io.jpom.model.data.OutGivingNodeProject)2 File (java.io.File)2 List (java.util.List)2 ThreadUtil (cn.hutool.core.thread.ThreadUtil)1 EnumUtil (cn.hutool.core.util.EnumUtil)1 ObjectUtil (cn.hutool.core.util.ObjectUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 HttpStatus (cn.hutool.http.HttpStatus)1 JsonMessage (cn.jiangzeyin.common.JsonMessage)1 SpringUtil (cn.jiangzeyin.common.spring.SpringUtil)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 JpomApplication (io.jpom.JpomApplication)1 NodeForward (io.jpom.common.forward.NodeForward)1 NodeUrl (io.jpom.common.forward.NodeUrl)1 AfterOpt (io.jpom.model.AfterOpt)1 NodeModel (io.jpom.model.data.NodeModel)1