This commit is contained in:
bwcx_jzy 2024-01-04 21:32:54 +08:00
parent b6c1d12c96
commit 123d82accd
No known key found for this signature in database
GPG Key ID: E187D6E9DDDE8C53
18 changed files with 71 additions and 50 deletions

View File

@ -8,6 +8,8 @@
### 🐞 解决BUG、优化功能
1. 【all】优化 日志记录器提升日志记录性能
2. 【server】优化 取消/停止构建采用异常来打断子进程
3. 【server】修复 本地构建无法取消
------

View File

@ -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);
}
}

View File

@ -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;
});

View File

@ -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;
/**

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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("日志记录器被关闭/或者未启用");
}
}

View File

@ -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) {
if (writer == null) {
throw new LogRecorderCloseException();
}
writer.println(title);
String s = ExceptionUtil.stacktraceToString(throwable);
writer.println(s);
writer.flush();
} else {
throw new IllegalStateException("日志记录器未启用");
}
}
/**
@ -119,13 +115,12 @@ public class LogRecorder implements ILogRecorder, AutoCloseable {
* @param info 日志
*/
public String info(String info, Object... vals) {
if (writer == null) {
throw new LogRecorderCloseException();
}
String format = StrUtil.format(info, vals);
if (!this.close) {
writer.println(format);
writer.flush();
} else {
throw new IllegalStateException("日志记录器未启用");
}
return format;
}
@ -162,12 +157,12 @@ public class LogRecorder implements ILogRecorder, AutoCloseable {
* @param info 日志
*/
public void append(String info, Object... vals) {
if (!this.close) {
if (writer == null) {
throw new LogRecorderCloseException();
}
writer.append(StrUtil.format(info, vals));
writer.flush();
} else {
throw new IllegalStateException("日志记录器未启用");
}
}
/**
@ -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);
}
}

View File

@ -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);

View File

@ -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()) {

View File

@ -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<String> agentExceptionHandler(HttpServletRequest request, AgentException e) {
Throwable cause = e.getCause();
if (cause != null) {

View File

@ -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;

View File

@ -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;

View File

@ -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<PageResultDto<UserLoginLogModel>> listLoginLogData(HttpServletRequest request) {
UserModel user = getUser();
PageResultDto<UserLoginLogModel> 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<PageResultDto<UserOperateLogV1>> listOperateLogData(HttpServletRequest request) {
UserModel user = getUser();
PageResultDto<UserOperateLogV1> 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<JSONObject> recentData(HttpServletRequest request) {
UserModel user = getUser();
JSONObject jsonObject = new JSONObject();

View File

@ -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;

View File

@ -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;