Skip to content
Merged

Dev #286

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ REST 层通过请求参数 `mockKey` 分流到 Mock 实例(见 `FlowRecordCont

### 事件(IFlowEvent + EventPusher)

10 种事件:开始 / 待办 / 已办 / 完成 / 催办 / 撤销(`FlowRecordRevokeEvent`)/ 删除(`FlowRecordDeleteEvent`)/ 子流程重置(`FlowSubProcessResetEvent`)/ 减签(`FlowRecordReduceAuditEvent`)/ 二次发起(`FlowRecordReissueEvent`)。操作完成后用 `FlowEventPublisher.push(event)` 推送(**禁止**直接调用 `EventPusher`);引入 starter 时由 `SpringFlowEventPublisher` 保证「事务提交后投递 + 按 `eventKey()`(默认 processId)分区串行」,未注册实现时自动回退 `EventPusher`。记录类事件携带 `FlowRecord` + `mock` 标记,重置事件携带新旧聚合组快照。外部可用 Spring `@EventListener` 订阅。
11 种事件:开始 / 待办 / 已办 / 完成 / 催办 / 撤销(`FlowRecordRevokeEvent`)/ 删除(`FlowRecordDeleteEvent`)/ 子流程重置(`FlowSubProcessResetEvent`)/ 减签(`FlowRecordReduceAuditEvent`)/ 二次发起(`FlowRecordReissueEvent`)/ 保存(`FlowRecordSaveEvent`,由 `FlowRecordSaveService.saveAll()` 按实际落库记录逐条推送)。操作完成后用 `FlowEventPublisher.push(event)` 推送(**禁止**直接调用 `EventPusher`);引入 starter 时由 `SpringFlowEventPublisher` 保证「事务提交后投递 + 按 `eventKey()`(默认 processId)分区串行」,未注册实现时自动回退 `EventPusher`。记录类事件携带 `FlowRecord` + `mock` 标记,重置事件携带新旧聚合组快照。外部可用 Spring `@EventListener` 订阅。

### API 模块划分

Expand Down
14 changes: 13 additions & 1 deletion docs/Integration/event-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IAsyncEvent extends IEvent {} // 异步事件标记 ←

同步/异步由 `DomainEventContext.push` 的 `instanceof` 判定:`IAsyncEvent` → 异步(线程池执行);`ISyncEvent`/裸 `IEvent` → 同步(发布线程内联执行)。

### 1.2 十种事件
### 1.2 十一种事件

记录类事件均携带 `FlowRecord`(或当前记录)与 `boolean mock` 标记(Mock 模式为 `true`)。

Expand All @@ -43,6 +43,7 @@ public interface IAsyncEvent extends IEvent {} // 异步事件标记 ←
| `FlowRecordReduceAuditEvent` | 减签(每条被撤下的记录) | `FlowRecord, long reduceOperatorId, boolean mock` | `getFlowRecord()` / `getReduceOperatorId()` / `isMock()` |
| `FlowSubProcessResetEvent` | 子流程数据重置(issue #219) | `SubProcessRecord oldRecord, SubProcessRecord newRecord, long resetRecordId, IFlowOperator resetOperator, boolean mock` | `getOldRecord()` / `getNewRecord()` / `getResetRecordId()` / `getResetOperator()` / `isMock()` |
| `FlowRecordReissueEvent` | 已完成流程二次发起(issue #272) | `FlowRecord sourceRecord, FlowRecord newRecord, IFlowOperator operator, boolean mock` | `getSourceRecord()` / `getNewRecord()` / `getOperator()` / `isMock()` |
| `FlowRecordSaveEvent` | 流程记录保存(每条实际落库记录,issue #280) | `FlowRecord, boolean mock` | `getFlowRecord()` / `isMock()` |

**子流程重置事件订阅须知**:

Expand Down Expand Up @@ -132,9 +133,20 @@ public class EventPusher {
| `FlowDeleteService.delete()` | `DeleteEvent` |
| `FlowSubProcessResetService.reset()` | 被作废记录链逐条 `RevokeEvent` + 子流程重置 `SubProcessResetEvent`;重建实例经 `FlowCreateService` 推送各自的 `StartEvent` + `TodoEvent` |
| `FlowReissueService.reissue()` | 二次发起 `ReissueEvent` + 新流程经 `FlowCreateService` 推送的 `StartEvent` + `TodoEvent` |
| `FlowRecordSaveService.saveAll()` | 本次实际落库的每条记录 `SaveEvent`(结束节点虚拟记录不落库,不推送) |

> 事件在业务数据保存之后推送,且引入 starter 时会在**事务提交后**才投递给订阅方(见 4.5),保证订阅方回查到的记录已持久化。

### 保存事件订阅须知(issue #280)

`FlowRecordSaveEvent` 覆盖全部保存路径(`IRepositoryHolder.saveRecords` / `saveRecord` → `FlowRecordService` → `FlowRecordSaveService.saveAll()`),订阅方需注意:

- **粒度**:每条实际落库记录一个事件,与 `StartEvent` / `TodoEvent` 等保持一致;分区键为 `processId`;
- **频次**:一次业务操作可能多次保存(如审批通过时多次 `saveRecords`),同一流程会收到多条保存事件,建议按 `processId + recordId` 幂等;
- **已读回写**:首次查看流程详情时 `FlowDetailService` 会回写已读状态,同样会触发保存事件;
- **不区分操作类型**:保存事件不标明发起/审批/撤销等业务动作,需要区分时请结合 `StartEvent` / `DoneEvent` / `RevokeEvent` 等事件;
- **回滚不投递**:引入 starter 时由 `SpringFlowEventPublisher` 的 `afterCommit` 保证事务回滚不投递。

### 结束节点虚拟记录拦截

`PassAction` 会拦截**结束节点虚拟记录**的 `TodoEvent`(结束节点不持久化、`operatorId=-1`,并非真实业务待办),避免下游收到「已完成流程的待办」:
Expand Down
2 changes: 1 addition & 1 deletion docs/capabilities/flow-engine-framework/flow-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ content_hash: 48847288af1a4551b8327e398d01a8ec4de643dce1c049b90dcdc8ad0134df3b
| `FlowProcessNodeService` | 查询流程节点执行记录 |
| `FlowDelayTriggerService` | 处理延迟触发任务 |
| `FlowUrgeService` | 处理催办逻辑 |
| `FlowRecordSaveService` | 保存流程记录和待办合并关系 |
| `FlowRecordSaveService` | 保存流程记录和待办合并关系,并按实际落库记录推送 `FlowRecordSaveEvent`(issue #280) |
| `FlowSubProcessResetService` | 子流程数据重置(旧聚合组作废、实例继承/重建) |
| `FlowSubProcessResultService` | 子流程完成后回写父流程并生成待办/已办 |
| `SubProcessSessionFactory` | 子流程会话构建(按聚合组/实例定位记录) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.codingapi.flow.event;

import com.codingapi.flow.record.FlowRecord;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* 运行中流程实例强制同步事件
*
* <p>流程实例的运行时快照引用由 {@code oldWorkRuntimeId} 换绑为 {@code newWorkRuntimeId} 后触发,
* 业务方可据此刷新本地缓存或提示流程参与人。每个被同步的实例产生一条事件。</p>
*/
@Getter
@AllArgsConstructor
public class FlowRecordForceSyncEvent implements IFlowEvent {

/**
* 被同步的流程记录(实例内任一记录,用于定位流程实例与节点)
*/
private final FlowRecord flowRecord;

/**
* 同步前的运行时快照id
*/
private final long oldWorkRuntimeId;

/**
* 同步后的运行时快照id
*/
private final long newWorkRuntimeId;

private final boolean mock;

/**
* 事件分区键:按被同步实例的流程id串行投递
*/
@Override
public String eventKey() {
return flowRecord == null ? null : flowRecord.getProcessId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.codingapi.flow.event;

import com.codingapi.flow.record.FlowRecord;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* 流程记录保存事件
* <p>
* 每次保存流程记录后按记录逐条推送,仅包含实际落库的记录(结束节点虚拟记录不落库,不推送)
*/
@Getter
@AllArgsConstructor
public class FlowRecordSaveEvent implements IFlowEvent {

private final FlowRecord flowRecord;

private final boolean mock;

/**
* 事件分区键:同一流程内的事件按顺序投递
*/
@Override
public String eventKey() {
return flowRecord.getProcessId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public static FlowNotFoundException record(long recordId) {
String.format("Flow record not found: %d", recordId));
}

/**
* Flow instance not found
*
* @param processId flow instance process id
* @return exception
*/
public static FlowNotFoundException process(String processId) {
return new FlowNotFoundException("notFound.process.id",
String.format("Flow instance not found: %s", processId));
}

/**
* Node not found
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,40 @@ public static FlowStateException recordNotSupportReissue() {
"Flow record is not finished normally, reissue not allowed");
}

/**
* Force sync rejected because the latest workflow structure does not match the runtime snapshot
*
* @param processId flow instance process id
* @param detail structure differences
* @return exception
*/
public static FlowStateException forceSyncStructureInconsistent(String processId, String detail) {
return new FlowStateException("state.forceSync.structureInconsistent",
String.format("Flow instance %s cannot force sync, workflow structure changed: %s", processId, detail));
}

/**
* Force sync only supports running flow instances
*
* @param processId flow instance process id
* @return exception
*/
public static FlowStateException forceSyncInstanceNotRunning(String processId) {
return new FlowStateException("state.forceSync.notRunning",
String.format("Flow instance %s is not running, force sync not allowed", processId));
}

/**
* Records of one flow instance reference different runtime snapshots
*
* @param processId flow instance process id
* @return exception
*/
public static FlowStateException forceSyncRuntimeInconsistent(String processId) {
return new FlowStateException("state.forceSync.runtimeInconsistent",
String.format("Flow instance %s references different runtime snapshots, force sync not allowed", processId));
}

/**
* Workflow script not found when executing a node or action
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public MockRepositoryHolder(FlowOperatorGateway flowOperatorGateway,
this.workflowRepository = workflowRepository;
this.workflowRuntimeRepository = new WorkflowRuntimeRepositoryMockImpl();
this.workflowVersionRepository = new WorkflowVersionRepositoryMockImpl();
this.flowRecordService = new FlowRecordService(flowTodoRecordRepository, flowTodoMergeRepository, flowRecordRepository);
this.flowRecordService = new FlowRecordService(flowTodoRecordRepository, flowTodoMergeRepository, flowRecordRepository, true);
this.workflowService = new WorkflowService(workflowVersionRepository, workflowRepository, workflowRuntimeRepository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.codingapi.flow.mock.MockRepositoryHolder;
import com.codingapi.flow.mock.repository.FlowRecordRepositoryMockImpl;
import com.codingapi.flow.mock.repository.FlowTodoRecordRepositoryMockImpl;
import com.codingapi.flow.node.nodes.StartNode;
import com.codingapi.flow.pojo.response.FlowRecordContent;
import com.codingapi.flow.query.FlowRecordQueryService;
import com.codingapi.flow.record.FlowRecord;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -78,4 +81,19 @@ public Page<FlowRecordContent> findDoneRecordPage(long userId, PageRequest reque
return toPage(recordList, request);
}

@Override
public Page<FlowRecordContent> findRunningInstancePage(String workCode, String title, PageRequest request) {
FlowRecordRepositoryMockImpl flowRecordRepositoryMock = (FlowRecordRepositoryMockImpl) mockRepositoryHolder.getFlowRecordRepository();
List<FlowRecordContent> recordList = flowRecordRepositoryMock.findAll().stream()
.filter(record -> workCode.equals(record.getWorkCode()))
.filter(record -> record.getFlowState() == FlowRecord.SATE_FLOW_RUNNING)
.filter(record -> record.getFromId() == 0 && StartNode.NODE_TYPE.equals(record.getNodeType()))
.filter(record -> !record.isHidden())
.filter(record -> !StringUtils.hasText(title)
|| (record.getTitle() != null && record.getTitle().contains(title)))
.map(FlowRecordContent::convert)
.toList();
return toPage(recordList, request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.codingapi.flow.pojo.request;

import com.codingapi.flow.exception.FlowValidationException;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* 运行中流程实例强制同步最新流程设计请求(独立接口,不属于常规审批动作)。
*
* <p>强制同步只换绑运行时快照引用:选中实例的记录改指向最新设计对应的快照,
* 未选中的实例保持原快照不变。同步前会校验选中实例的结构与最新设计一致。</p>
*/
@Data
@NoArgsConstructor
public class FlowForceSyncRequest {

/**
* 流程编码
*/
private String workCode;

/**
* 选中同步的流程实例唯一标识列表(未选中的实例不会被修改)
*/
private List<String> processIds;

/**
* 操作者
*/
private long operatorId;

public FlowForceSyncRequest(String workCode, List<String> processIds, long operatorId) {
this.workCode = workCode;
this.processIds = processIds;
this.operatorId = operatorId;
}

public void verify() {
if (!StringUtils.hasText(workCode)) {
throw FlowValidationException.required("workCode");
}
if (processIds == null || processIds.isEmpty()) {
throw FlowValidationException.required("processIds");
}
if (processIds.stream().anyMatch(processId -> !StringUtils.hasText(processId))) {
throw FlowValidationException.required("processIds");
}
if (operatorId <= 0) {
throw FlowValidationException.required("operatorId");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.codingapi.flow.pojo.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* 运行中流程实例强制同步结果
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FlowForceSyncResult {

/**
* 本次同步使用的运行时快照id(同一设计版本下多个实例共享同一份快照)
*/
private long workflowRuntimeId;

/**
* 该快照对应的设计版本(流程设计的更新时间)
*/
private long workVersion;

/**
* 已换绑到新快照的流程实例
*/
private List<String> syncedProcessIds;

/**
* 已是最新快照而跳过的流程实例
*/
private List<String> skippedProcessIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ public interface FlowRecordQueryService {
Page<FlowRecordContent> findNotifyRecordPage(long userId, PageRequest request);

Page<FlowRecordContent> findDoneRecordPage(long userId, PageRequest request);

/**
* 查询指定流程编码下运行中的流程实例(以开始节点记录代表一个实例)
*
* <p>用于「运行中实例强制同步最新流程设计」时选择同步范围(issue #281)。</p>
*
* @param workCode 流程编码
* @param title 流程标题(模糊匹配,可为空)
* @param request 分页
* @return 运行中的流程实例分页
*/
Page<FlowRecordContent> findRunningInstancePage(String workCode, String title, PageRequest request);
}
Loading
Loading