diff --git a/CHANGELOG-BETA.md b/CHANGELOG-BETA.md index 12b543018..aed381a08 100644 --- a/CHANGELOG-BETA.md +++ b/CHANGELOG-BETA.md @@ -8,6 +8,8 @@ ### 🐞 解决BUG、优化功能 1. 【all】优化 日志记录器提升日志记录性能 +2. 【server】优化 取消/停止构建采用异常来打断子进程 +3. 【server】修复 本地构建无法取消 ------ diff --git a/modules/agent-transport/agent-transport-common/src/main/java/org/dromara/jpom/transport/AgentException.java b/modules/agent-transport/agent-transport-common/src/main/java/org/dromara/jpom/transport/TransportAgentException.java similarity index 91% rename from modules/agent-transport/agent-transport-common/src/main/java/org/dromara/jpom/transport/AgentException.java rename to modules/agent-transport/agent-transport-common/src/main/java/org/dromara/jpom/transport/TransportAgentException.java index 45a494596..277a58f2c 100644 --- a/modules/agent-transport/agent-transport-common/src/main/java/org/dromara/jpom/transport/AgentException.java +++ b/modules/agent-transport/agent-transport-common/src/main/java/org/dromara/jpom/transport/TransportAgentException.java @@ -29,9 +29,9 @@ import lombok.NoArgsConstructor; * @since 2022/12/24 */ @NoArgsConstructor -public class AgentException extends RuntimeException { +public class TransportAgentException extends RuntimeException { - public AgentException(String message) { + public TransportAgentException(String message) { super(message); } } diff --git a/modules/agent-transport/agent-transport-http/src/main/java/org/dromara/jpom/transport/HttpTransportServer.java b/modules/agent-transport/agent-transport-http/src/main/java/org/dromara/jpom/transport/HttpTransportServer.java index e5c1bf5e7..f168f09c1 100644 --- a/modules/agent-transport/agent-transport-http/src/main/java/org/dromara/jpom/transport/HttpTransportServer.java +++ b/modules/agent-transport/agent-transport-http/src/main/java/org/dromara/jpom/transport/HttpTransportServer.java @@ -108,7 +108,7 @@ public class HttpTransportServer implements TransportServer { } } catch (Exception e) { log.error("编码异常", e); - throw new AgentException("节点传输信息编码异常:" + e.getMessage()); + throw new TransportAgentException("节点传输信息编码异常:" + e.getMessage()); } }); } @@ -124,7 +124,7 @@ public class HttpTransportServer implements TransportServer { log.debug("Completed {}", body); if (status != HttpStatus.HTTP_OK) { log.warn("{} 响应异常 状态码错误:{} {}", nodeInfo.name(), status, body); - throw new AgentException(nodeInfo.name() + " 节点响应异常,状态码错误:" + status); + throw new TransportAgentException(nodeInfo.name() + " 节点响应异常,状态码错误:" + status); } return body; }); diff --git a/modules/agent/src/main/java/org/dromara/jpom/common/AgentExceptionHandler.java b/modules/agent/src/main/java/org/dromara/jpom/common/AgentExceptionHandler.java index e88a47f5e..63034f91b 100644 --- a/modules/agent/src/main/java/org/dromara/jpom/common/AgentExceptionHandler.java +++ b/modules/agent/src/main/java/org/dromara/jpom/common/AgentExceptionHandler.java @@ -23,6 +23,7 @@ package org.dromara.jpom.common; import lombok.extern.slf4j.Slf4j; +import org.dromara.jpom.exception.BaseExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; /** diff --git a/modules/agent/src/main/java/org/dromara/jpom/common/commander/AbstractProjectCommander.java b/modules/agent/src/main/java/org/dromara/jpom/common/commander/AbstractProjectCommander.java index 050e0a80b..02bf0846a 100644 --- a/modules/agent/src/main/java/org/dromara/jpom/common/commander/AbstractProjectCommander.java +++ b/modules/agent/src/main/java/org/dromara/jpom/common/commander/AbstractProjectCommander.java @@ -41,7 +41,7 @@ import cn.hutool.system.SystemUtil; import cn.keepbx.jpom.plugins.IPlugin; import lombok.Lombok; import lombok.extern.slf4j.Slf4j; -import org.dromara.jpom.common.IllegalArgument2Exception; +import org.dromara.jpom.exception.IllegalArgument2Exception; import org.dromara.jpom.configuration.ProjectConfig; import org.dromara.jpom.configuration.ProjectLogConfig; import org.dromara.jpom.model.RunMode; diff --git a/modules/agent/src/main/java/org/dromara/jpom/service/script/DslScriptServer.java b/modules/agent/src/main/java/org/dromara/jpom/service/script/DslScriptServer.java index 097252c9d..41b74ef1e 100644 --- a/modules/agent/src/main/java/org/dromara/jpom/service/script/DslScriptServer.java +++ b/modules/agent/src/main/java/org/dromara/jpom/service/script/DslScriptServer.java @@ -34,7 +34,7 @@ import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson2.JSONObject; import org.dromara.jpom.JpomApplication; import org.dromara.jpom.common.Const; -import org.dromara.jpom.common.IllegalArgument2Exception; +import org.dromara.jpom.exception.IllegalArgument2Exception; import org.dromara.jpom.configuration.ProjectLogConfig; import org.dromara.jpom.model.EnvironmentMapBuilder; import org.dromara.jpom.model.data.DslYmlDto; diff --git a/modules/common/src/main/java/org/dromara/jpom/common/BaseExceptionHandler.java b/modules/common/src/main/java/org/dromara/jpom/exception/BaseExceptionHandler.java similarity index 99% rename from modules/common/src/main/java/org/dromara/jpom/common/BaseExceptionHandler.java rename to modules/common/src/main/java/org/dromara/jpom/exception/BaseExceptionHandler.java index dfe7f2623..9c8786564 100644 --- a/modules/common/src/main/java/org/dromara/jpom/common/BaseExceptionHandler.java +++ b/modules/common/src/main/java/org/dromara/jpom/exception/BaseExceptionHandler.java @@ -20,7 +20,7 @@ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package org.dromara.jpom.common; +package org.dromara.jpom.exception; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.exceptions.ValidateException; diff --git a/modules/common/src/main/java/org/dromara/jpom/common/IllegalArgument2Exception.java b/modules/common/src/main/java/org/dromara/jpom/exception/IllegalArgument2Exception.java similarity index 97% rename from modules/common/src/main/java/org/dromara/jpom/common/IllegalArgument2Exception.java rename to modules/common/src/main/java/org/dromara/jpom/exception/IllegalArgument2Exception.java index d2c5af606..84be270d3 100644 --- a/modules/common/src/main/java/org/dromara/jpom/common/IllegalArgument2Exception.java +++ b/modules/common/src/main/java/org/dromara/jpom/exception/IllegalArgument2Exception.java @@ -20,7 +20,7 @@ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package org.dromara.jpom.common; +package org.dromara.jpom.exception; /** * @author bwcx_jzy diff --git a/modules/common/src/main/java/org/dromara/jpom/exception/LogRecorderCloseException.java b/modules/common/src/main/java/org/dromara/jpom/exception/LogRecorderCloseException.java new file mode 100644 index 000000000..babdd68b2 --- /dev/null +++ b/modules/common/src/main/java/org/dromara/jpom/exception/LogRecorderCloseException.java @@ -0,0 +1,11 @@ +package org.dromara.jpom.exception; + +/** + * @author bwcx_jzy + * @since 24/1/4 004 + */ +public class LogRecorderCloseException extends IllegalStateException { + public LogRecorderCloseException() { + super("日志记录器被关闭/或者未启用"); + } +} diff --git a/modules/common/src/main/java/org/dromara/jpom/util/LogRecorder.java b/modules/common/src/main/java/org/dromara/jpom/util/LogRecorder.java index 493309a44..851d3ca93 100644 --- a/modules/common/src/main/java/org/dromara/jpom/util/LogRecorder.java +++ b/modules/common/src/main/java/org/dromara/jpom/util/LogRecorder.java @@ -31,6 +31,8 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.keepbx.jpom.log.ILogRecorder; import lombok.extern.slf4j.Slf4j; +import org.dromara.jpom.exception.LogRecorderCloseException; +import org.springframework.util.Assert; import java.io.File; import java.io.PrintWriter; @@ -44,16 +46,12 @@ import java.nio.charset.Charset; */ @Slf4j public class LogRecorder implements ILogRecorder, AutoCloseable { - private final File file; - private final PrintWriter writer; - /** - * 是否关闭 - */ - private boolean close; + + private File file; + private PrintWriter writer; public LogRecorder(File file, Charset charset) { if (file == null) { - this.close = true; this.writer = null; this.file = null; return; @@ -102,15 +100,13 @@ public class LogRecorder implements ILogRecorder, AutoCloseable { */ public void error(String title, Throwable throwable) { log.error(title, throwable); - if (!this.close) { - writer.println(title); - String s = ExceptionUtil.stacktraceToString(throwable); - writer.println(s); - writer.flush(); - } else { - throw new IllegalStateException("日志记录器未启用"); + if (writer == null) { + throw new LogRecorderCloseException(); } - + writer.println(title); + String s = ExceptionUtil.stacktraceToString(throwable); + writer.println(s); + writer.flush(); } /** @@ -119,13 +115,12 @@ public class LogRecorder implements ILogRecorder, AutoCloseable { * @param info 日志 */ public String info(String info, Object... vals) { - String format = StrUtil.format(info, vals); - if (!this.close) { - writer.println(format); - writer.flush(); - } else { - throw new IllegalStateException("日志记录器未启用"); + if (writer == null) { + throw new LogRecorderCloseException(); } + String format = StrUtil.format(info, vals); + writer.println(format); + writer.flush(); return format; } @@ -162,12 +157,12 @@ public class LogRecorder implements ILogRecorder, AutoCloseable { * @param info 日志 */ public void append(String info, Object... vals) { - if (!this.close) { - writer.append(StrUtil.format(info, vals)); - writer.flush(); - } else { - throw new IllegalStateException("日志记录器未启用"); + if (writer == null) { + throw new LogRecorderCloseException(); } + writer.append(StrUtil.format(info, vals)); + writer.flush(); + } /** @@ -182,10 +177,12 @@ public class LogRecorder implements ILogRecorder, AutoCloseable { @Override public void close() { IoUtil.close(writer); - this.close = true; + this.writer = null; + this.file = null; } public long size() { + Assert.notNull(writer, "日志记录器未启用"); return FileUtil.size(this.file); } } diff --git a/modules/server/src/main/java/org/dromara/jpom/build/BuildExecuteManage.java b/modules/server/src/main/java/org/dromara/jpom/build/BuildExecuteManage.java index 8978a185a..3760eb201 100644 --- a/modules/server/src/main/java/org/dromara/jpom/build/BuildExecuteManage.java +++ b/modules/server/src/main/java/org/dromara/jpom/build/BuildExecuteManage.java @@ -44,6 +44,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.jpom.JpomApplication; import org.dromara.jpom.common.BaseServerController; import org.dromara.jpom.common.ServerConst; +import org.dromara.jpom.exception.LogRecorderCloseException; import org.dromara.jpom.func.assets.server.MachineDockerServer; import org.dromara.jpom.func.files.service.FileStorageService; import org.dromara.jpom.model.data.BuildInfoModel; @@ -115,6 +116,7 @@ public class BuildExecuteManage implements Runnable { private LogRecorder logRecorder; private File gitFile; private Thread currentThread; + private ReleaseManage releaseManage; /** * 提交任务时间 @@ -627,11 +629,15 @@ public class BuildExecuteManage implements Runnable { int waitFor = JpomApplication.getInstance() .execScript(s1 + script, file -> { try { - return CommandUtil.execWaitFor(file, this.gitFile, environment, StrUtil.EMPTY, (s, process) -> logRecorder.info(s)); + return CommandUtil.execWaitFor(file, this.gitFile, environment, StrUtil.EMPTY, (s, process) -> { + BuildExecuteManage.this.process = process; + logRecorder.info(s); + }); } catch (IOException | InterruptedException e) { throw Lombok.sneakyThrow(e); } }); + BuildExecuteManage.this.process = null; logRecorder.system("执行脚本的退出码是:{}", waitFor); // 判断是否为严格执行 if (buildExtraModule.strictlyEnforce()) { @@ -653,7 +659,7 @@ public class BuildExecuteManage implements Runnable { BuildInfoModel buildInfoModel = taskData.buildInfoModel; UserModel userModel = taskData.userModel; // 发布文件 - ReleaseManage releaseManage = ReleaseManage.builder() + this.releaseManage = ReleaseManage.builder() .buildNumberId(buildInfoModel.getBuildId()) .buildExtraModule(buildExtraModule) .userModel(userModel) @@ -849,6 +855,8 @@ public class BuildExecuteManage implements Runnable { if (!stop) { // 没有执行 stop this.asyncWebHooks("success"); } + } catch (LogRecorderCloseException logRecorderCloseException) { + log.warn("构建日志记录器已关闭,可能手动取消停止构建,流程:{}", processName); } catch (DiyInterruptException diyInterruptException) { // 主动中断 this.asyncWebHooks("stop", "process", processName); diff --git a/modules/server/src/main/java/org/dromara/jpom/build/ReleaseManage.java b/modules/server/src/main/java/org/dromara/jpom/build/ReleaseManage.java index be2068eb7..887b22217 100644 --- a/modules/server/src/main/java/org/dromara/jpom/build/ReleaseManage.java +++ b/modules/server/src/main/java/org/dromara/jpom/build/ReleaseManage.java @@ -118,6 +118,7 @@ public class ReleaseManage { private final LogRecorder logRecorder; private File resultFile; + private Process process; private Integer getRealBuildNumberId() { @@ -386,11 +387,15 @@ public class ReleaseManage { int waitFor = JpomApplication.getInstance() .execScript(s1 + releaseCommand, file -> { try { - return CommandUtil.execWaitFor(file, sourceFile, envFileMap, StrUtil.EMPTY, (s, process) -> logRecorder.info(s)); + return CommandUtil.execWaitFor(file, sourceFile, envFileMap, StrUtil.EMPTY, (s, process) -> { + ReleaseManage.this.process = process; + logRecorder.info(s); + }); } catch (IOException | InterruptedException e) { throw Lombok.sneakyThrow(e); } }); + ReleaseManage.this.process = null; logRecorder.system("执行发布脚本的退出码是:{}", waitFor); // 判断是否为严格执行 if (buildExtraModule.strictlyEnforce()) { diff --git a/modules/server/src/main/java/org/dromara/jpom/common/GlobalDefaultExceptionHandler.java b/modules/server/src/main/java/org/dromara/jpom/common/GlobalDefaultExceptionHandler.java index 30251433f..320e80462 100644 --- a/modules/server/src/main/java/org/dromara/jpom/common/GlobalDefaultExceptionHandler.java +++ b/modules/server/src/main/java/org/dromara/jpom/common/GlobalDefaultExceptionHandler.java @@ -27,7 +27,9 @@ import cn.keepbx.jpom.model.JsonMessage; import lombok.extern.slf4j.Slf4j; import org.dromara.jpom.exception.AgentAuthorizeException; import org.dromara.jpom.exception.AgentException; +import org.dromara.jpom.exception.BaseExceptionHandler; import org.dromara.jpom.exception.PermissionException; +import org.dromara.jpom.transport.TransportAgentException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -63,7 +65,7 @@ public class GlobalDefaultExceptionHandler extends BaseExceptionHandler { * @author jzy * @since 2021-08-01 */ - @ExceptionHandler({AgentException.class}) + @ExceptionHandler({AgentException.class, TransportAgentException.class}) public IJsonMessage agentExceptionHandler(HttpServletRequest request, AgentException e) { Throwable cause = e.getCause(); if (cause != null) { diff --git a/modules/server/src/main/java/org/dromara/jpom/common/forward/NodeForward.java b/modules/server/src/main/java/org/dromara/jpom/common/forward/NodeForward.java index 43e087502..ca06da270 100644 --- a/modules/server/src/main/java/org/dromara/jpom/common/forward/NodeForward.java +++ b/modules/server/src/main/java/org/dromara/jpom/common/forward/NodeForward.java @@ -44,12 +44,12 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.jpom.common.BaseServerController; import org.dromara.jpom.common.Const; import org.dromara.jpom.configuration.NodeConfig; +import org.dromara.jpom.exception.AgentAuthorizeException; +import org.dromara.jpom.exception.AgentException; import org.dromara.jpom.func.assets.model.MachineNodeModel; import org.dromara.jpom.func.assets.server.MachineNodeServer; import org.dromara.jpom.model.data.NodeModel; import org.dromara.jpom.model.user.UserModel; -import org.dromara.jpom.exception.AgentException; -import org.dromara.jpom.exception.AgentAuthorizeException; import org.dromara.jpom.system.ServerConfig; import org.dromara.jpom.transport.*; import org.dromara.jpom.util.StrictSyncFinisher; diff --git a/modules/server/src/main/java/org/dromara/jpom/common/interceptor/PermissionInterceptor.java b/modules/server/src/main/java/org/dromara/jpom/common/interceptor/PermissionInterceptor.java index 9de9896b7..cb3797e35 100644 --- a/modules/server/src/main/java/org/dromara/jpom/common/interceptor/PermissionInterceptor.java +++ b/modules/server/src/main/java/org/dromara/jpom/common/interceptor/PermissionInterceptor.java @@ -28,6 +28,7 @@ import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.spring.SpringUtil; import cn.keepbx.jpom.model.JsonMessage; import org.dromara.jpom.common.BaseServerController; +import org.dromara.jpom.exception.AgentException; import org.dromara.jpom.model.BaseNodeModel; import org.dromara.jpom.model.data.NodeModel; import org.dromara.jpom.model.user.UserBindWorkspaceModel; @@ -37,7 +38,6 @@ import org.dromara.jpom.service.h2db.BaseNodeService; import org.dromara.jpom.service.h2db.BaseWorkspaceService; import org.dromara.jpom.service.node.NodeService; import org.dromara.jpom.service.user.UserBindWorkspaceService; -import org.dromara.jpom.exception.AgentException; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.method.HandlerMethod; diff --git a/modules/server/src/main/java/org/dromara/jpom/controller/user/UserBasicInfoController.java b/modules/server/src/main/java/org/dromara/jpom/controller/user/UserBasicInfoController.java index 50248d22b..bea9bb8df 100644 --- a/modules/server/src/main/java/org/dromara/jpom/controller/user/UserBasicInfoController.java +++ b/modules/server/src/main/java/org/dromara/jpom/controller/user/UserBasicInfoController.java @@ -54,8 +54,6 @@ import org.dromara.jpom.model.log.BuildHistoryLog; import org.dromara.jpom.model.log.UserOperateLogV1; import org.dromara.jpom.model.user.UserModel; import org.dromara.jpom.monitor.EmailUtil; -import org.dromara.jpom.permission.Feature; -import org.dromara.jpom.permission.MethodFeature; import org.dromara.jpom.service.dblog.DbBuildHistoryLogService; import org.dromara.jpom.service.dblog.DbUserOperateLogService; import org.dromara.jpom.service.system.SystemParametersServer; @@ -305,7 +303,6 @@ public class UserBasicInfoController extends BaseServerController { * @return json */ @RequestMapping(value = "list-login-log-data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.LIST) public IJsonMessage> listLoginLogData(HttpServletRequest request) { UserModel user = getUser(); PageResultDto pageResult = userLoginLogServer.listPageByUserId(request, user.getId()); @@ -318,7 +315,6 @@ public class UserBasicInfoController extends BaseServerController { * @return json */ @RequestMapping(value = "list-operate-log-data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.LIST) public IJsonMessage> listOperateLogData(HttpServletRequest request) { UserModel user = getUser(); PageResultDto pageResult = dbUserOperateLogService.listPageByUserId(request, user.getId()); @@ -326,7 +322,6 @@ public class UserBasicInfoController extends BaseServerController { } @RequestMapping(value = "recent-log-data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.LIST) public IJsonMessage recentData(HttpServletRequest request) { UserModel user = getUser(); JSONObject jsonObject = new JSONObject(); diff --git a/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineNodeServer.java b/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineNodeServer.java index d02709688..fb8ed2351 100644 --- a/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineNodeServer.java +++ b/modules/server/src/main/java/org/dromara/jpom/func/assets/server/MachineNodeServer.java @@ -47,6 +47,8 @@ import org.dromara.jpom.common.forward.NodeForward; import org.dromara.jpom.common.forward.NodeUrl; import org.dromara.jpom.configuration.NodeConfig; import org.dromara.jpom.cron.CronUtils; +import org.dromara.jpom.exception.AgentAuthorizeException; +import org.dromara.jpom.exception.AgentException; import org.dromara.jpom.func.assets.model.MachineNodeModel; import org.dromara.jpom.func.assets.model.MachineNodeStatLogModel; import org.dromara.jpom.func.system.service.ClusterInfoService; @@ -54,8 +56,6 @@ import org.dromara.jpom.model.data.NodeModel; import org.dromara.jpom.model.user.UserModel; import org.dromara.jpom.service.h2db.BaseDbService; import org.dromara.jpom.service.node.NodeService; -import org.dromara.jpom.exception.AgentException; -import org.dromara.jpom.exception.AgentAuthorizeException; import org.dromara.jpom.system.ServerConfig; import org.dromara.jpom.system.db.InitDb; import org.springframework.context.ApplicationContext; diff --git a/modules/server/src/main/java/org/dromara/jpom/service/node/script/NodeScriptExecuteLogServer.java b/modules/server/src/main/java/org/dromara/jpom/service/node/script/NodeScriptExecuteLogServer.java index a664f6d59..aead47c50 100644 --- a/modules/server/src/main/java/org/dromara/jpom/service/node/script/NodeScriptExecuteLogServer.java +++ b/modules/server/src/main/java/org/dromara/jpom/service/node/script/NodeScriptExecuteLogServer.java @@ -33,6 +33,7 @@ import org.dromara.jpom.common.BaseServerController; import org.dromara.jpom.common.ServerConst; import org.dromara.jpom.common.forward.NodeForward; import org.dromara.jpom.common.forward.NodeUrl; +import org.dromara.jpom.exception.AgentException; import org.dromara.jpom.model.BaseDbModel; import org.dromara.jpom.model.data.NodeModel; import org.dromara.jpom.model.data.WorkspaceModel; @@ -41,7 +42,6 @@ import org.dromara.jpom.model.user.UserModel; import org.dromara.jpom.service.h2db.BaseNodeService; import org.dromara.jpom.service.node.NodeService; import org.dromara.jpom.service.system.WorkspaceService; -import org.dromara.jpom.exception.AgentException; import org.springframework.stereotype.Service; import java.util.Collection;