/
workflow.go
159 lines (142 loc) · 5.16 KB
/
workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package parentclosepolicy
import (
"context"
"time"
"go.uber.org/cadence"
"go.uber.org/cadence/activity"
"go.uber.org/cadence/workflow"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/types"
)
type (
contextKey string
)
const (
processorContextKey contextKey = "processorContext"
// processorTaskListName is the tasklist name
processorTaskListName = "cadence-sys-processor-parent-close-policy"
// processorWFTypeName is the workflow type
processorWFTypeName = "cadence-sys-parent-close-policy-workflow"
processorActivityName = "cadence-sys-parent-close-policy-activity"
infiniteDuration = 20 * 365 * 24 * time.Hour
processorChannelName = "ParentClosePolicyProcessorChannelName"
)
type (
// RequestDetail defines detail of each workflow to process
RequestDetail struct {
WorkflowID string
RunID string
Policy types.ParentClosePolicy
}
// Request defines the request for parent close policy
Request struct {
Executions []RequestDetail
DomainName string
DomainUUID string
}
)
var (
retryPolicy = cadence.RetryPolicy{
InitialInterval: 10 * time.Second,
BackoffCoefficient: 1.7,
MaximumInterval: 5 * time.Minute,
ExpirationInterval: infiniteDuration,
}
activityOptions = workflow.ActivityOptions{
ScheduleToStartTimeout: time.Minute,
StartToCloseTimeout: 5 * time.Minute,
RetryPolicy: &retryPolicy,
}
)
func init() {
workflow.RegisterWithOptions(ProcessorWorkflow, workflow.RegisterOptions{Name: processorWFTypeName})
activity.RegisterWithOptions(ProcessorActivity, activity.RegisterOptions{Name: processorActivityName})
}
// ProcessorWorkflow is the workflow that performs actions for ParentClosePolicy
func ProcessorWorkflow(ctx workflow.Context) error {
requestCh := workflow.GetSignalChannel(ctx, processorChannelName)
for {
var request Request
if !requestCh.ReceiveAsync(&request) {
// no more request
break
}
opt := workflow.WithActivityOptions(ctx, activityOptions)
_ = workflow.ExecuteActivity(opt, processorActivityName, request).Get(ctx, nil)
}
return nil
}
// ProcessorActivity is activity for processing batch operation
func ProcessorActivity(ctx context.Context, request Request) error {
processor := ctx.Value(processorContextKey).(*Processor)
for _, execution := range request.Executions {
var err error
switch execution.Policy {
case types.ParentClosePolicyAbandon:
//no-op
continue
case types.ParentClosePolicyTerminate:
err = processor.frontendClient.TerminateWorkflowExecution(ctx, &types.TerminateWorkflowExecutionRequest{
Domain: request.DomainName,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: execution.WorkflowID,
RunID: execution.RunID,
},
Reason: "by parent close policy",
Identity: processorWFTypeName,
})
case types.ParentClosePolicyRequestCancel:
err = processor.frontendClient.RequestCancelWorkflowExecution(ctx, &types.RequestCancelWorkflowExecutionRequest{
Domain: request.DomainName,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: execution.WorkflowID,
RunID: execution.RunID,
},
Identity: processorWFTypeName,
})
}
if err != nil {
switch err.(type) {
case *types.EntityNotExistsError, *types.CancellationAlreadyRequestedError:
err = nil
}
}
if err != nil {
processor.metricsClient.IncCounter(metrics.ParentClosePolicyProcessorScope, metrics.ParentClosePolicyProcessorFailures)
getActivityLogger(ctx).Error("failed to process parent close policy", tag.Error(err))
return err
}
processor.metricsClient.IncCounter(metrics.ParentClosePolicyProcessorScope, metrics.ParentClosePolicyProcessorSuccess)
}
return nil
}
func getActivityLogger(ctx context.Context) log.Logger {
processor := ctx.Value(processorContextKey).(*Processor)
wfInfo := activity.GetInfo(ctx)
return processor.logger.WithTags(
tag.WorkflowID(wfInfo.WorkflowExecution.ID),
tag.WorkflowRunID(wfInfo.WorkflowExecution.RunID),
tag.WorkflowDomainName(wfInfo.WorkflowDomain),
)
}