Search in sources :

Example 1 with OutGivingModel

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());
    }
}
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 2 with OutGivingModel

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ServerWhitelist(io.jpom.model.data.ServerWhitelist) ServerConfigBean(io.jpom.system.ServerConfigBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Feature(io.jpom.permission.Feature) ProjectInfoCacheService(io.jpom.service.node.ProjectInfoCacheService) JsonMessage(cn.jiangzeyin.common.JsonMessage) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpUtil(cn.hutool.http.HttpUtil) BaseEnum(io.jpom.model.BaseEnum) OutGivingRun(io.jpom.outgiving.OutGivingRun) BaseNodeModel(io.jpom.model.BaseNodeModel) OutGivingServer(io.jpom.service.node.OutGivingServer) MethodFeature(io.jpom.permission.MethodFeature) NodeForward(io.jpom.common.forward.NodeForward) ClassFeature(io.jpom.permission.ClassFeature) MediaType(org.springframework.http.MediaType) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IOException(java.io.IOException) AfterOpt(io.jpom.model.AfterOpt) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) BooleanUtil(cn.hutool.core.util.BooleanUtil) OutGivingModel(io.jpom.model.data.OutGivingModel) NodeUrl(io.jpom.common.forward.NodeUrl) Objects(java.util.Objects) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) ValidatorRule(cn.jiangzeyin.common.validator.ValidatorRule) List(java.util.List) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) StringUtil(io.jpom.util.StringUtil) Convert(cn.hutool.core.convert.Convert) ConfigBean(io.jpom.system.ConfigBean) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) BaseServerController(io.jpom.common.BaseServerController) Assert(org.springframework.util.Assert) BaseNodeModel(io.jpom.model.BaseNodeModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) OutGivingModel(io.jpom.model.data.OutGivingModel) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with OutGivingModel

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;
}
Also used : OutGivingModel(io.jpom.model.data.OutGivingModel)

Example 4 with 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());
    }
}
Also used : MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ServerWhitelist(io.jpom.model.data.ServerWhitelist) ServerConfigBean(io.jpom.system.ServerConfigBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Feature(io.jpom.permission.Feature) ProjectInfoCacheService(io.jpom.service.node.ProjectInfoCacheService) JsonMessage(cn.jiangzeyin.common.JsonMessage) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpUtil(cn.hutool.http.HttpUtil) BaseEnum(io.jpom.model.BaseEnum) OutGivingRun(io.jpom.outgiving.OutGivingRun) BaseNodeModel(io.jpom.model.BaseNodeModel) OutGivingServer(io.jpom.service.node.OutGivingServer) MethodFeature(io.jpom.permission.MethodFeature) NodeForward(io.jpom.common.forward.NodeForward) ClassFeature(io.jpom.permission.ClassFeature) MediaType(org.springframework.http.MediaType) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IOException(java.io.IOException) AfterOpt(io.jpom.model.AfterOpt) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) BooleanUtil(cn.hutool.core.util.BooleanUtil) OutGivingModel(io.jpom.model.data.OutGivingModel) NodeUrl(io.jpom.common.forward.NodeUrl) Objects(java.util.Objects) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) ValidatorRule(cn.jiangzeyin.common.validator.ValidatorRule) List(java.util.List) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) StringUtil(io.jpom.util.StringUtil) Convert(cn.hutool.core.convert.Convert) ConfigBean(io.jpom.system.ConfigBean) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) BaseServerController(io.jpom.common.BaseServerController) Assert(org.springframework.util.Assert) ServerWhitelist(io.jpom.model.data.ServerWhitelist) AfterOpt(io.jpom.model.AfterOpt) OutGivingModel(io.jpom.model.data.OutGivingModel) File(java.io.File) IOException(java.io.IOException) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with OutGivingModel

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, "修改成功");
}
Also used : OutGivingModel(io.jpom.model.data.OutGivingModel)

Aggregations

OutGivingModel (io.jpom.model.data.OutGivingModel)12 ClassFeature (io.jpom.permission.ClassFeature)7 Feature (io.jpom.permission.Feature)7 MethodFeature (io.jpom.permission.MethodFeature)7 NodeModel (io.jpom.model.data.NodeModel)6 OutGivingNodeProject (io.jpom.model.data.OutGivingNodeProject)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 JSONObject (com.alibaba.fastjson.JSONObject)5 AfterOpt (io.jpom.model.AfterOpt)5 OutGivingServer (io.jpom.service.node.OutGivingServer)5 List (java.util.List)5 StrUtil (cn.hutool.core.util.StrUtil)4 JsonMessage (cn.jiangzeyin.common.JsonMessage)4 NodeForward (io.jpom.common.forward.NodeForward)4 NodeUrl (io.jpom.common.forward.NodeUrl)4 File (java.io.File)4 CollUtil (cn.hutool.core.collection.CollUtil)3 Convert (cn.hutool.core.convert.Convert)3 Entity (cn.hutool.db.Entity)3 ValidatorItem (cn.jiangzeyin.common.validator.ValidatorItem)3