删除掉所有没用的工作流相关的类,重新开发。

This commit is contained in:
dengqichen 2024-12-10 12:27:01 +08:00
parent 26b0fb6a3e
commit 412a7e5c91
3 changed files with 95 additions and 156 deletions

View File

@ -126,10 +126,6 @@ public class ShellTaskDelegate implements JavaDelegate {
output.append(line).append("\n"); output.append(line).append("\n");
} }
log.info("Shell output: {}", line); log.info("Shell output: {}", line);
// 使用RuntimeService更新变量
runtimeService.setVariable(execution.getProcessInstanceId(),
executionId + "_shellOutput", output.toString());
Thread.sleep(500);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Error reading process output", e); log.error("Error reading process output", e);
@ -146,9 +142,6 @@ public class ShellTaskDelegate implements JavaDelegate {
error.append(line).append("\n"); error.append(line).append("\n");
} }
log.error("Shell error: {}", line); log.error("Shell error: {}", line);
// 使用RuntimeService更新变量
runtimeService.setVariable(execution.getProcessInstanceId(),
executionId + "_shellError", error.toString());
} }
} catch (IOException e) { } catch (IOException e) {
log.error("Error reading process error", e); log.error("Error reading process error", e);

View File

@ -104,20 +104,5 @@ public class WorkflowExecutionDTO {
* 结束时间 * 结束时间
*/ */
private Date endTime; private Date endTime;
/**
* Shell任务的输出如果是 SHELL_TASK 类型
*/
private String output;
/**
* Shell任务的错误信息如果是 SHELL_TASK 类型
*/
private String error;
/**
* Shell任务的退出码如果是 SHELL_TASK 类型
*/
private Integer exitCode;
} }
} }

View File

@ -10,6 +10,8 @@ import com.qqchen.deploy.backend.workflow.repository.IWorkflowDefinitionReposito
import com.qqchen.deploy.backend.workflow.service.IWorkflowDefinitionService; import com.qqchen.deploy.backend.workflow.service.IWorkflowDefinitionService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.bpmn.model.StartEvent;
import org.flowable.engine.HistoryService; import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService; import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService; import org.flowable.engine.RuntimeService;
@ -17,6 +19,7 @@ import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Deployment; import org.flowable.engine.repository.Deployment;
import org.flowable.bpmn.model.BpmnModel; import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance; import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.bpmn.model.Process; import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.FlowElement; import org.flowable.bpmn.model.FlowElement;
@ -133,6 +136,9 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
@Override @Override
public WorkflowExecutionDTO getWorkflowExecution(String processInstanceId) { public WorkflowExecutionDTO getWorkflowExecution(String processInstanceId) {
try { try {
log.info("=== 开始获取工作流执行状态 ===");
log.info("流程实例ID: {}", processInstanceId);
// 1. 获取流程实例信息 // 1. 获取流程实例信息
ProcessInstance runningInstance = runtimeService.createProcessInstanceQuery() ProcessInstance runningInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId) .processInstanceId(processInstanceId)
@ -146,24 +152,15 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
throw new RuntimeException("Process instance not found: " + processInstanceId); throw new RuntimeException("Process instance not found: " + processInstanceId);
} }
log.info("=== 开始获取工作流执行状态 ===");
log.info("流程实例ID: {}", processInstanceId);
log.info("流程定义ID: {}", historicInstance.getProcessDefinitionId()); log.info("流程定义ID: {}", historicInstance.getProcessDefinitionId());
log.info("是否有运行实例: {}", runningInstance != null); log.info("流程是否在运行: {}", runningInstance != null);
log.info("流程开始时间: {}", historicInstance.getStartTime());
log.info("流程结束时间: {}", historicInstance.getEndTime());
// 2. 获取流程定义 // 2. 获取流程定义
BpmnModel bpmnModel = repositoryService.getBpmnModel(historicInstance.getProcessDefinitionId()); BpmnModel bpmnModel = repositoryService.getBpmnModel(historicInstance.getProcessDefinitionId());
Process process = bpmnModel.getMainProcess(); Process process = bpmnModel.getMainProcess();
// 打印流程定义中的所有节点
log.info("=== 流程定义节点信息 ===");
process.getFlowElements().forEach(element -> {
log.info("节点信息 - ID: {}, 名称: {}, 类型: {}",
element.getId(),
element.getName(),
element.getClass().getSimpleName());
});
// 3. 获取所有历史活动实例 // 3. 获取所有历史活动实例
List<HistoricActivityInstance> historicActivities = historyService.createHistoricActivityInstanceQuery() List<HistoricActivityInstance> historicActivities = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId) .processInstanceId(processInstanceId)
@ -172,34 +169,38 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
.list(); .list();
log.info("=== 历史活动实例信息 ==="); log.info("=== 历史活动实例信息 ===");
historicActivities.forEach(activity -> { for (HistoricActivityInstance activity : historicActivities) {
log.info("历史活动 - ID: {}, 名称: {}, 类型: {}, 开始时间: {}, 结束时间: {}", log.info("节点ID: {}, 名称: {}, 类型: {}, 开始时间: {}, 结束时间: {}, 执行ID: {}",
activity.getActivityId(), activity.getActivityId(),
activity.getActivityName(), activity.getActivityName(),
activity.getActivityType(), activity.getActivityType(),
activity.getStartTime(), activity.getStartTime(),
activity.getEndTime()); activity.getEndTime(),
}); activity.getExecutionId());
// 4. 获取当前活动的节点
List<String> activeActivityIds = new ArrayList<>();
if (runningInstance != null) {
activeActivityIds = runtimeService.getActiveActivityIds(processInstanceId);
log.info("=== 当前活动节点 ===");
log.info("活动节点IDs: {}", activeActivityIds);
} }
// 5. 获取所有流程变量 // 4. 获取当前正在执行的活动节点
List<HistoricVariableInstance> variables = historyService.createHistoricVariableInstanceQuery() List<String> activeActivityIds = new ArrayList<>();
.processInstanceId(processInstanceId) if (runningInstance != null) {
.list(); List<Execution> executions = runtimeService.createExecutionQuery()
.processInstanceId(processInstanceId)
.list();
log.info("=== 流程变量信息 ==="); for (Execution execution : executions) {
Map<String, Object> variableMap = new HashMap<>(); if (execution.getActivityId() != null) {
variables.forEach(variable -> { activeActivityIds.add(execution.getActivityId());
variableMap.put(variable.getVariableName(), variable.getValue()); }
log.info("变量 - 名称: {}, 值: {}", variable.getVariableName(), variable.getValue()); }
}); log.info("当前活动节点IDs: {}", activeActivityIds);
}
// 5. 构建历史活动实例映射
Map<String, HistoricActivityInstance> historicActivityMap = historicActivities.stream()
.collect(Collectors.toMap(
HistoricActivityInstance::getActivityId,
activity -> activity,
(existing, replacement) -> existing
));
// 6. 构建执行状态DTO // 6. 构建执行状态DTO
WorkflowExecutionDTO executionDTO = new WorkflowExecutionDTO(); WorkflowExecutionDTO executionDTO = new WorkflowExecutionDTO();
@ -210,9 +211,8 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
executionDTO.setStartTime(historicInstance.getStartTime()); executionDTO.setStartTime(historicInstance.getStartTime());
executionDTO.setEndTime(historicInstance.getEndTime()); executionDTO.setEndTime(historicInstance.getEndTime());
executionDTO.setDurationInMillis(historicInstance.getDurationInMillis()); executionDTO.setDurationInMillis(historicInstance.getDurationInMillis());
executionDTO.setVariables(variableMap);
// 设置状态 // 设置流程状态
if (historicInstance.getEndTime() != null) { if (historicInstance.getEndTime() != null) {
executionDTO.setStatus(historicInstance.getDeleteReason() == null ? executionDTO.setStatus(historicInstance.getDeleteReason() == null ?
WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED); WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED);
@ -221,117 +221,78 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
WorkflowInstanceStatus.SUSPENDED : WorkflowInstanceStatus.RUNNING); WorkflowInstanceStatus.SUSPENDED : WorkflowInstanceStatus.RUNNING);
} }
// 7. 创建历史活动实例的映射用于快速查找 // 7. 构建阶段列表
Map<String, HistoricActivityInstance> historicActivityMap = historicActivities.stream()
.collect(Collectors.toMap(
HistoricActivityInstance::getActivityId,
activity -> activity,
(existing, replacement) -> existing
));
// 8. 获取所有流程节点并构建阶段列表
List<WorkflowExecutionDTO.StageDTO> stages = new ArrayList<>(); List<WorkflowExecutionDTO.StageDTO> stages = new ArrayList<>();
Collection<FlowElement> flowElements = process.getFlowElements(); List<FlowElement> flowElements = process.getFlowElements().stream()
.filter(element -> !(element instanceof SequenceFlow))
.collect(Collectors.toList());
// 记录已执行过的节点ID log.info("=== 开始构建节点状态 ===");
Set<String> executedActivityIds = historicActivities.stream()
.map(HistoricActivityInstance::getActivityId)
.collect(Collectors.toSet());
// 获取所有节点的列表按照流程定义的顺序
List<FlowElement> orderedElements = new ArrayList<>();
for (FlowElement element : flowElements) { for (FlowElement element : flowElements) {
if (element instanceof Task || element instanceof Event || element instanceof ServiceTask) {
orderedElements.add(element);
}
}
// 遍历所有节点
for (int i = 0; i < orderedElements.size(); i++) {
FlowElement element = orderedElements.get(i);
WorkflowExecutionDTO.StageDTO stage = new WorkflowExecutionDTO.StageDTO(); WorkflowExecutionDTO.StageDTO stage = new WorkflowExecutionDTO.StageDTO();
stage.setId(element.getId()); stage.setId(element.getId());
stage.setName(element.getName()); stage.setName(element.getName());
stage.setType(element.getClass().getSimpleName());
// 设置节点类型
if (element instanceof ServiceTask) {
stage.setType("serviceTask");
} else if (element instanceof Event) {
stage.setType("startEvent");
if (element.getId().equals("end")) {
stage.setType("endEvent");
}
} else {
stage.setType(element.getClass().getSimpleName());
}
// 获取历史活动实例
HistoricActivityInstance historicActivity = historicActivityMap.get(element.getId()); HistoricActivityInstance historicActivity = historicActivityMap.get(element.getId());
// 设置开始和结束时间 // 确定节点状态
if (historicActivity != null) { determineStageStatus(stage, element, historicActivity, activeActivityIds);
stage.setStartTime(historicActivity.getStartTime());
stage.setEndTime(historicActivity.getEndTime());
}
// 设置节点状态 log.info("节点[{}] - 名称: {}, 类型: {}, 状态: {}, 开始时间: {}, 结束时间: {}",
if (historicActivity != null) { stage.getId(),
if (historicActivity.getEndTime() != null) { stage.getName(),
// 已完成的节点 stage.getType(),
stage.setStatus(historicActivity.getDeleteReason() == null ? stage.getStatus(),
WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED); stage.getStartTime(),
} else if (activeActivityIds.contains(element.getId())) { stage.getEndTime());
// 正在执行的节点
stage.setStatus(WorkflowInstanceStatus.RUNNING);
} else {
// 已开始但未完成的节点
stage.setStatus(WorkflowInstanceStatus.RUNNING);
}
} else {
// 未执行的节点
if (i == 0 && executedActivityIds.isEmpty()) {
// 如果是第一个节点且没有任何节点执行过设置为RUNNING
stage.setStatus(WorkflowInstanceStatus.RUNNING);
} else if (i > 0 && stages.get(i-1).getStatus() == WorkflowInstanceStatus.COMPLETED) {
// 如果前一个节点已完成当前节点设置为RUNNING
stage.setStatus(WorkflowInstanceStatus.RUNNING);
} else {
// 其他情况设置为NOT_STARTED
stage.setStatus(WorkflowInstanceStatus.NOT_STARTED);
}
}
// 如果是Shell任务获取执行结果
if (element instanceof ServiceTask) {
ServiceTask serviceTask = (ServiceTask) element;
if ("shell".equals(serviceTask.getType()) && historicActivity != null) {
String executionId = historicActivity.getExecutionId();
stage.setOutput((String) variableMap.get(executionId + "_shellOutput"));
stage.setError((String) variableMap.get(executionId + "_shellError"));
stage.setExitCode((Integer) variableMap.get(executionId + "_exitCode"));
}
}
stages.add(stage); stages.add(stage);
} }
executionDTO.setStages(stages); executionDTO.setStages(stages);
// 10. 设置当前活动节点
if (!activeActivityIds.isEmpty()) {
String currentActivityId = activeActivityIds.get(0);
FlowElement currentElement = process.getFlowElement(currentActivityId);
if (currentElement != null) {
executionDTO.setCurrentActivityId(currentActivityId);
executionDTO.setCurrentActivityName(currentElement.getName());
executionDTO.setCurrentActivityType(currentElement.getClass().getSimpleName());
}
}
return executionDTO; return executionDTO;
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to get workflow execution: {}", processInstanceId, e); log.error("获取工作流执行状态失败", e);
throw new RuntimeException("Failed to get workflow execution", e); throw new RuntimeException("Failed to get workflow execution status", e);
}
}
/**
* 确定节点状态
*/
private void determineStageStatus(
WorkflowExecutionDTO.StageDTO stage,
FlowElement element,
HistoricActivityInstance historicActivity,
List<String> activeActivityIds
) {
if (historicActivity == null) {
// 节点未执行过
stage.setStatus(WorkflowInstanceStatus.NOT_STARTED);
stage.setStartTime(null);
stage.setEndTime(null);
return;
}
// 设置时间
stage.setStartTime(historicActivity.getStartTime());
stage.setEndTime(historicActivity.getEndTime());
// 确定状态
if (historicActivity.getEndTime() != null) {
// 节点已结束
stage.setStatus(historicActivity.getDeleteReason() == null ?
WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED);
} else if (element instanceof StartEvent) {
// 开始节点特殊处理有开始时间就视为完成
stage.setStatus(WorkflowInstanceStatus.COMPLETED);
} else if (activeActivityIds.contains(element.getId())) {
// 节点正在执行
stage.setStatus(WorkflowInstanceStatus.RUNNING);
} else {
// 其他情况节点已开始但未完成且不在当前活动节点列表中
stage.setStatus(WorkflowInstanceStatus.NOT_STARTED);
} }
} }