删除掉所有没用的工作流相关的类,重新开发。
This commit is contained in:
parent
26b0fb6a3e
commit
412a7e5c91
@ -126,10 +126,6 @@ public class ShellTaskDelegate implements JavaDelegate {
|
||||
output.append(line).append("\n");
|
||||
}
|
||||
log.info("Shell output: {}", line);
|
||||
// 使用RuntimeService更新变量
|
||||
runtimeService.setVariable(execution.getProcessInstanceId(),
|
||||
executionId + "_shellOutput", output.toString());
|
||||
Thread.sleep(500);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error reading process output", e);
|
||||
@ -146,9 +142,6 @@ public class ShellTaskDelegate implements JavaDelegate {
|
||||
error.append(line).append("\n");
|
||||
}
|
||||
log.error("Shell error: {}", line);
|
||||
// 使用RuntimeService更新变量
|
||||
runtimeService.setVariable(execution.getProcessInstanceId(),
|
||||
executionId + "_shellError", error.toString());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error reading process error", e);
|
||||
|
||||
@ -104,20 +104,5 @@ public class WorkflowExecutionDTO {
|
||||
* 结束时间
|
||||
*/
|
||||
private Date endTime;
|
||||
|
||||
/**
|
||||
* Shell任务的输出(如果是 SHELL_TASK 类型)
|
||||
*/
|
||||
private String output;
|
||||
|
||||
/**
|
||||
* Shell任务的错误信息(如果是 SHELL_TASK 类型)
|
||||
*/
|
||||
private String error;
|
||||
|
||||
/**
|
||||
* Shell任务的退出码(如果是 SHELL_TASK 类型)
|
||||
*/
|
||||
private Integer exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,8 @@ import com.qqchen.deploy.backend.workflow.repository.IWorkflowDefinitionReposito
|
||||
import com.qqchen.deploy.backend.workflow.service.IWorkflowDefinitionService;
|
||||
import jakarta.annotation.Resource;
|
||||
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.RepositoryService;
|
||||
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.repository.Deployment;
|
||||
import org.flowable.bpmn.model.BpmnModel;
|
||||
import org.flowable.engine.runtime.Execution;
|
||||
import org.flowable.engine.runtime.ProcessInstance;
|
||||
import org.flowable.bpmn.model.Process;
|
||||
import org.flowable.bpmn.model.FlowElement;
|
||||
@ -133,11 +136,14 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
|
||||
@Override
|
||||
public WorkflowExecutionDTO getWorkflowExecution(String processInstanceId) {
|
||||
try {
|
||||
log.info("=== 开始获取工作流执行状态 ===");
|
||||
log.info("流程实例ID: {}", processInstanceId);
|
||||
|
||||
// 1. 获取流程实例信息
|
||||
ProcessInstance runningInstance = runtimeService.createProcessInstanceQuery()
|
||||
.processInstanceId(processInstanceId)
|
||||
.singleResult();
|
||||
|
||||
|
||||
HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery()
|
||||
.processInstanceId(processInstanceId)
|
||||
.singleResult();
|
||||
@ -146,24 +152,15 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
|
||||
throw new RuntimeException("Process instance not found: " + processInstanceId);
|
||||
}
|
||||
|
||||
log.info("=== 开始获取工作流执行状态 ===");
|
||||
log.info("流程实例ID: {}", processInstanceId);
|
||||
log.info("流程定义ID: {}", historicInstance.getProcessDefinitionId());
|
||||
log.info("是否有运行实例: {}", runningInstance != null);
|
||||
log.info("流程是否在运行: {}", runningInstance != null);
|
||||
log.info("流程开始时间: {}", historicInstance.getStartTime());
|
||||
log.info("流程结束时间: {}", historicInstance.getEndTime());
|
||||
|
||||
// 2. 获取流程定义
|
||||
BpmnModel bpmnModel = repositoryService.getBpmnModel(historicInstance.getProcessDefinitionId());
|
||||
Process process = bpmnModel.getMainProcess();
|
||||
|
||||
// 打印流程定义中的所有节点
|
||||
log.info("=== 流程定义节点信息 ===");
|
||||
process.getFlowElements().forEach(element -> {
|
||||
log.info("节点信息 - ID: {}, 名称: {}, 类型: {}",
|
||||
element.getId(),
|
||||
element.getName(),
|
||||
element.getClass().getSimpleName());
|
||||
});
|
||||
|
||||
// 3. 获取所有历史活动实例
|
||||
List<HistoricActivityInstance> historicActivities = historyService.createHistoricActivityInstanceQuery()
|
||||
.processInstanceId(processInstanceId)
|
||||
@ -172,34 +169,38 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
|
||||
.list();
|
||||
|
||||
log.info("=== 历史活动实例信息 ===");
|
||||
historicActivities.forEach(activity -> {
|
||||
log.info("历史活动 - ID: {}, 名称: {}, 类型: {}, 开始时间: {}, 结束时间: {}",
|
||||
for (HistoricActivityInstance activity : historicActivities) {
|
||||
log.info("节点ID: {}, 名称: {}, 类型: {}, 开始时间: {}, 结束时间: {}, 执行ID: {}",
|
||||
activity.getActivityId(),
|
||||
activity.getActivityName(),
|
||||
activity.getActivityType(),
|
||||
activity.getStartTime(),
|
||||
activity.getEndTime());
|
||||
});
|
||||
|
||||
// 4. 获取当前活动的节点
|
||||
List<String> activeActivityIds = new ArrayList<>();
|
||||
if (runningInstance != null) {
|
||||
activeActivityIds = runtimeService.getActiveActivityIds(processInstanceId);
|
||||
log.info("=== 当前活动节点 ===");
|
||||
log.info("活动节点IDs: {}", activeActivityIds);
|
||||
activity.getEndTime(),
|
||||
activity.getExecutionId());
|
||||
}
|
||||
|
||||
// 5. 获取所有流程变量
|
||||
List<HistoricVariableInstance> variables = historyService.createHistoricVariableInstanceQuery()
|
||||
.processInstanceId(processInstanceId)
|
||||
.list();
|
||||
|
||||
log.info("=== 流程变量信息 ===");
|
||||
Map<String, Object> variableMap = new HashMap<>();
|
||||
variables.forEach(variable -> {
|
||||
variableMap.put(variable.getVariableName(), variable.getValue());
|
||||
log.info("变量 - 名称: {}, 值: {}", variable.getVariableName(), variable.getValue());
|
||||
});
|
||||
// 4. 获取当前正在执行的活动节点
|
||||
List<String> activeActivityIds = new ArrayList<>();
|
||||
if (runningInstance != null) {
|
||||
List<Execution> executions = runtimeService.createExecutionQuery()
|
||||
.processInstanceId(processInstanceId)
|
||||
.list();
|
||||
|
||||
for (Execution execution : executions) {
|
||||
if (execution.getActivityId() != null) {
|
||||
activeActivityIds.add(execution.getActivityId());
|
||||
}
|
||||
}
|
||||
log.info("当前活动节点IDs: {}", activeActivityIds);
|
||||
}
|
||||
|
||||
// 5. 构建历史活动实例映射
|
||||
Map<String, HistoricActivityInstance> historicActivityMap = historicActivities.stream()
|
||||
.collect(Collectors.toMap(
|
||||
HistoricActivityInstance::getActivityId,
|
||||
activity -> activity,
|
||||
(existing, replacement) -> existing
|
||||
));
|
||||
|
||||
// 6. 构建执行状态DTO
|
||||
WorkflowExecutionDTO executionDTO = new WorkflowExecutionDTO();
|
||||
@ -210,128 +211,88 @@ public class WorkflowDefinitionServiceImpl extends BaseServiceImpl<WorkflowDefin
|
||||
executionDTO.setStartTime(historicInstance.getStartTime());
|
||||
executionDTO.setEndTime(historicInstance.getEndTime());
|
||||
executionDTO.setDurationInMillis(historicInstance.getDurationInMillis());
|
||||
executionDTO.setVariables(variableMap);
|
||||
|
||||
// 设置状态
|
||||
// 设置流程状态
|
||||
if (historicInstance.getEndTime() != null) {
|
||||
executionDTO.setStatus(historicInstance.getDeleteReason() == null ?
|
||||
executionDTO.setStatus(historicInstance.getDeleteReason() == null ?
|
||||
WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED);
|
||||
} else {
|
||||
executionDTO.setStatus(runningInstance != null && runningInstance.isSuspended() ?
|
||||
WorkflowInstanceStatus.SUSPENDED : WorkflowInstanceStatus.RUNNING);
|
||||
}
|
||||
|
||||
// 7. 创建历史活动实例的映射,用于快速查找
|
||||
Map<String, HistoricActivityInstance> historicActivityMap = historicActivities.stream()
|
||||
.collect(Collectors.toMap(
|
||||
HistoricActivityInstance::getActivityId,
|
||||
activity -> activity,
|
||||
(existing, replacement) -> existing
|
||||
));
|
||||
|
||||
// 8. 获取所有流程节点并构建阶段列表
|
||||
// 7. 构建阶段列表
|
||||
List<WorkflowExecutionDTO.StageDTO> stages = new ArrayList<>();
|
||||
Collection<FlowElement> flowElements = process.getFlowElements();
|
||||
|
||||
// 记录已执行过的节点ID
|
||||
Set<String> executedActivityIds = historicActivities.stream()
|
||||
.map(HistoricActivityInstance::getActivityId)
|
||||
.collect(Collectors.toSet());
|
||||
List<FlowElement> flowElements = process.getFlowElements().stream()
|
||||
.filter(element -> !(element instanceof SequenceFlow))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 获取所有节点的列表(按照流程定义的顺序)
|
||||
List<FlowElement> orderedElements = new ArrayList<>();
|
||||
log.info("=== 开始构建节点状态 ===");
|
||||
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();
|
||||
stage.setId(element.getId());
|
||||
stage.setName(element.getName());
|
||||
|
||||
// 设置节点类型
|
||||
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());
|
||||
}
|
||||
stage.setType(element.getClass().getSimpleName());
|
||||
|
||||
// 获取历史活动实例
|
||||
HistoricActivityInstance historicActivity = historicActivityMap.get(element.getId());
|
||||
|
||||
// 设置开始和结束时间
|
||||
if (historicActivity != null) {
|
||||
stage.setStartTime(historicActivity.getStartTime());
|
||||
stage.setEndTime(historicActivity.getEndTime());
|
||||
}
|
||||
|
||||
// 设置节点状态
|
||||
if (historicActivity != null) {
|
||||
if (historicActivity.getEndTime() != null) {
|
||||
// 已完成的节点
|
||||
stage.setStatus(historicActivity.getDeleteReason() == null ?
|
||||
WorkflowInstanceStatus.COMPLETED : WorkflowInstanceStatus.FAILED);
|
||||
} else if (activeActivityIds.contains(element.getId())) {
|
||||
// 正在执行的节点
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
||||
// 确定节点状态
|
||||
determineStageStatus(stage, element, historicActivity, activeActivityIds);
|
||||
|
||||
log.info("节点[{}] - 名称: {}, 类型: {}, 状态: {}, 开始时间: {}, 结束时间: {}",
|
||||
stage.getId(),
|
||||
stage.getName(),
|
||||
stage.getType(),
|
||||
stage.getStatus(),
|
||||
stage.getStartTime(),
|
||||
stage.getEndTime());
|
||||
|
||||
stages.add(stage);
|
||||
}
|
||||
|
||||
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;
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to get workflow execution: {}", processInstanceId, e);
|
||||
throw new RuntimeException("Failed to get workflow execution", e);
|
||||
log.error("获取工作流执行状态失败", 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user