worker common 模块
This commit is contained in:
parent
d48e2225b6
commit
ee9ac3b7b4
@ -38,4 +38,9 @@ public class BuildParameters {
|
||||
this.desc = desc;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public BuildParameters(String key, String value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package cd.casic.ci.process.api.process.pojo;
|
||||
|
||||
import cd.casic.ci.common.pipeline.enums.BuildTaskStatus;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -14,6 +15,7 @@ import java.util.Map;
|
||||
* @Filename:BuildTask
|
||||
* @description:Todo
|
||||
*/
|
||||
@Data
|
||||
@Schema(title = "流水线模型-构建任务")
|
||||
public class BuildTask {
|
||||
@Schema(title = "构建ID", required = true)
|
||||
|
@ -0,0 +1,127 @@
|
||||
package cd.casic.devops.common.worker.task;
|
||||
import cd.casic.ci.common.pipeline.enums.ErrorCode;
|
||||
import cd.casic.ci.common.pipeline.pojo.ErrorType;
|
||||
import cd.casic.ci.process.api.process.pojo.BuildTask;
|
||||
import cd.casic.ci.process.api.process.pojo.BuildTaskResult;
|
||||
import cd.casic.ci.process.api.process.pojo.BuildVariables;
|
||||
import com.github.xiaoymin.knife4j.core.util.CommonUtils;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static cd.casic.ci.process.api.process.utils.Constants.PIPELINE_TASK_MESSAGE_STRING_LENGTH_MAX;
|
||||
|
||||
public class TaskDaemon implements Callable<Map<String, String>> {
|
||||
private final ITask task;
|
||||
private final BuildTask buildTask;
|
||||
private final BuildVariables buildVariables;
|
||||
private final File workspace;
|
||||
private static final int PARAM_MAX_LENGTH = 4000; // 流水线参数最大长度
|
||||
|
||||
public TaskDaemon(ITask task, BuildTask buildTask, BuildVariables buildVariables, File workspace) {
|
||||
this.task = task;
|
||||
this.buildTask = buildTask;
|
||||
this.buildVariables = buildVariables;
|
||||
this.workspace = workspace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> call() {
|
||||
task.run(buildTask, buildVariables, workspace);
|
||||
return task.getAllEnv();
|
||||
}
|
||||
|
||||
public void runWithTimeout() {
|
||||
int timeout = TaskUtil.getTimeOut(buildTask);
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
String taskId = buildTask.getTaskId();
|
||||
|
||||
if (taskId != null) {
|
||||
TaskExecutorCache.put(taskId, executor);
|
||||
}
|
||||
Future<Map<String, String>> f1 = executor.submit(this);
|
||||
try {
|
||||
if (f1.get(timeout, TimeUnit.MINUTES) == null) {
|
||||
throw new TimeoutException("Task[" + buildTask.getElementName() +
|
||||
"] timeout: " + timeout + " minutes");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new TaskExecuteException(
|
||||
ErrorType.USER,
|
||||
ErrorCode.USER_TASK_OUTTIME_LIMIT,
|
||||
e.getMessage() != null ? e.getMessage() :
|
||||
"Task[" + buildTask.getElementName() + "] timeout: " + timeout + " minutes"
|
||||
);
|
||||
} finally {
|
||||
executor.shutdownNow();
|
||||
if (taskId != null) {
|
||||
TaskExecutorCache.invalidate(taskId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> getAllEnv() {
|
||||
return task.getAllEnv();
|
||||
}
|
||||
|
||||
private Map<String, Object> getMonitorData() {
|
||||
return task.getMonitorData();
|
||||
}
|
||||
|
||||
public BuildTaskResult getBuildResult(
|
||||
Boolean isSuccess,
|
||||
String errorMessage,
|
||||
String errorType,
|
||||
Integer errorCode) {
|
||||
Map<String, String> allEnv = getAllEnv();
|
||||
Map<String, String> buildResult = new HashMap<>();
|
||||
|
||||
if (allEnv != null && !allEnv.isEmpty()) {
|
||||
for (Map.Entry<String, String> entry : allEnv.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
String value = entry.getValue();
|
||||
|
||||
// 检查值长度
|
||||
if (value != null && value.length() > PARAM_MAX_LENGTH) {
|
||||
LoggerService.getInstance().addWarnLine(
|
||||
"Warning, assignment to variable [" + key + "] failed, " +
|
||||
"more than " + PARAM_MAX_LENGTH + " characters(len=" + value.length() + ")"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (SensitiveValueService.matchSensitiveValue(value)) {
|
||||
LoggerService.getInstance().addWarnLine(
|
||||
"Warning, credentials cannot be assigned to variable[" + key + "]"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
buildResult.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
return new BuildTaskResult(
|
||||
buildTask.getTaskId(),
|
||||
buildTask.getTaskId(),
|
||||
buildVariables.getContainerHashId(),
|
||||
buildTask.getElementVersion(),
|
||||
isSuccess,
|
||||
buildTask.getExecuteCount(),
|
||||
buildResult,
|
||||
errorMessage != null ?
|
||||
CommonUtils.interceptStringInLength(
|
||||
SensitiveValueService.fixSensitiveContent(errorMessage),
|
||||
PIPELINE_TASK_MESSAGE_STRING_LENGTH_MAX
|
||||
) : null,
|
||||
buildTask.getType(),
|
||||
errorType,
|
||||
errorCode,
|
||||
task.getPlatformCode(),
|
||||
task.getPlatformErrorCode(),
|
||||
getMonitorData()
|
||||
);
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user