Search in sources :

Example 1 with ConsoleService

use of io.jpom.service.manage.ConsoleService in project Jpom by dromara.

the class AgentWebSocketConsoleHandle method runMsg.

private void runMsg(ConsoleCommandOp consoleCommandOp, Session session, NodeProjectInfoModel nodeProjectInfoModel, String copyId, JSONObject reqJson) throws Exception {
    ConsoleService consoleService = SpringUtil.getBean(ConsoleService.class);
    // 
    NodeProjectInfoModel.JavaCopyItem copyItem = nodeProjectInfoModel.findCopyItem(copyId);
    JSONObject resultData = null;
    String strResult;
    boolean logUser = false;
    try {
        // 执行相应命令
        switch(consoleCommandOp) {
            case start:
            case restart:
                logUser = true;
                strResult = consoleService.execCommand(consoleCommandOp, nodeProjectInfoModel, copyItem);
                if (strResult.contains(AbstractProjectCommander.RUNNING_TAG)) {
                    resultData = JsonMessage.toJson(200, "操作成功:" + strResult);
                } else {
                    resultData = JsonMessage.toJson(400, strResult);
                }
                break;
            case stop:
                logUser = true;
                // 停止项目
                strResult = consoleService.execCommand(consoleCommandOp, nodeProjectInfoModel, copyItem);
                if (strResult.contains(AbstractProjectCommander.STOP_TAG)) {
                    resultData = JsonMessage.toJson(200, "操作成功:" + strResult);
                } else {
                    resultData = JsonMessage.toJson(500, strResult);
                }
                break;
            case status:
                // 获取项目状态
                strResult = consoleService.execCommand(consoleCommandOp, nodeProjectInfoModel, copyItem);
                if (strResult.contains(AbstractProjectCommander.RUNNING_TAG)) {
                    resultData = JsonMessage.toJson(200, "运行中", strResult);
                } else {
                    resultData = JsonMessage.toJson(404, "未运行", strResult);
                }
                break;
            case showlog:
                {
                    // 进入管理页面后需要实时加载日志
                    // 日志文件路径
                    String fileName = reqJson.getString("fileName");
                    File file;
                    if (StrUtil.isEmpty(fileName)) {
                        file = copyItem == null ? new File(nodeProjectInfoModel.getLog()) : nodeProjectInfoModel.getLog(copyItem);
                    } else {
                        file = FileUtil.file(nodeProjectInfoModel.allLib(), fileName);
                    }
                    try {
                        boolean watcher = AgentFileTailWatcher.addWatcher(file, session);
                        if (!watcher) {
                            SocketSessionUtil.send(session, "监听文件失败,可能文件不存在");
                        }
                    } catch (IOException io) {
                        DefaultSystemLog.getLog().error("监听日志变化", io);
                        SocketSessionUtil.send(session, io.getMessage());
                    }
                    break;
                }
            default:
                resultData = JsonMessage.toJson(404, "不支持的方式:" + consoleCommandOp.name());
                break;
        }
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("执行命令失败", e);
        SocketSessionUtil.send(session, "执行命令失败,详情如下:");
        SocketSessionUtil.send(session, ExceptionUtil.stacktraceToString(e));
        return;
    } finally {
        if (logUser) {
            // 记录操作人
            NodeProjectInfoModel newNodeProjectInfoModel = projectInfoService.getItem(nodeProjectInfoModel.getId());
            String name = getOptUserName(session);
            newNodeProjectInfoModel.setModifyUser(name);
            projectInfoService.updateItem(newNodeProjectInfoModel);
        }
    }
    // 返回数据
    if (resultData != null) {
        reqJson.putAll(resultData);
        reqJson.put("JPOM_MSG", "JPOM_MSG");
        DefaultSystemLog.getLog().info(reqJson.toString());
        SocketSessionUtil.send(session, reqJson.toString());
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) JSONObject(com.alibaba.fastjson.JSONObject) ConsoleService(io.jpom.service.manage.ConsoleService) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 NodeProjectInfoModel (io.jpom.model.data.NodeProjectInfoModel)1 ConsoleService (io.jpom.service.manage.ConsoleService)1 File (java.io.File)1 IOException (java.io.IOException)1