@@ -33,20 +33,24 @@ import (
3333 enumspb "go.temporal.io/api/enums/v1"
3434
3535 "go.temporal.io/server/common"
36+ "go.temporal.io/server/common/clock"
3637 "go.temporal.io/server/common/definition"
3738 "go.temporal.io/server/common/metrics"
3839 "go.temporal.io/server/common/namespace"
40+ "go.temporal.io/server/common/persistence"
3941 "go.temporal.io/server/common/searchattribute"
4042 "go.temporal.io/server/service/history/configs"
4143 "go.temporal.io/server/service/history/consts"
4244 "go.temporal.io/server/service/history/shard"
45+ "go.temporal.io/server/service/history/tasks"
4346 "go.temporal.io/server/service/worker/archiver"
4447)
4548
4649type (
4750 DeleteManager interface {
48- DeleteWorkflowExecution (namespaceID namespace.ID , we commonpb.WorkflowExecution , weCtx Context , ms MutableState , sourceTaskVersion int64 ) error
49- DeleteWorkflowExecutionByRetention (namespaceID namespace.ID , we commonpb.WorkflowExecution , weCtx Context , ms MutableState , sourceTaskVersion int64 ) error
51+ AddDeleteWorkflowExecutionTask (nsID namespace.ID , we commonpb.WorkflowExecution , ms MutableState ) error
52+ DeleteWorkflowExecution (nsID namespace.ID , we commonpb.WorkflowExecution , weCtx Context , ms MutableState , sourceTaskVersion int64 ) error
53+ DeleteWorkflowExecutionByRetention (nsID namespace.ID , we commonpb.WorkflowExecution , weCtx Context , ms MutableState , sourceTaskVersion int64 ) error
5054 }
5155
5256 DeleteManagerImpl struct {
5559 config * configs.Config
5660 metricsClient metrics.Client
5761 archivalClient archiver.Client
62+ timeSource clock.TimeSource
5863 }
5964)
6065
@@ -65,20 +70,59 @@ func NewDeleteManager(
6570 cache Cache ,
6671 config * configs.Config ,
6772 archiverClient archiver.Client ,
73+ timeSource clock.TimeSource ,
6874) * DeleteManagerImpl {
6975 deleteManager := & DeleteManagerImpl {
7076 shard : shard ,
7177 historyCache : cache ,
7278 metricsClient : shard .GetMetricsClient (),
7379 config : config ,
7480 archivalClient : archiverClient ,
81+ timeSource : timeSource ,
7582 }
7683
7784 return deleteManager
7885}
86+ func (m * DeleteManagerImpl ) AddDeleteWorkflowExecutionTask (
87+ nsID namespace.ID ,
88+ we commonpb.WorkflowExecution ,
89+ ms MutableState ,
90+ ) error {
91+
92+ if ms .IsWorkflowExecutionRunning () {
93+ // Running workflow cannot be deleted. Close or terminate it first.
94+ return consts .ErrWorkflowNotCompleted
95+ }
96+
97+ taskGenerator := NewTaskGenerator (
98+ m .shard .GetNamespaceRegistry (),
99+ ms ,
100+ )
101+
102+ deleteTask , err := taskGenerator .GenerateDeleteExecutionTask (m .timeSource .Now ())
103+ if err != nil {
104+ return err
105+ }
106+
107+ err = m .shard .AddTasks (& persistence.AddTasksRequest {
108+ ShardID : m .shard .GetShardID (),
109+ // RangeID is set by shard
110+ NamespaceID : nsID .String (),
111+ WorkflowID : we .GetWorkflowId (),
112+ RunID : we .GetRunId (),
113+ Tasks : map [tasks.Category ][]tasks.Task {
114+ tasks .CategoryTransfer : {deleteTask },
115+ },
116+ })
117+ if err != nil {
118+ return err
119+ }
120+
121+ return nil
122+ }
79123
80124func (m * DeleteManagerImpl ) DeleteWorkflowExecution (
81- namespaceID namespace.ID ,
125+ nsID namespace.ID ,
82126 we commonpb.WorkflowExecution ,
83127 weCtx Context ,
84128 ms MutableState ,
@@ -94,7 +138,7 @@ func (m *DeleteManagerImpl) DeleteWorkflowExecution(
94138 }
95139
96140 err := m .deleteWorkflowExecutionInternal (
97- namespaceID ,
141+ nsID ,
98142 we ,
99143 weCtx ,
100144 ms ,
@@ -107,7 +151,7 @@ func (m *DeleteManagerImpl) DeleteWorkflowExecution(
107151}
108152
109153func (m * DeleteManagerImpl ) DeleteWorkflowExecutionByRetention (
110- namespaceID namespace.ID ,
154+ nsID namespace.ID ,
111155 we commonpb.WorkflowExecution ,
112156 weCtx Context ,
113157 ms MutableState ,
@@ -122,7 +166,7 @@ func (m *DeleteManagerImpl) DeleteWorkflowExecutionByRetention(
122166 }
123167
124168 err := m .deleteWorkflowExecutionInternal (
125- namespaceID ,
169+ nsID ,
126170 we ,
127171 weCtx ,
128172 ms ,
0 commit comments