refactor: service层存储依赖改窄接口

This commit is contained in:
rulego-team committed 2026-09-28 09:27:48 +08:00
1 parent 307843de2a
commit fff70d523c
24 files changed
+221 -104

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
package dao
import "github.com/rulego/gflow-engine/query"
// Underlying 系列返回事务入口所需的 *query.Query:service 层 WithInstanceTx
// 经此取查询束开事务,以方法而非字段暴露是为了让存储窄接口的实现不必
// 背负 gorm gen 的查询束结构。
func (d *TaskDAO) Underlying() *query.Query { return d.Query }
func (d *HiTaskDAO) Underlying() *query.Query { return d.Query }
func (d *InstanceDAO) Underlying() *query.Query { return d.Query }
func (d *HiInstanceDAO) Underlying() *query.Query { return d.Query }
func (d *ProcessDAO) Underlying() *query.Query { return d.Query }
func (d *TaskAssigneeDAO) Underlying() *query.Query { return d.Query }
func (d *TaskCommentDAO) Underlying() *query.Query { return d.Query }
+1 -2
View File
@@ -23,7 +23,6 @@ import (
"strings"
"time"
"github.com/rulego/gflow-engine/dao"
"github.com/rulego/gflow-engine/model"
"github.com/rulego/gflow-engine/types/constants"
"github.com/rulego/gflow-engine/types/enums"
@@ -42,7 +41,7 @@ var (
)
type TaskCreator struct {
instanceDAO *dao.InstanceDAO
instanceDAO InstanceStore
workflowEngine WorkflowEngine
}
+3 -3
View File
@@ -18,9 +18,9 @@ var _ HistoryService = (*HistoryServiceImpl)(nil)
// HistoryServiceImpl HistoryService的实现
type HistoryServiceImpl struct {
hiInstanceDAO *dao.HiInstanceDAO
hiTaskDAO *dao.HiTaskDAO
processDAO *dao.ProcessDAO
hiInstanceDAO HiInstanceStore
hiTaskDAO HiTaskStore
processDAO ProcessStore
}
// NewHistoryService 创建HistoryService实例
+14 -14
View File
@@ -33,39 +33,39 @@ func bareScope(q *query.Query) *InstanceScope {
return &InstanceScope{tx: q, bare: true}
}
// Tasks 返回绑定了当前 tx 的 TaskDAO。
// Tasks 返回绑定了当前 tx 的任务存储。
// 每次调用都新建 wrapper(dao.NewTaskDAOWithQuery 只设置一个指针字段,廉价)。
func (s *InstanceScope) Tasks() *dao.TaskDAO {
func (s *InstanceScope) Tasks() TaskStore {
return dao.NewTaskDAOWithQuery(s.tx)
}
// Instances 返回绑定了当前 tx 的 InstanceDAO。
func (s *InstanceScope) Instances() *dao.InstanceDAO {
// Instances 返回绑定了当前 tx 的实例存储。
func (s *InstanceScope) Instances() InstanceStore {
return dao.NewInstanceDAOWithQuery(s.tx)
}
// HiTasks 返回绑定了当前 tx 的 HiTaskDAO(历史任务归档)。
func (s *InstanceScope) HiTasks() *dao.HiTaskDAO {
// HiTasks 返回绑定了当前 tx 的历史任务存储(归档)。
func (s *InstanceScope) HiTasks() HiTaskStore {
return dao.NewHiTaskDAOWithQuery(s.tx)
}
// HiInstances 返回绑定了当前 tx 的 HiInstanceDAO(历史实例归档)。
func (s *InstanceScope) HiInstances() *dao.HiInstanceDAO {
// HiInstances 返回绑定了当前 tx 的历史实例存储(归档)。
func (s *InstanceScope) HiInstances() HiInstanceStore {
return dao.NewHiInstanceDAOWithQuery(s.tx)
}
// Processes 返回绑定了当前 tx 的 ProcessDAO(流程定义)。
func (s *InstanceScope) Processes() *dao.ProcessDAO {
// Processes 返回绑定了当前 tx 的流程定义存储。
func (s *InstanceScope) Processes() ProcessStore {
return dao.NewProcessDAOWithQuery(s.tx)
}
// TaskAssignees 返回绑定了当前 tx 的 TaskAssigneeDAO(候选校验需在事务内读候选池)。
func (s *InstanceScope) TaskAssignees() *dao.TaskAssigneeDAO {
// TaskAssignees 返回绑定了当前 tx 的候选池存储(候选校验须在事务内读)。
func (s *InstanceScope) TaskAssignees() TaskAssigneeStore {
return dao.NewTaskAssigneeDAOWithQuery(s.tx)
}
// TaskComments 返回绑定了当前 tx 的 TaskCommentDAO(审批意见随审批动作同事务落库)。
func (s *InstanceScope) TaskComments() *dao.TaskCommentDAO {
// TaskComments 返回绑定了当前 tx 的评论存储(审批意见随审批动作同事务落库)。
func (s *InstanceScope) TaskComments() TaskCommentStore {
return dao.NewTaskCommentDAOWithQuery(s.tx)
}
+3 -3
View File
@@ -22,8 +22,8 @@ var _ ProcessService = (*ProcessServiceImpl)(nil)
// ProcessServiceImpl ProcessService的实现
type ProcessServiceImpl struct {
processDAO *dao.ProcessDAO
instanceDAO *dao.InstanceDAO
processDAO ProcessStore
instanceDAO InstanceStore
idGenerator IDGenerator
// engine 用于在 Deploy 时预加载流程链到共享 enginePool,使 subProcess 子链可被父流程寻址。
engine WorkflowEngine
@@ -265,7 +265,7 @@ func (s *ProcessServiceImpl) create(ctx context.Context, process *model.WfProces
}
// 保存流程定义 + 退役旧 active 原子(保证同 key 仅一个 active,防并发/抖动产生多 active)
if err := s.processDAO.Query.Transaction(func(tx *query.Query) error {
if err := s.processDAO.Underlying().Transaction(func(tx *query.Query) error {
txDAO := dao.NewProcessDAOWithQuery(tx)
if err := txDAO.Create(ctx, process); err != nil {
return fmt.Errorf("failed to deploy process: %w", err)
+2 -2
View File
@@ -90,7 +90,7 @@ func RecentlyUpdatedProcessIDs(ctx context.Context, since time.Time) ([]string,
if !ok || s == nil {
return true
}
db := s.processDAO.Query.WfProcess.UnderlyingDB().WithContext(ctx)
db := s.processDAO.Underlying().WfProcess.UnderlyingDB().WithContext(ctx)
if err := db.Model(&model.WfProcess{}).Where("updated_at > ?", since).Pluck("id", &ids).Error; err != nil {
lastErr = err
return true
@@ -177,7 +177,7 @@ func (s *RuntimeServiceImpl) poolFor(tenantID string) types.RuleEnginePool {
// 只能在事务外调用;事务内(如 TerminateInTx)须改用 evictStaleChain 并传入事务 q,
// 走全局连接会与当前事务互等(SQLite 等单写锁数据库)且读不到未提交的删除结果。
func (s *RuntimeServiceImpl) EvictStaleChain(ctx context.Context, tenantID, processID string) {
s.evictStaleChain(ctx, s.processDAO.Query, tenantID, processID)
s.evictStaleChain(ctx, s.processDAO.Underlying(), tenantID, processID)
}
// evictStaleChain 统一驱逐实现;q 事务外传全局 Query,事务内传事务 tx。
+15 -15
View File
@@ -32,10 +32,10 @@ const archiveBatchSize = 100
type RuntimeServiceImpl struct {
BaseService
workflowEngine WorkflowEngine
instanceDAO *dao.InstanceDAO
hiInstanceDAO *dao.HiInstanceDAO
processDAO *dao.ProcessDAO
taskDAO *dao.TaskDAO
instanceDAO InstanceStore
hiInstanceDAO HiInstanceStore
processDAO ProcessStore
taskDAO TaskStore
idGenerator IDGenerator
enginePool types.RuleEnginePool
// execGates ExecuteNext 的实例级可重入门闩表(instanceID -> *execGate)
@@ -342,7 +342,7 @@ func (s *RuntimeServiceImpl) DeleteProcessInstance(ctx context.Context, actor Ac
return fmt.Errorf("process instance ID cannot be empty")
}
err := WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
err := WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
tx := scope.Tx()
// 1. 在事务内读取实例(已持锁)
instance, err := tx.WfInstance.WithContext(ctx).Where(tx.WfInstance.ID.Eq(processInstanceID)).First()
@@ -439,7 +439,7 @@ func (s *RuntimeServiceImpl) DeleteProcessInstance(ctx context.Context, actor Ac
// markArchivedInstanceDeleted 把已归档实例的历史行标为 deleted。
func (s *RuntimeServiceImpl) markArchivedInstanceDeleted(ctx context.Context, processInstanceID, reason string) error {
q := s.instanceDAO.Query
q := s.instanceDAO.Underlying()
hi, err := q.WfHiInstance.WithContext(ctx).Where(q.WfHiInstance.ID.Eq(processInstanceID)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -480,7 +480,7 @@ func (s *RuntimeServiceImpl) SuspendProcessInstance(ctx context.Context, actor A
if processInstanceID == "" {
return fmt.Errorf("process instance ID cannot be empty")
}
return WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
return s.suspendProcessInstanceInternal(ctx, scope, processInstanceID)
})
}
@@ -593,7 +593,7 @@ func (s *RuntimeServiceImpl) ActivateProcessInstance(ctx context.Context, actor
// 持锁执行实例状态变更 + 级联任务激活
var isDraft bool
var draftInstance *model.WfInstance
if err := WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
if err := WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
inst, startedDraft, err := s.activateProcessInstanceInternal(ctx, scope, processInstanceID)
if err != nil {
return err
@@ -864,7 +864,7 @@ func (s *RuntimeServiceImpl) SetProcessInstanceVariables(ctx context.Context, ac
return err
}
return WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
tx := scope.Tx()
existingVars, err := s.getProcessInstanceVariablesInTx(ctx, tx, processInstanceID)
if err != nil {
@@ -886,7 +886,7 @@ func (s *RuntimeServiceImpl) SetProcessInstanceVariable(ctx context.Context, act
if variableName == "" {
return fmt.Errorf("variable name cannot be empty")
}
return WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
tx := scope.Tx()
variables, err := s.getProcessInstanceVariablesInTx(ctx, tx, processInstanceID)
if err != nil {
@@ -906,7 +906,7 @@ func (s *RuntimeServiceImpl) RemoveProcessInstanceVariable(ctx context.Context,
if variableName == "" {
return fmt.Errorf("variable name cannot be empty")
}
return WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
tx := scope.Tx()
variables, err := s.getProcessInstanceVariablesInTx(ctx, tx, processInstanceID)
if err != nil {
@@ -1072,7 +1072,7 @@ func (s *RuntimeServiceImpl) CompleteProcessInstance(ctx context.Context, actor
now := time.Now()
duration := now.Sub(instance.CreatedAt).Milliseconds()
if err := WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
if err := WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
tx := scope.Tx()
// 1. 更新实例状态为已完成
if _, err := tx.WfInstance.WithContext(ctx).Where(tx.WfInstance.ID.Eq(processInstanceID)).
@@ -1609,7 +1609,7 @@ func (s *RuntimeServiceImpl) GetStuckProcessInstances(ctx context.Context, actor
if err != nil {
return nil, err
}
db := s.instanceDAO.Query.WfInstance.UnderlyingDB().WithContext(ctx)
db := s.instanceDAO.Underlying().WfInstance.UnderlyingDB().WithContext(ctx)
q := db.Table("wf_instance as i").
Select("i.*").
Where("i.status = ?", string(enums.InstanceStatusActive)).
@@ -1642,7 +1642,7 @@ func (s *RuntimeServiceImpl) GetExpiredDelayTasks(ctx context.Context, actor Act
if err != nil {
return nil, err
}
db := s.taskDAO.Query.WfTask.UnderlyingDB().WithContext(ctx)
db := s.taskDAO.Underlying().WfTask.UnderlyingDB().WithContext(ctx)
q := db.Table("wf_task").
Where("task_type = ?", constants.TaskTypeDelay).
Where("status IN (?, ?)", string(enums.TaskStatusActive), string(enums.TaskStatusPending)).
@@ -2137,7 +2137,7 @@ func (s *RuntimeServiceImpl) TerminateProcessInstance(ctx context.Context, actor
if processInstanceID == "" {
return fmt.Errorf("process instance ID cannot be empty")
}
if err := WithInstanceTx(ctx, s.instanceDAO.Query, processInstanceID, func(scope *InstanceScope) error {
if err := WithInstanceTx(ctx, s.instanceDAO.Underlying(), processInstanceID, func(scope *InstanceScope) error {
evt, err := s.TerminateInTx(ctx, scope.Tx(), processInstanceID, reason)
if err != nil {
return err
+1 -1
View File
@@ -538,7 +538,7 @@ func TestActivateDraftInternal_ReportsDraftStartOnlyOnce(t *testing.T) {
// activate 跑一次持锁激活,返回内部判定的「本次是否为草稿激活」
activate := func() (bool, error) {
var wasDraft bool
err := WithInstanceTx(ctx, rs.instanceDAO.Query, "d_twice", func(scope *InstanceScope) error {
err := WithInstanceTx(ctx, rs.instanceDAO.Underlying(), "d_twice", func(scope *InstanceScope) error {
_, started, err := rs.activateProcessInstanceInternal(ctx, scope, "d_twice")
wasDraft = started
return err
+108
View File
@@ -0,0 +1,108 @@
// service 层消费的存储窄接口:方法集为 service 层的实际调用,具体 *dao.XxxDAO
// 天然满足。替换实现(读写分离、缓存、测试替身)经此接缝接入;当前构造函数
// 固定装配内置 DAO,注入构造待真实需求出现再补。Underlying 提供事务入口所需
// 的 *query.Query(WithInstanceTx 据此开事务)——以方法而非字段暴露,接口
// 实现才不必背负 gorm gen 的查询束结构。}
package service
import (
"context"
"time"
"github.com/rulego/gflow-engine/dao"
"github.com/rulego/gflow-engine/model"
"github.com/rulego/gflow-engine/query"
"github.com/rulego/gflow-engine/types/dto"
)
// TaskStore 运行任务表存储。
type TaskStore interface {
Underlying() *query.Query
Create(ctx context.Context, entity *model.WfTask) error
Get(ctx context.Context, id string) (*model.WfTask, error)
Update(ctx context.Context, entity *model.WfTask) error
Delete(ctx context.Context, id string) error
GetByProcessInstanceID(ctx context.Context, processInstanceID string) ([]*model.WfTask, error)
GetByParentID(ctx context.Context, parentID string) ([]*model.WfTask, error)
List(ctx context.Context, query *dto.TaskQuery) ([]*model.WfTask, int64, error)
AggregateActiveByProcess(ctx context.Context, tenantID string, limit int) ([]*dao.BacklogAggRow, error)
ListProcessNamesByID(ctx context.Context, ids []string) (map[string]string, error)
}
// HiTaskStore 历史任务表存储。
type HiTaskStore interface {
Underlying() *query.Query
Create(ctx context.Context, entity *model.WfHiTask) error
Get(ctx context.Context, id string) (*model.WfTask, error)
Delete(ctx context.Context, id string) error
List(ctx context.Context, query *dto.TaskQuery) ([]*model.WfTask, int64, error)
}
// InstanceStore 运行实例表存储。
type InstanceStore interface {
Underlying() *query.Query
List(ctx context.Context, request *dto.ProcessInstanceQueryDTO) ([]*model.WfInstance, int64, error)
Create(ctx context.Context, entity *model.WfInstance) error
Get(ctx context.Context, id string) (*model.WfInstance, error)
Update(ctx context.Context, entity *model.WfInstance) error
GetByProcessID(ctx context.Context, ProcessID string, limit, offset int) ([]*model.WfInstance, int64, error)
HasActiveByParentID(ctx context.Context, parentID string) (bool, error)
SetCurrentActivity(ctx context.Context, id, activityKey string) error
ListByTaskConditions(ctx context.Context, req *dto.TaskQuery) ([]*model.WfInstance, int64, error)
GetInstancesUnionPagination(ctx context.Context, tenantID, ProcessID, startUserID string, statuses []string, keyword string, startTimeFrom, startTimeTo *time.Time, limit, offset int, instanceID, businessKey, endReasonPrefix string, endReasonNotPrefixes ...string) ([]*model.WfInstance, int64, error)
CountInstancesUnionByBuckets(ctx context.Context, tenantID, processID, startUserID, keyword string, startTimeFrom, startTimeTo *time.Time, buckets []dao.InstanceStatusBucket) (map[string]int64, error)
CountTaskInstancesByBuckets(ctx context.Context, req *dto.TaskQuery, buckets []dao.InstanceStatusBucket) (map[string]int64, error)
}
// HiInstanceStore 历史实例表存储。
type HiInstanceStore interface {
Underlying() *query.Query
List(ctx context.Context, request *dto.ProcessInstanceQueryDTO) ([]*model.WfInstance, int64, error)
Get(ctx context.Context, id string) (*model.WfInstance, error)
GetIncludingDeleted(ctx context.Context, id string) (*model.WfInstance, error)
HasByParentID(ctx context.Context, parentID string) (bool, error)
HasTerminatedByParentID(ctx context.Context, parentID string) (bool, error)
Delete(ctx context.Context, id string) error
}
// ProcessStore 流程定义表存储。
type ProcessStore interface {
Underlying() *query.Query
List(ctx context.Context, request *dto.ProcessQueryRequest) ([]*model.WfProcess, int64, error)
Get(ctx context.Context, id string) (*model.WfProcess, error)
GetByIDs(ctx context.Context, ids []string) ([]*model.WfProcess, error)
GetByKeyAndVersion(ctx context.Context, tenantID, processKey string, version int32) (*model.WfProcess, error)
GetLatestByKey(ctx context.Context, tenantID, processKey string) (*model.WfProcess, error)
Update(ctx context.Context, entity *model.WfProcess) error
Delete(ctx context.Context, tenantID, id string) error
CountActiveReferencingForm(ctx context.Context, tenantID, formKey string) (int64, error)
}
// TaskAssigneeStore 候选池表存储。
type TaskAssigneeStore interface {
Underlying() *query.Query
CreateBatch(ctx context.Context, entities []*model.WfTaskAssignee) error
GetByTaskID(ctx context.Context, tenantID, taskID string) ([]*model.WfTaskAssignee, error)
GetByInstanceAndDefKey(ctx context.Context, tenantID, processInstanceID, taskDefKey string) ([]*model.WfTaskAssignee, error)
DeleteByTaskAndEntities(ctx context.Context, tenantID, taskID, entityType string, entityIDs []string) error
CountCandidateTasks(ctx context.Context, tenantID, userID string, roleIDs, deptIDs, statuses []string, createdAfter, dueBefore *time.Time) (int64, error)
}
// TaskCommentStore 任务评论表存储。
type TaskCommentStore interface {
Underlying() *query.Query
Create(ctx context.Context, comment *model.WfTaskComment) error
ListByTaskID(ctx context.Context, taskID string) ([]*model.WfTaskComment, error)
}
// 编译期锁定:具体 DAO 必须满足对应窄接口,宿主自有实现与内置实现同契约。
var (
_ TaskStore = (*dao.TaskDAO)(nil)
_ HiTaskStore = (*dao.HiTaskDAO)(nil)
_ InstanceStore = (*dao.InstanceDAO)(nil)
_ HiInstanceStore = (*dao.HiInstanceDAO)(nil)
_ ProcessStore = (*dao.ProcessDAO)(nil)
_ TaskAssigneeStore = (*dao.TaskAssigneeDAO)(nil)
_ TaskCommentStore = (*dao.TaskCommentDAO)(nil)
)
+10 -10
View File
@@ -43,9 +43,9 @@ func (s *TaskServiceImpl) SetAssignee(ctx context.Context, actor Actor, taskID,
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.setAssigneeInternal(ctx, bareScope(s.taskDAO.Query), taskID, userID)
return s.setAssigneeInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, userID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.setAssigneeInternal(ctx, scope, taskID, userID)
})
}
@@ -114,9 +114,9 @@ func (s *TaskServiceImpl) SetOwner(ctx context.Context, actor Actor, taskID, use
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.setOwnerInternal(ctx, bareScope(s.taskDAO.Query), taskID, userID)
return s.setOwnerInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, userID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.setOwnerInternal(ctx, scope, taskID, userID)
})
}
@@ -186,9 +186,9 @@ func (s *TaskServiceImpl) Delegate(ctx context.Context, actor Actor, taskID, use
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.delegateInternal(ctx, bareScope(s.taskDAO.Query), taskID, userID, reason)
return s.delegateInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, userID, reason)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.delegateInternal(ctx, scope, taskID, userID, reason)
})
}
@@ -317,9 +317,9 @@ func (s *TaskServiceImpl) Resolve(ctx context.Context, actor Actor, taskID strin
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.resolveInternal(ctx, bareScope(s.taskDAO.Query), taskID)
return s.resolveInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.resolveInternal(ctx, scope, taskID)
})
}
@@ -443,9 +443,9 @@ func (s *TaskServiceImpl) Transfer(ctx context.Context, actor Actor, taskID, toU
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.transferInternal(ctx, bareScope(s.taskDAO.Query), taskID, fromUserID, toUserID, reason)
return s.transferInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, fromUserID, toUserID, reason)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.transferInternal(ctx, scope, taskID, fromUserID, toUserID, reason)
})
}
+1 -2
View File
@@ -14,7 +14,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/rulego/gflow-engine/dao"
"github.com/rulego/gflow-engine/model"
"github.com/rulego/gflow-engine/types/constants"
"github.com/rulego/gflow-engine/types/dto"
@@ -60,7 +59,7 @@ func expandCandidateMembers(ctx context.Context, identity IdentityService, tenan
// collectCandidateMembersExcluding 展开任务候选池成员(role/dept 经 identity 展开 + person),去重并排除指定用户。
// assignees 传 scope.TaskAssignees():本方法在实例行锁事务内调用,走全局连接会与事务互等连接,
// 单连接配置下死锁到事务超时。
func (s *TaskServiceImpl) collectCandidateMembersExcluding(ctx context.Context, assignees *dao.TaskAssigneeDAO, tenantID, taskID, excludeUserID string) []string {
func (s *TaskServiceImpl) collectCandidateMembersExcluding(ctx context.Context, assignees TaskAssigneeStore, tenantID, taskID, excludeUserID string) []string {
rows, err := assignees.GetByTaskID(ctx, tenantID, taskID)
if err != nil || len(rows) == 0 {
return nil
+5 -5
View File
@@ -44,9 +44,9 @@ func (s *TaskServiceImpl) Claim(ctx context.Context, actor Actor, taskID string)
if instanceID == "" {
// orphan/draft 任务:没有实例行可以锁定,直接在 service 默认 query 上执行。
// 这种场景下不存在跨实例协调问题,但仍保留 Internal 函数的幂等性校验。
return s.claimInternal(ctx, bareScope(s.taskDAO.Query), taskID, userID)
return s.claimInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, userID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.claimInternal(ctx, scope, taskID, userID)
})
}
@@ -217,9 +217,9 @@ func (s *TaskServiceImpl) Unclaim(ctx context.Context, actor Actor, taskID strin
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.unclaimInternal(ctx, bareScope(s.taskDAO.Query), taskID, userID)
return s.unclaimInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, userID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.unclaimInternal(ctx, scope, taskID, userID)
})
}
@@ -294,7 +294,7 @@ func (s *TaskServiceImpl) unclaimInternal(ctx context.Context, scope *InstanceSc
task.UpdatedBy = &username
task.UpdatedAt = &now
qa := taskDAO.Query.WfTask
qa := taskDAO.Underlying().WfTask
if _, err := qa.WithContext(ctx).Where(qa.ID.Eq(task.ID)).
Select(qa.Assignee, qa.Status, qa.ClaimedAt, qa.UpdatedBy, qa.UpdatedAt).
Updates(task); err != nil {
+2 -2
View File
@@ -87,10 +87,10 @@ func (s *TaskServiceImpl) CompleteWithApproval(ctx context.Context, actor Actor,
if instanceID == "" {
// 没有 instance 关联(孤儿任务):无行可锁,用 bare scope 跑同样的逻辑;
// 其 AfterCommit 回调在 bare scope 上立即内联执行(见 InstanceScope.AfterCommit)。
scope := bareScope(s.taskDAO.Query)
scope := bareScope(s.taskDAO.Underlying())
return s.completeWithApprovalInternal(ctx, scope, request)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.completeWithApprovalInternal(ctx, scope, request)
})
}
+3 -3
View File
@@ -58,9 +58,9 @@ func (s *TaskServiceImpl) CreateCountersignSubTasks(ctx context.Context, parentT
instanceID = *parentTask.ProcessInstanceID
}
if instanceID == "" {
return s.createCountersignSubTasksInternal(ctx, bareScope(s.taskDAO.Query), parentTaskID, assignees, approvalRule)
return s.createCountersignSubTasksInternal(ctx, bareScope(s.taskDAO.Underlying()), parentTaskID, assignees, approvalRule)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.createCountersignSubTasksInternal(ctx, scope, parentTaskID, assignees, approvalRule)
})
}
@@ -144,7 +144,7 @@ func (s *TaskServiceImpl) CheckCountersignSubTaskCompletion(ctx context.Context,
if parentTaskID == "" {
return false, false, fmt.Errorf("parentTaskID cannot be empty")
}
return s.checkCountersignSubTaskCompletionInternal(ctx, bareScope(s.taskDAO.Query), parentTaskID, approvalRule)
return s.checkCountersignSubTaskCompletionInternal(ctx, bareScope(s.taskDAO.Underlying()), parentTaskID, approvalRule)
}
// checkCountersignSubTaskCompletionInternal 在指定 scope 上执行会签完成判定。
+2 -2
View File
@@ -158,9 +158,9 @@ func (s *TaskServiceImpl) DeleteTask(ctx context.Context, actor Actor, taskID, r
}
if instanceID == "" {
// orphan/draft 任务:没有实例行可以锁定,直接走 Internal
return s.deleteTaskInternal(ctx, bareScope(s.taskDAO.Query), taskID)
return s.deleteTaskInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.deleteTaskInternal(ctx, scope, taskID)
})
}
+1 -2
View File
@@ -8,7 +8,6 @@ import (
"context"
"fmt"
"github.com/rulego/gflow-engine/dao"
"github.com/rulego/gflow-engine/model"
"github.com/rulego/gflow-engine/types/dto"
)
@@ -20,7 +19,7 @@ const taskFetchAllPageSize = 1000
// listAllTasks 按 query 条件翻页取全量任务。task_dao.List 无条件分页,
// pageSize 未设时回落默认 10,同节点清理/恢复、减签、节点置换这类全量语义
// 的调用会被截断:留下幽灵待办,或漏恢复/漏减签该命中的行。
func listAllTasks(ctx context.Context, taskDAO *dao.TaskDAO, query *dto.TaskQuery) ([]*model.WfTask, error) {
func listAllTasks(ctx context.Context, taskDAO TaskStore, query *dto.TaskQuery) ([]*model.WfTask, error) {
var all []*model.WfTask
for page := 1; ; page++ {
pageQuery := *query
+4 -4
View File
@@ -15,10 +15,10 @@ var _ TaskServiceInternal = (*TaskServiceImpl)(nil)
// TaskServiceImpl TaskService接口的实现
type TaskServiceImpl struct {
taskDAO *dao.TaskDAO
hiTaskDAO *dao.HiTaskDAO
taskAssigneeDAO *dao.TaskAssigneeDAO
taskCommentDAO *dao.TaskCommentDAO
taskDAO TaskStore
hiTaskDAO HiTaskStore
taskAssigneeDAO TaskAssigneeStore
taskCommentDAO TaskCommentStore
idGenerator IDGenerator
workflowEngine WorkflowEngine
}
+2 -2
View File
@@ -71,9 +71,9 @@ func (s *TaskServiceImpl) ProxyAudit(ctx context.Context, actor Actor, taskID st
}
request := &ApprovalRequest{TaskID: taskID, ApprovalResult: result, Comment: comment}
if instanceID == "" {
return s.completeWithApprovalInternal(ctx, bareScope(s.taskDAO.Query), request)
return s.completeWithApprovalInternal(ctx, bareScope(s.taskDAO.Underlying()), request)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.completeWithApprovalInternal(ctx, scope, request)
})
}
+2 -2
View File
@@ -81,10 +81,10 @@ func (s *TaskServiceImpl) Reassign(ctx context.Context, actor Actor, taskID, new
if instanceID == "" {
// orphan/draft 任务:没有实例行可以锁定,直接走 Internal
if err := reassignFn(bareScope(s.taskDAO.Query)); err != nil {
if err := reassignFn(bareScope(s.taskDAO.Underlying())); err != nil {
return "", err
}
} else if err := WithInstanceTx(ctx, s.taskDAO.Query, instanceID, reassignFn); err != nil {
} else if err := WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, reassignFn); err != nil {
return "", err
}
+5 -6
View File
@@ -11,7 +11,6 @@ import (
"fmt"
"time"
"github.com/rulego/gflow-engine/dao"
"github.com/rulego/gflow-engine/model"
"github.com/rulego/gflow-engine/query"
"github.com/rulego/gflow-engine/types/constants"
@@ -84,7 +83,7 @@ func (s *TaskServiceImpl) Recall(ctx context.Context, actor Actor, instanceID, r
return fmt.Errorf("%w: 该流程已关闭审批人收回", ErrPermissionDenied)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.recallInternal(ctx, scope, chain, actor.UserID, instanceID, reason)
})
}
@@ -130,7 +129,7 @@ func (s *TaskServiceImpl) recallCompleted(ctx context.Context, actor Actor, hi *
return fmt.Errorf("%w: 超过可收回窗口(%d 天),无法收回", ErrValidation, recallWindowDays(ap))
}
lastNode, lastNodeName, voters, err := latestCompletedNodeInHistory(ctx, s.taskDAO.Query, instanceID)
lastNode, lastNodeName, voters, err := latestCompletedNodeInHistory(ctx, s.taskDAO.Underlying(), instanceID)
if err != nil {
return fmt.Errorf("failed to resolve last node: %w", err)
}
@@ -145,7 +144,7 @@ func (s *TaskServiceImpl) recallCompleted(ctx context.Context, actor Actor, hi *
}
// 复活实例:同 PK 回插运行表并移除归档行,并发双收回靠主键冲突天然互斥
err = s.taskDAO.Query.Transaction(func(tx *query.Query) error {
err = s.taskDAO.Underlying().Transaction(func(tx *query.Query) error {
revived := &model.WfInstance{
ID: hi.ID,
ProcessID: hi.ProcessID,
@@ -176,7 +175,7 @@ func (s *TaskServiceImpl) recallCompleted(ctx context.Context, actor Actor, hi *
// 重入末节点重建任务。失败则整体退回归档态补偿——不补偿会留下
// active 但零任务的僵尸实例,且在途收回通道救不了它(无 completed 任务可寻)
if err := internal.ExecuteNext(ctx, instanceID, lastNode, nil); err != nil {
if cerr := s.rearchiveCompletedInstance(ctx, s.taskDAO.Query, hi, instanceID); cerr != nil {
if cerr := s.rearchiveCompletedInstance(ctx, s.taskDAO.Underlying(), hi, instanceID); cerr != nil {
return fmt.Errorf("末节点重入失败且补偿失败,实例 %s 可能停留为无任务运行态需人工处理: 重入错误=%v 补偿错误=%w", instanceID, err, cerr)
}
return fmt.Errorf("%w: 末节点重入失败,已回滚为已完成态: %v", ErrValidation, err)
@@ -787,7 +786,7 @@ func hasProxyMark(t *model.WfTask) bool {
// 停泊判定)、前沿终止集、加签豁免共用这一份快照——默认 pageSize=10 会在
// 会签/加签/驳回回跳重跑场景截断,截掉的恰是守卫要看的更晚记录与该终止的
// 前沿任务,收回因此双向失真(误拒或误放行+幽灵待办)。
func listAllInstanceTasksTx(ctx context.Context, taskDAO *dao.TaskDAO, instanceID string) ([]*model.WfTask, error) {
func listAllInstanceTasksTx(ctx context.Context, taskDAO TaskStore, instanceID string) ([]*model.WfTask, error) {
return listAllTasks(ctx, taskDAO, &dto.TaskQuery{InstanceID: &instanceID})
}
+5 -6
View File
@@ -12,7 +12,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/rulego/gflow-engine/dao"
"github.com/rulego/gflow-engine/model"
"github.com/rulego/gflow-engine/types/constants"
"github.com/rulego/gflow-engine/types/dto"
@@ -30,7 +29,7 @@ func (s *TaskServiceImpl) authorizeSignOperator(ctx context.Context, task *model
// authorizeSignOperatorWithDAO 用指定 TaskDAO 校验操作者身份。
// 传 s.taskDAO 用于锁外廉价校验;传 scope.Tasks() 在实例锁内按最新快照复跑(收窄
// TOCTOU 窗口:任务可能在锁外校验后被并发改派)。
func (s *TaskServiceImpl) authorizeSignOperatorWithDAO(ctx context.Context, taskDAO *dao.TaskDAO, task *model.WfTask) error {
func (s *TaskServiceImpl) authorizeSignOperatorWithDAO(ctx context.Context, taskDAO TaskStore, task *model.WfTask) error {
u := GetUserFromCtx(ctx)
if u == nil || u.UserID == "" {
return fmt.Errorf("authentication required: %w", ErrPermissionDenied)
@@ -95,9 +94,9 @@ func (s *TaskServiceImpl) AddSign(ctx context.Context, actor Actor, taskID strin
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.addSignInternal(ctx, bareScope(s.taskDAO.Query), taskID, userIDs, reason)
return s.addSignInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, userIDs, reason)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.addSignInternal(ctx, scope, taskID, userIDs, reason)
})
}
@@ -229,9 +228,9 @@ func (s *TaskServiceImpl) ReduceSign(ctx context.Context, actor Actor, taskID st
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.reduceSignInternal(ctx, bareScope(s.taskDAO.Query), taskID, userIDs, reason)
return s.reduceSignInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, userIDs, reason)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.reduceSignInternal(ctx, scope, taskID, userIDs, reason)
})
}
+8 -8
View File
@@ -36,9 +36,9 @@ func (s *TaskServiceImpl) SetPriority(ctx context.Context, actor Actor, taskID s
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.setPriorityInternal(ctx, bareScope(s.taskDAO.Query), taskID, priority)
return s.setPriorityInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, priority)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.setPriorityInternal(ctx, scope, taskID, priority)
})
}
@@ -98,9 +98,9 @@ func (s *TaskServiceImpl) SetDueDate(ctx context.Context, actor Actor, taskID st
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.setDueDateInternal(ctx, bareScope(s.taskDAO.Query), taskID, dueDate)
return s.setDueDateInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, dueDate)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.setDueDateInternal(ctx, scope, taskID, dueDate)
})
}
@@ -156,9 +156,9 @@ func (s *TaskServiceImpl) SuspendTask(ctx context.Context, actor Actor, taskID s
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.suspendTaskInternal(ctx, bareScope(s.taskDAO.Query), taskID)
return s.suspendTaskInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.suspendTaskInternal(ctx, scope, taskID)
})
}
@@ -222,9 +222,9 @@ func (s *TaskServiceImpl) ActivateTask(ctx context.Context, actor Actor, taskID
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.activateTaskInternal(ctx, bareScope(s.taskDAO.Query), taskID)
return s.activateTaskInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.activateTaskInternal(ctx, scope, taskID)
})
}
+6 -6
View File
@@ -89,9 +89,9 @@ func (s *TaskServiceImpl) SetTaskVariables(ctx context.Context, actor Actor, tas
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.setTaskVariablesInternal(ctx, bareScope(s.taskDAO.Query), taskID, variables)
return s.setTaskVariablesInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, variables)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.setTaskVariablesInternal(ctx, scope, taskID, variables)
})
}
@@ -155,9 +155,9 @@ func (s *TaskServiceImpl) SetTaskVariable(ctx context.Context, actor Actor, task
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.setTaskVariableInternal(ctx, bareScope(s.taskDAO.Query), taskID, variableName, value)
return s.setTaskVariableInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, variableName, value)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.setTaskVariableInternal(ctx, scope, taskID, variableName, value)
})
}
@@ -203,9 +203,9 @@ func (s *TaskServiceImpl) RemoveTaskVariable(ctx context.Context, actor Actor, t
instanceID = *task.ProcessInstanceID
}
if instanceID == "" {
return s.removeTaskVariableInternal(ctx, bareScope(s.taskDAO.Query), taskID, variableName)
return s.removeTaskVariableInternal(ctx, bareScope(s.taskDAO.Underlying()), taskID, variableName)
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.removeTaskVariableInternal(ctx, scope, taskID, variableName)
})
}
+4 -4
View File
@@ -50,7 +50,7 @@ func (s *TaskServiceImpl) Withdraw(ctx context.Context, actor Actor, taskID, rea
}
instanceID := *task.ProcessInstanceID
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.withdrawInternal(ctx, scope, taskID, userID, reason, false)
})
}
@@ -65,7 +65,7 @@ func (s *TaskServiceImpl) WithdrawByInstance(ctx context.Context, actor Actor, i
return fmt.Errorf("instance ID and user ID cannot be empty")
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
taskDAO := scope.Tasks()
q := &dto.TaskQuery{
InstanceID: &instanceID,
@@ -287,7 +287,7 @@ func (s *TaskServiceImpl) Return(ctx context.Context, actor Actor, taskID, targe
if instanceID == "" {
return fmt.Errorf("task has no associated process instance")
}
return WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
return WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
return s.returnInternal(ctx, scope, taskID, targetActivityID, userID, reason)
})
}
@@ -434,7 +434,7 @@ func (s *TaskServiceImpl) SupersedeNodeTasks(ctx context.Context, instanceID, ta
return 0, fmt.Errorf("instanceID and taskDefKey cannot be empty")
}
archived := 0
if err := WithInstanceTx(ctx, s.taskDAO.Query, instanceID, func(scope *InstanceScope) error {
if err := WithInstanceTx(ctx, s.taskDAO.Underlying(), instanceID, func(scope *InstanceScope) error {
n, err := s.supersedeNodeTasksInternal(ctx, scope, instanceID, taskDefKey, reason)
if err != nil {
return err