feat: 任务线模块

This commit is contained in:
2025-10-09 12:24:23 +08:00
parent c6eb668ac2
commit 49d86da3ea
28 changed files with 1269 additions and 10 deletions
@@ -0,0 +1,35 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.constant;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 任务状态(固定值)
*
* @author odboy
* @date 2025-09-26
*/
@Getter
@AllArgsConstructor
public enum TaskStatusEnum {
Running("running", "运行中"),
Success("success", "执行成功"),
Fail("fail", "执行失败");
private final String code;
private final String name;
}
@@ -0,0 +1,57 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.controller;
import cn.hutool.core.thread.ThreadUtil;
import cn.odboy.annotation.AnonymousAccess;
import cn.odboy.task.constant.TaskChangeTypeEnum;
import cn.odboy.task.core.TaskManage;
import cn.odboy.task.dal.dataobject.TaskInstanceInfoTb;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/api/task")
public class TaskTestsController {
@Autowired
private TaskManage taskManage;
@AnonymousAccess
@GetMapping(value = "/testCreate")
public ResponseEntity<?> testCreate() {
TaskInstanceInfoTb instanceInfo = taskManage.createJob("cutejava", TaskChangeTypeEnum.AppApply, "java", "daily", "cutejava", "功能测试", null);
log.info("任务创建成功,实例为:{}", JSON.toJSONString(instanceInfo));
return ResponseEntity.ok(instanceInfo);
}
@AnonymousAccess
@GetMapping(value = "/testCreateAfterStop")
public ResponseEntity<?> testCreateAfterStop() {
TaskInstanceInfoTb instanceInfo = taskManage.createJob("cutejava", TaskChangeTypeEnum.AppApply, "java", "daily", "cutejava", "功能测试", null);
log.info("任务创建成功,实例为:{}", JSON.toJSONString(instanceInfo));
ThreadUtil.execAsync(() -> {
ThreadUtil.safeSleep(5000);
taskManage.stopJob(instanceInfo.getId());
});
return ResponseEntity.ok(null);
}
}
@@ -0,0 +1,126 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core;
import cn.hutool.core.util.StrUtil;
import cn.odboy.framework.context.CsSpringBeanHolder;
import cn.odboy.task.dal.dataobject.TaskInstanceDetailTb;
import cn.odboy.task.dal.model.TaskTemplateInfoVo;
import cn.odboy.task.dal.model.TaskTemplateNodeVo;
import cn.odboy.task.service.TaskInstanceDetailService;
import cn.odboy.task.service.TaskInstanceInfoService;
import cn.odboy.task.service.TaskTemplateInfoService;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.quartz.InterruptableJob;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
public class TaskJobBean extends QuartzJobBean implements InterruptableJob {
private Thread subMainThread = null;
private String getBeanAlias(String code) {
// node_init
String[] s = code.split("_");
return Arrays.stream(s).map(StrUtil::upperFirst).collect(Collectors.joining());
}
@Override
public void executeInternal(JobExecutionContext context) {
this.subMainThread = Thread.currentThread();
// ========================== 获取代理类 ==========================
TaskInstanceInfoService taskInstanceInfoService = CsSpringBeanHolder.getBean(TaskInstanceInfoService.class);
TaskInstanceDetailService taskInstanceDetailService = CsSpringBeanHolder.getBean(TaskInstanceDetailService.class);
TaskTemplateInfoService taskTemplateInfoService = CsSpringBeanHolder.getBean(TaskTemplateInfoService.class);
// ========================== 获取参数 ==========================
JobDataMap dataMap = context.getMergedJobDataMap();
long id = dataMap.getLong("id");
// 应用名、资源类型
String contextName = dataMap.getString("contextName");
// 开发语言、资源版本
String language = dataMap.getString("language");
String envAlias = dataMap.getString("envAlias");
// ========================== 获取任务编排模板 ==========================
TaskTemplateInfoVo taskInstanceInfoVo = taskTemplateInfoService.getTemplateInfoByECL(envAlias, contextName, language);
if (taskInstanceInfoVo == null) {
taskInstanceInfoService.fastFailWithMessage(id, "没有查询到任务编排模板");
return;
}
String templateInfo = taskInstanceInfoVo.getTemplateInfo();
List<TaskTemplateNodeVo> taskTemplateNodeVos = JSON.parseArray(templateInfo, TaskTemplateNodeVo.class);
if (taskTemplateNodeVos == null || taskTemplateNodeVos.isEmpty()) {
taskInstanceInfoService.fastFailWithMessage(id, "没有查询到任务编排明细");
return;
}
// ========================== 初始化执行明细 ==========================
List<TaskInstanceDetailTb> taskInstanceDetails = new ArrayList<>();
for (TaskTemplateNodeVo taskTemplateNodeVo : taskTemplateNodeVos) {
TaskInstanceDetailTb taskInstanceDetail = new TaskInstanceDetailTb();
taskInstanceDetail.setInstanceId(id);
taskInstanceDetail.setFinishTime(null);
taskInstanceDetail.setBizCode(taskTemplateNodeVo.getCode());
taskInstanceDetail.setBizName(taskTemplateNodeVo.getName());
taskInstanceDetail.setExecuteInfo("未开始");
taskInstanceDetail.setExecuteStatus("pending");
taskInstanceDetails.add(taskInstanceDetail);
}
taskInstanceDetailService.saveBatch(taskInstanceDetails);
Map<String, Long> codeIdMap = taskInstanceDetails.stream().collect(Collectors.toMap(TaskInstanceDetailTb::getBizCode, TaskInstanceDetailTb::getId));
// ========================== 顺序执行 ==========================
try {
for (TaskTemplateNodeVo taskTemplateNodeVo : taskTemplateNodeVos) {
String code = taskTemplateNodeVo.getCode();
try {
TaskStepExecutor executor = CsSpringBeanHolder.getBean("taskStep" + getBeanAlias(code));
executor.execute(codeIdMap.getOrDefault(code, null), dataMap, taskTemplateNodeVo, new TaskStepCallback() {
@Override
public void onStart() {
taskInstanceDetailService.fastStart(id, code, dataMap);
}
@Override
public void onFinish(String executeInfo) {
taskInstanceDetailService.fastSuccessWithInfo(id, code, executeInfo);
}
});
} catch (Exception e) {
taskInstanceDetailService.fastFailWithInfo(id, code, e.getMessage());
throw new RuntimeException(e);
}
}
taskInstanceInfoService.fastSuccessWithData(id, dataMap);
} catch (Exception e) {
log.error("任务执行失败", e);
taskInstanceInfoService.fastFailWithMessageData(id, e.getMessage(), dataMap);
}
}
@Override
public void interrupt() {
if (subMainThread != null) {
subMainThread.stop();
}
}
}
@@ -0,0 +1,117 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core;
import cn.odboy.framework.exception.web.BadRequestException;
import cn.odboy.task.constant.TaskChangeTypeEnum;
import cn.odboy.task.constant.TaskStatusEnum;
import cn.odboy.task.dal.dataobject.TaskInstanceInfoTb;
import cn.odboy.task.service.TaskInstanceInfoService;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
@Slf4j
@Component
public class TaskManage {
@Resource
private Scheduler scheduler;
@Autowired
private TaskInstanceInfoService taskInstanceInfoService;
/**
* 创建任务单
*
* @param contextName 上下文名称,这里特指应用名
* @param changeType 变更类型
* @param envAlias 环境别名
* @param source 来源
* @param reason 变更原因
* @param dataMap 任务参数
*/
@Transactional(rollbackFor = Exception.class)
public TaskInstanceInfoTb createJob(String contextName, TaskChangeTypeEnum changeType, String language, String envAlias, String source, String reason, JobDataMap dataMap) {
if (dataMap == null) {
dataMap = new JobDataMap();
}
// ========================== 传递参数 ==========================
// 应用名、资源类型
dataMap.put("contextName", contextName);
// 开发语言、资源版本
dataMap.put("language", language);
dataMap.put("envAlias", envAlias);
// ========================== 创建任务 ==========================
TaskInstanceInfoTb newInstance = new TaskInstanceInfoTb();
newInstance.setContextName(contextName);
newInstance.setChangeType(changeType.getCode());
newInstance.setEnvAlias(envAlias);
newInstance.setStatus(TaskStatusEnum.Running.getCode());
newInstance.setFinishTime(null);
newInstance.setSource(source);
newInstance.setReason(reason);
newInstance.setJobData(JSON.toJSONString(dataMap));
taskInstanceInfoService.save(newInstance);
dataMap.put("id", newInstance.getId());
// ========================== 执行任务 ==========================
JobKey jobKey = JobKey.jobKey(changeType.getCode(), contextName);
TriggerKey triggerKey = TriggerKey.triggerKey(changeType.getCode(), contextName);
JobDetail jobDetail = JobBuilder.newJob(TaskJobBean.class).withIdentity(jobKey).usingJobData(dataMap).build();
Trigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).startNow().build();
try {
scheduler.scheduleJob(jobDetail, cronTrigger);
} catch (ObjectAlreadyExistsException e) {
throw new BadRequestException("任务已存在,跳过加载");
} catch (SchedulerException e) {
log.error("任务执行失败", e);
throw new BadRequestException(e);
}
return newInstance;
}
@Transactional(rollbackFor = Exception.class)
public void stopJob(Long instanceId) {
TaskInstanceInfoTb taskInstanceInfoTb = taskInstanceInfoService.getRunningById(instanceId);
String changeType = taskInstanceInfoTb.getChangeType();
String contextName = taskInstanceInfoTb.getContextName();
taskInstanceInfoTb.setStatus(TaskStatusEnum.Fail.getCode());
taskInstanceInfoTb.setErrorMessage("任务被中断");
taskInstanceInfoTb.setFinishTime(new Date());
taskInstanceInfoService.updateById(taskInstanceInfoTb);
try {
JobKey jobKey = JobKey.jobKey(changeType, contextName);
TriggerKey triggerKey = TriggerKey.triggerKey(changeType, contextName);
// 停止触发器
scheduler.resumeTrigger(triggerKey);
// 移除触发器
scheduler.unscheduleJob(triggerKey);
// 删除并中断任务
scheduler.deleteJob(jobKey);
scheduler.interrupt(jobKey);
} catch (SchedulerException e) {
log.error("任务停止失败", e);
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,22 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core;
public interface TaskStepCallback {
void onStart();
void onFinish(String executeInfo);
}
@@ -0,0 +1,32 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core;
import cn.odboy.framework.exception.web.BadRequestException;
import cn.odboy.task.dal.model.TaskTemplateNodeVo;
import org.quartz.JobDataMap;
import javax.validation.constraints.NotNull;
public interface TaskStepExecutor {
/**
* @param instanceDetailId 任务实例明细id
* @param jobDataMap 执行参数
* @param taskTemplateNode 任务节点信息
* @param callback 执行成功回调
*/
void execute(Long instanceDetailId, JobDataMap jobDataMap, TaskTemplateNodeVo taskTemplateNode, @NotNull TaskStepCallback callback) throws BadRequestException;
}
@@ -0,0 +1,37 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core.impl;
import cn.hutool.core.thread.ThreadUtil;
import cn.odboy.framework.exception.web.BadRequestException;
import cn.odboy.task.core.TaskStepCallback;
import cn.odboy.task.core.TaskStepExecutor;
import cn.odboy.task.dal.model.TaskTemplateNodeVo;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class TaskStepNodeBuildJava implements TaskStepExecutor {
@Override
public void execute(Long instanceDetailId, JobDataMap jobDataMap, TaskTemplateNodeVo taskTemplateNode, TaskStepCallback callback) throws BadRequestException {
callback.onStart();
ThreadUtil.sleep(2000);
callback.onFinish("执行成功");
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core.impl;
import cn.hutool.core.thread.ThreadUtil;
import cn.odboy.framework.exception.web.BadRequestException;
import cn.odboy.task.core.TaskStepCallback;
import cn.odboy.task.core.TaskStepExecutor;
import cn.odboy.task.dal.model.TaskTemplateNodeVo;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class TaskStepNodeDeployJava implements TaskStepExecutor {
@Override
public void execute(Long instanceDetailId, JobDataMap jobDataMap, TaskTemplateNodeVo taskTemplateNode, TaskStepCallback callback) throws BadRequestException {
callback.onStart();
ThreadUtil.sleep(5000);
callback.onFinish("执行成功");
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core.impl;
import cn.odboy.framework.exception.web.BadRequestException;
import cn.odboy.task.core.TaskStepCallback;
import cn.odboy.task.core.TaskStepExecutor;
import cn.odboy.task.dal.model.TaskTemplateNodeVo;
import cn.odboy.task.service.TaskInstanceStepDetailService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class TaskStepNodeInit implements TaskStepExecutor {
@Autowired
private TaskInstanceStepDetailService service;
@Override
public void execute(Long instanceDetailId, JobDataMap jobDataMap, TaskTemplateNodeVo taskTemplateNode, TaskStepCallback callback) throws BadRequestException {
callback.onStart();
service.success(instanceDetailId, "开始初始化");
service.success(instanceDetailId, "初始化完毕后,放入了一些参数");
jobDataMap.put("ExcuseMe", "Hello World!");
callback.onFinish("执行成功");
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.core.impl;
import cn.hutool.core.thread.ThreadUtil;
import cn.odboy.framework.exception.web.BadRequestException;
import cn.odboy.task.core.TaskStepCallback;
import cn.odboy.task.core.TaskStepExecutor;
import cn.odboy.task.dal.model.TaskTemplateNodeVo;
import cn.odboy.task.service.TaskInstanceStepDetailService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class TaskStepNodeMergeBranch implements TaskStepExecutor {
@Autowired
private TaskInstanceStepDetailService service;
@Override
public void execute(Long instanceDetailId, JobDataMap jobDataMap, TaskTemplateNodeVo taskTemplateNode, TaskStepCallback callback) throws BadRequestException {
callback.onStart();
ThreadUtil.sleep(3000);
service.fail(instanceDetailId, "执行失败测试");
callback.onFinish("执行成功");
}
}
@@ -16,7 +16,10 @@
package cn.odboy.task.dal.dataobject;
import cn.odboy.base.CsObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
@@ -48,6 +51,8 @@ public class TaskInstanceDetailTb extends CsObject {
*/
@ApiModelProperty("id")
@TableId(value = "id", type = IdType.ASSIGN_ID)
@JsonSerialize(using = ToStringSerializer.class)
@JSONField(format = "string")
private Long id;
/**
@@ -57,6 +62,13 @@ public class TaskInstanceDetailTb extends CsObject {
@ApiModelProperty("任务实例id")
private Long instanceId;
/**
* 开始时间
*/
@ApiModelProperty("开始时间")
@TableField("start_time")
private Date startTime;
/**
* 结束时间
*/
@@ -78,6 +90,13 @@ public class TaskInstanceDetailTb extends CsObject {
@ApiModelProperty("业务名称(步骤)")
private String bizName;
/**
* 执行参数
*/
@ApiModelProperty("执行参数")
@TableField("execute_params")
private String executeParams;
/**
* 执行信息
*/
@@ -16,7 +16,10 @@
package cn.odboy.task.dal.dataobject;
import cn.odboy.base.CsObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
@@ -54,10 +57,12 @@ public class TaskInstanceInfoTb extends CsObject {
private Date updateTime;
/**
* id
* id, @JsonSerialize和@JSONField,用于处理大数精度丢失问题
*/
@ApiModelProperty("id")
@TableId(value = "id", type = IdType.ASSIGN_ID)
@JsonSerialize(using = ToStringSerializer.class)
@JSONField(format = "string")
private Long id;
/**
@@ -109,17 +114,17 @@ public class TaskInstanceInfoTb extends CsObject {
@ApiModelProperty("变更原因")
private String reason;
/**
* QuartzJob的key名称
*/
@TableField("bind_job_key")
@ApiModelProperty("QuartzJob的key名称")
private String bindJobKey;
/**
* QuartzJob参数
*/
@TableField("bind_job_data")
@TableField("job_data")
@ApiModelProperty("QuartzJob参数")
private String bindJobData;
private String jobData;
/**
* 异常信息
*/
@TableField("error_message")
@ApiModelProperty("异常信息")
private String errorMessage;
}
@@ -0,0 +1,79 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.dal.dataobject;
import cn.odboy.base.CsObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Date;
/**
* <p>
* 任务实例步骤明细
* </p>
*
* @author codegen
* @since 2025-09-28
*/
@Getter
@Setter
@ToString
@TableName("task_instance_step_detail")
@ApiModel(value = "TaskInstanceStepDetailTb对象", description = "")
public class TaskInstanceStepDetailTb extends CsObject {
@TableField(fill = FieldFill.INSERT)
@ApiModelProperty(value = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true)
private Date createTime;
/**
* id
*/
@ApiModelProperty("id")
@TableId(value = "id", type = IdType.ASSIGN_ID)
@JsonSerialize(using = ToStringSerializer.class)
@JSONField(format = "string")
private Long id;
/**
* task_instance_detail表id
*/
@TableField("instance_detail_id")
@ApiModelProperty("task_instance_detail表id")
private Long instanceDetailId;
/**
* 步骤描述
*/
@TableField("step_desc")
@ApiModelProperty("步骤描述")
private String stepDesc;
/**
* 状态(success成功 fail失败)
*/
@TableField("step_status")
@ApiModelProperty("状态(success成功 fail失败)")
private String stepStatus;
}
@@ -0,0 +1,100 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.dal.dataobject;
import cn.odboy.base.CsBaseUserTimeTb;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* <p>
* 任务模板
* </p>
*
* @author codegen
* @since 2025-09-28
*/
@Getter
@Setter
@ToString
@TableName("task_template_info")
@ApiModel(value = "TaskTemplateInfoTb对象", description = "任务模板")
public class TaskTemplateInfoTb extends CsBaseUserTimeTb {
/**
* id
*/
@ApiModelProperty("id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 数据有效性
*/
@TableField("available")
@ApiModelProperty("数据有效性")
private Boolean available;
/**
* 变更类型
*/
@ApiModelProperty("变更类型")
@TableField("change_type")
private String changeType;
/**
* 流水线名称
*/
@TableField("`name`")
@ApiModelProperty("流水线名称")
private String name;
/**
* 流水线描述
*/
@ApiModelProperty("流水线描述")
@TableField("`description`")
private String description;
/**
* 流水线模板内容
*/
@TableField("template")
@ApiModelProperty("流水线模板内容")
private String template;
/**
* 环境别名
*/
@TableField("env_alias")
@ApiModelProperty("环境别名")
private String envAlias;
/**
* 应用为语言,资源为类型
*/
@TableField("`language`")
@ApiModelProperty("应用为语言,资源为类型")
private String language;
}
@@ -0,0 +1,32 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.dal.model;
import cn.odboy.base.CsObject;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class TaskTemplateInfoVo extends CsObject {
private String changeType;
private String name;
private String description;
private String envAlias;
private String language;
private String contextName;
private String templateInfo;
}
@@ -0,0 +1,50 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.dal.model;
import cn.odboy.base.CsObject;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.Map;
/**
* 流水线节点控制按钮
*
* @author odboy
*/
@Getter
@Setter
public class TaskTemplateNodeOperateButtonVo extends CsObject {
/**
* 按钮类型(service:调用某个服务并传递参数 link:带参跳转)
*/
private String type;
/**
* 按钮标题
*/
private String title;
/**
* 按钮传递的参数
*/
private String code;
/**
* 默认附加参数
*/
private Map<String, String> parameters = new HashMap<>();
}
@@ -0,0 +1,68 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.dal.model;
import cn.odboy.base.CsObject;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 任务节点模板
*
* @author odboy
*/
@Getter
@Setter
public class TaskTemplateNodeVo extends CsObject {
/**
* 业务编码
*/
protected String code;
/**
* 业务类型(service:系统内置服务 rpc:远程调用)
*/
protected String type;
/**
* 业务名称
*/
protected String name;
/**
* 是否可点击
*/
protected Boolean click = false;
/**
* 是否可重试
*/
protected Boolean retry = false;
/**
* 是否可点击:点击展示详情,详情内容类型
*/
protected String detailType = "";
/**
* 默认参数<br/> execute: 执行调用 describe: 明细调用
*/
protected Map<String, String> parameters = new HashMap<>();
/**
* 流水线节点控制按钮
*/
protected List<TaskTemplateNodeOperateButtonVo> buttons = new ArrayList<>();
}
@@ -0,0 +1,33 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.dal.mysql;
import cn.odboy.task.dal.dataobject.TaskInstanceStepDetailTb;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* <p>
* 任务实例步骤明细 Mapper 接口
* </p>
*
* @author codegen
* @since 2025-09-28
*/
@Mapper
public interface TaskInstanceStepDetailMapper extends BaseMapper<TaskInstanceStepDetailTb> {
}
@@ -0,0 +1,35 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.dal.mysql;
import cn.odboy.task.dal.dataobject.TaskTemplateInfoTb;
import cn.odboy.task.dal.model.TaskTemplateInfoVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* <p>
* 任务模板 Mapper 接口
* </p>
*
* @author codegen
* @since 2025-09-28
*/
@Mapper
public interface TaskTemplateInfoMapper extends BaseMapper<TaskTemplateInfoTb> {
TaskTemplateInfoVo selectTemplateInfoByECL(@Param("envAlias") String envAlias, @Param("contextName") String contextName, @Param("language") String language);
}
@@ -17,6 +17,7 @@ package cn.odboy.task.service;
import cn.odboy.task.dal.dataobject.TaskInstanceDetailTb;
import com.baomidou.mybatisplus.extension.service.IService;
import org.quartz.JobDataMap;
/**
* <p>
@@ -27,5 +28,9 @@ import com.baomidou.mybatisplus.extension.service.IService;
* @since 2025-09-26
*/
public interface TaskInstanceDetailService extends IService<TaskInstanceDetailTb> {
void fastFailWithInfo(Long instanceId, String code, String message);
void fastSuccessWithInfo(Long id, String code, String executeInfo);
void fastStart(Long id, String code, JobDataMap dataMap);
}
@@ -17,6 +17,7 @@ package cn.odboy.task.service;
import cn.odboy.task.dal.dataobject.TaskInstanceInfoTb;
import com.baomidou.mybatisplus.extension.service.IService;
import org.quartz.JobDataMap;
/**
* <p>
@@ -27,5 +28,12 @@ import com.baomidou.mybatisplus.extension.service.IService;
* @since 2025-09-26
*/
public interface TaskInstanceInfoService extends IService<TaskInstanceInfoTb> {
TaskInstanceInfoTb getRunningById(Long instanceId);
void fastFailWithMessage(Long id, String errorMessage);
void fastFailWithMessageData(Long id, String errorMessage, JobDataMap dataMap);
void fastSuccessWithData(Long id, JobDataMap dataMap);
}
@@ -0,0 +1,45 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.service;
import cn.odboy.task.dal.dataobject.TaskInstanceStepDetailTb;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 任务实例步骤明细 服务类
* </p>
*
* @author codegen
* @since 2025-09-28
*/
public interface TaskInstanceStepDetailService extends IService<TaskInstanceStepDetailTb> {
/**
* 步骤执行成功
*
* @param instanceDetailId 任务实例明细id
* @param stepDesc 步骤描述
*/
void success(Long instanceDetailId, String stepDesc);
/**
* 步骤执行失败
*
* @param instanceDetailId 任务实例明细id
* @param stepDesc 步骤描述
*/
void fail(Long instanceDetailId, String stepDesc);
}
@@ -0,0 +1,32 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.service;
import cn.odboy.task.dal.dataobject.TaskTemplateInfoTb;
import cn.odboy.task.dal.model.TaskTemplateInfoVo;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 任务模板 服务类
* </p>
*
* @author codegen
* @since 2025-09-28
*/
public interface TaskTemplateInfoService extends IService<TaskTemplateInfoTb> {
TaskTemplateInfoVo getTemplateInfoByECL(String envAlias, String contextName, String language);
}
@@ -15,12 +15,17 @@
*/
package cn.odboy.task.service.impl;
import cn.odboy.task.constant.TaskStatusEnum;
import cn.odboy.task.dal.dataobject.TaskInstanceDetailTb;
import cn.odboy.task.dal.mysql.TaskInstanceDetailMapper;
import cn.odboy.task.service.TaskInstanceDetailService;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.quartz.JobDataMap;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* <p>
* 任务实例明细 服务实现类
@@ -32,4 +37,39 @@ import org.springframework.stereotype.Service;
@Service
public class TaskInstanceDetailServiceImpl extends ServiceImpl<TaskInstanceDetailMapper, TaskInstanceDetailTb> implements TaskInstanceDetailService {
@Override
public void fastFailWithInfo(Long instanceId, String code, String executeInfo) {
TaskInstanceDetailTb updRecord = new TaskInstanceDetailTb();
updRecord.setExecuteInfo(executeInfo);
updRecord.setExecuteStatus(TaskStatusEnum.Fail.getCode());
lambdaUpdate()
.eq(TaskInstanceDetailTb::getInstanceId, instanceId)
.eq(TaskInstanceDetailTb::getBizCode, code)
.update(updRecord);
}
@Override
public void fastSuccessWithInfo(Long instanceId, String code, String executeInfo) {
TaskInstanceDetailTb updRecord = new TaskInstanceDetailTb();
updRecord.setFinishTime(new Date());
updRecord.setExecuteInfo(executeInfo);
updRecord.setExecuteStatus(TaskStatusEnum.Success.getCode());
lambdaUpdate()
.eq(TaskInstanceDetailTb::getInstanceId, instanceId)
.eq(TaskInstanceDetailTb::getBizCode, code)
.update(updRecord);
}
@Override
public void fastStart(Long instanceId, String code, JobDataMap dataMap) {
TaskInstanceDetailTb updRecord = new TaskInstanceDetailTb();
updRecord.setStartTime(new Date());
updRecord.setExecuteInfo("运行中");
updRecord.setExecuteParams(JSON.toJSONString(dataMap));
updRecord.setExecuteStatus(TaskStatusEnum.Running.getCode());
lambdaUpdate()
.eq(TaskInstanceDetailTb::getInstanceId, instanceId)
.eq(TaskInstanceDetailTb::getBizCode, code)
.update(updRecord);
}
}
@@ -15,11 +15,17 @@
*/
package cn.odboy.task.service.impl;
import cn.odboy.task.constant.TaskStatusEnum;
import cn.odboy.task.dal.dataobject.TaskInstanceInfoTb;
import cn.odboy.task.dal.mysql.TaskInstanceInfoMapper;
import cn.odboy.task.service.TaskInstanceInfoService;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.quartz.JobDataMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
/**
* <p>
@@ -32,4 +38,46 @@ import org.springframework.stereotype.Service;
@Service
public class TaskInstanceInfoServiceImpl extends ServiceImpl<TaskInstanceInfoMapper, TaskInstanceInfoTb> implements TaskInstanceInfoService {
@Override
public TaskInstanceInfoTb getRunningById(Long id) {
return lambdaQuery()
.eq(TaskInstanceInfoTb::getId, id)
.eq(TaskInstanceInfoTb::getStatus, TaskStatusEnum.Running.getCode())
.orderByDesc(TaskInstanceInfoTb::getId)
.one();
}
@Override
@Transactional(rollbackFor = Exception.class)
public void fastFailWithMessage(Long id, String errorMessage) {
TaskInstanceInfoTb updRecord = new TaskInstanceInfoTb();
updRecord.setId(id);
updRecord.setFinishTime(new Date());
updRecord.setStatus(TaskStatusEnum.Fail.getCode());
updRecord.setErrorMessage(errorMessage);
updateById(updRecord);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void fastFailWithMessageData(Long id, String errorMessage, JobDataMap dataMap) {
TaskInstanceInfoTb updRecord = new TaskInstanceInfoTb();
updRecord.setId(id);
updRecord.setFinishTime(new Date());
updRecord.setStatus(TaskStatusEnum.Fail.getCode());
updRecord.setErrorMessage(errorMessage);
updRecord.setJobData(JSON.toJSONString(dataMap));
updateById(updRecord);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void fastSuccessWithData(Long id, JobDataMap dataMap) {
TaskInstanceInfoTb updRecord = new TaskInstanceInfoTb();
updRecord.setId(id);
updRecord.setFinishTime(new Date());
updRecord.setStatus(TaskStatusEnum.Success.getCode());
updRecord.setJobData(JSON.toJSONString(dataMap));
updateById(updRecord);
}
}
@@ -0,0 +1,55 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.service.impl;
import cn.odboy.framework.exception.web.BadRequestException;
import cn.odboy.task.constant.TaskStatusEnum;
import cn.odboy.task.dal.dataobject.TaskInstanceStepDetailTb;
import cn.odboy.task.dal.mysql.TaskInstanceStepDetailMapper;
import cn.odboy.task.service.TaskInstanceStepDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 任务实例步骤明细 服务实现类
* </p>
*
* @author codegen
* @since 2025-09-28
*/
@Service
public class TaskInstanceStepDetailServiceImpl extends ServiceImpl<TaskInstanceStepDetailMapper, TaskInstanceStepDetailTb> implements TaskInstanceStepDetailService {
@Override
public void success(Long instanceDetailId, String stepDesc) {
TaskInstanceStepDetailTb stepDetail = new TaskInstanceStepDetailTb();
stepDetail.setInstanceDetailId(instanceDetailId);
stepDetail.setStepDesc(stepDesc);
stepDetail.setStepStatus(TaskStatusEnum.Success.getCode());
save(stepDetail);
}
@Override
public void fail(Long instanceDetailId, String stepDesc) {
TaskInstanceStepDetailTb stepDetail = new TaskInstanceStepDetailTb();
stepDetail.setInstanceDetailId(instanceDetailId);
stepDetail.setStepDesc(stepDesc);
stepDetail.setStepStatus(TaskStatusEnum.Fail.getCode());
save(stepDetail);
throw new BadRequestException(stepDesc);
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2021-2025 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.task.service.impl;
import cn.odboy.task.dal.dataobject.TaskTemplateInfoTb;
import cn.odboy.task.dal.model.TaskTemplateInfoVo;
import cn.odboy.task.dal.mysql.TaskTemplateInfoMapper;
import cn.odboy.task.service.TaskTemplateInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 任务模板 服务实现类
* </p>
*
* @author codegen
* @since 2025-09-28
*/
@Service
public class TaskTemplateInfoServiceImpl extends ServiceImpl<TaskTemplateInfoMapper, TaskTemplateInfoTb> implements TaskTemplateInfoService {
@Override
public TaskTemplateInfoVo getTemplateInfoByECL(String envAlias, String contextName, String language) {
return baseMapper.selectTemplateInfoByECL(envAlias, contextName, language);
}
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.odboy.task.dal.mysql.TaskTemplateInfoMapper">
<select id="selectTemplateInfoByECL" resultType="cn.odboy.task.dal.model.TaskTemplateInfoVo">
SELECT t2.change_type,
t2.`name`,
t2.description,
t2.env_alias,
t2.`language`,
t1.context_name,
t1.template_info
FROM task_pipeline_config t1
LEFT JOIN task_template_info t2 ON t1.template_info_id = t2.id
WHERE t1.context_name = #{contextName}
AND t2.env_alias = #{envAlias}
AND t2.`language` = #{language}
</select>
</mapper>