-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworkflow.go
247 lines (202 loc) · 8.86 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package engagement
import (
"fmt"
"github.com/indeedeng/iwf-golang-samples/workflows/service"
"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
"github.com/indeedeng/iwf-golang-sdk/iwf"
"time"
)
func NewEngagementWorkflow(svc service.MyService) iwf.ObjectWorkflow {
return &EngagementWorkflow{
svc: svc,
}
}
type EngagementWorkflow struct {
iwf.DefaultWorkflowType
svc service.MyService
}
func (e EngagementWorkflow) GetWorkflowStates() []iwf.StateDef {
return []iwf.StateDef{
iwf.StartingStateDef(NewInitState()),
iwf.NonStartingStateDef(NewProcessTimoutState(e.svc)),
iwf.NonStartingStateDef(NewReminderState(e.svc)),
iwf.NonStartingStateDef(NewNotifyExternalSystemState(e.svc)),
}
}
func (e EngagementWorkflow) GetPersistenceSchema() []iwf.PersistenceFieldDef {
return []iwf.PersistenceFieldDef{
iwf.SearchAttributeDef(keyEmployerId, iwfidl.KEYWORD),
iwf.SearchAttributeDef(keyJobSeekerId, iwfidl.KEYWORD),
iwf.SearchAttributeDef(keyStatus, iwfidl.KEYWORD),
iwf.SearchAttributeDef(keyLastUpdateTimestamp, iwfidl.INT),
iwf.DataAttributeDef(keyNotes),
}
}
func (e EngagementWorkflow) GetCommunicationSchema() []iwf.CommunicationMethodDef {
return []iwf.CommunicationMethodDef{
iwf.SignalChannelDef(SignalChannelOptOutReminder),
iwf.InternalChannelDef(InternalChannelCompleteProcess),
iwf.RPCMethodDef(e.Describe, nil),
iwf.RPCMethodDef(e.Decline, nil),
iwf.RPCMethodDef(e.Accept, nil),
}
}
const (
keyEmployerId = "EmployerId"
keyJobSeekerId = "JobSeekerId"
keyStatus = "EngagementStatus"
keyLastUpdateTimestamp = "LastUpdateTimeMillis"
keyNotes = "notes"
SignalChannelOptOutReminder = "OptOutReminder"
InternalChannelCompleteProcess = "CompleteProcess"
)
func (e EngagementWorkflow) Describe(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (interface{}, error) {
status := persistence.GetSearchAttributeKeyword(keyStatus)
employerId := persistence.GetSearchAttributeKeyword(keyEmployerId)
jobSeekerId := persistence.GetSearchAttributeKeyword(keyJobSeekerId)
var notes string
persistence.GetDataAttribute(keyNotes, ¬es)
return EngagementDescription{
EmployerId: employerId,
JobSeekerId: jobSeekerId,
Notes: notes,
CurrentStatus: Status(status),
}, nil
}
func (e EngagementWorkflow) Decline(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (interface{}, error) {
status := Status(persistence.GetSearchAttributeKeyword(keyStatus))
if status != StatusInitiated {
return nil, fmt.Errorf("can only decline in INITIATED status, current is %v", status)
}
persistence.SetSearchAttributeKeyword(keyStatus, string(StatusDeclined))
persistence.SetSearchAttributeInt(keyLastUpdateTimestamp, time.Now().Unix())
communication.TriggerStateMovements(iwf.NewStateMovement(notifyExternalSystemState{}, string(StatusDeclined)))
var notes string
input.Get(¬es)
var currentNotes string
persistence.GetDataAttribute(keyNotes, ¤tNotes)
persistence.SetDataAttribute(keyNotes, currentNotes+";"+notes)
return nil, nil
}
func (e EngagementWorkflow) Accept(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (interface{}, error) {
status := Status(persistence.GetSearchAttributeKeyword(keyStatus))
if status != StatusInitiated && status != StatusDeclined {
return nil, fmt.Errorf("can only decline in INITIATED or DECLINED status, current is %v", status)
}
persistence.SetSearchAttributeKeyword(keyStatus, string(StatusAccepted))
persistence.SetSearchAttributeInt(keyLastUpdateTimestamp, time.Now().Unix())
communication.TriggerStateMovements(iwf.NewStateMovement(notifyExternalSystemState{}, string(StatusAccepted)))
var notes string
input.Get(¬es)
var currentNotes string
persistence.GetDataAttribute(keyNotes, ¤tNotes)
persistence.SetDataAttribute(keyNotes, currentNotes+";"+notes)
return nil, nil
}
func NewInitState() iwf.WorkflowState {
return initState{}
}
type initState struct {
iwf.WorkflowStateDefaultsNoWaitUntil
}
func (i initState) Execute(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) {
var engInput EngagementInput
input.Get(&engInput)
persistence.SetSearchAttributeKeyword(keyEmployerId, engInput.EmployerId)
persistence.SetSearchAttributeKeyword(keyJobSeekerId, engInput.JobSeekerId)
persistence.SetSearchAttributeKeyword(keyStatus, string(StatusInitiated))
persistence.SetDataAttribute(keyNotes, engInput.Notes)
return iwf.MultiNextStatesWithInput(
iwf.NewStateMovement(processTimoutState{}, nil),
iwf.NewStateMovement(reminderState{}, nil),
iwf.NewStateMovement(notifyExternalSystemState{}, StatusInitiated),
), nil
}
func NewProcessTimoutState(svc service.MyService) iwf.WorkflowState {
return processTimoutState{
svc: svc,
}
}
type processTimoutState struct {
iwf.WorkflowStateDefaults
svc service.MyService
}
func (p processTimoutState) WaitUntil(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (*iwf.CommandRequest, error) {
return iwf.AnyCommandCompletedRequest(
iwf.NewTimerCommand("", time.Now().Add(time.Hour*24*60)), // ~ 2 months
iwf.NewInternalChannelCommand("", InternalChannelCompleteProcess),
), nil
}
func (p processTimoutState) Execute(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) {
status := persistence.GetSearchAttributeKeyword(keyStatus)
employerId := persistence.GetSearchAttributeKeyword(keyEmployerId)
jobSeekerId := persistence.GetSearchAttributeKeyword(keyJobSeekerId)
updateStatus := "timeout"
if status == string(StatusAccepted) {
updateStatus = "done"
}
p.svc.UpdateExternalSystem(fmt.Sprintf("notify engagement from employer %v, jobSeeker %v for status %v", employerId, jobSeekerId, status))
return iwf.GracefulCompleteWorkflow(updateStatus), nil
}
func NewReminderState(svc service.MyService) iwf.WorkflowState {
return reminderState{
svc: svc,
}
}
type reminderState struct {
iwf.WorkflowStateDefaults
svc service.MyService
}
func (r reminderState) WaitUntil(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (*iwf.CommandRequest, error) {
return iwf.AnyCommandCompletedRequest(
iwf.NewTimerCommand("", time.Now().Add(time.Second*5)), // use 5 seconds for demo, should be 24 hours in real world
iwf.NewSignalCommand("", SignalChannelOptOutReminder),
), nil
}
func (r reminderState) Execute(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) {
status := persistence.GetSearchAttributeKeyword(keyStatus)
if status != string(StatusInitiated) {
return iwf.DeadEnd, nil
}
optoutSignalCommandResult := commandResults.Signals[0]
if optoutSignalCommandResult.Status == iwfidl.RECEIVED {
var currentNotes string
persistence.GetDataAttribute(keyNotes, ¤tNotes)
persistence.SetDataAttribute(keyNotes, currentNotes+";"+"User optout reminder")
return iwf.DeadEnd, nil
}
jobSeekerId := persistence.GetSearchAttributeKeyword(keyJobSeekerId)
r.svc.SendEmail(jobSeekerId, "Reminder:xxx please respond", "Hello xxx, ...")
return iwf.SingleNextState(reminderState{}, nil), nil
}
func NewNotifyExternalSystemState(svc service.MyService) iwf.WorkflowState {
return notifyExternalSystemState{
svc: svc,
}
}
type notifyExternalSystemState struct {
iwf.WorkflowStateDefaultsNoWaitUntil
svc service.MyService
}
func (n notifyExternalSystemState) Execute(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) {
var status Status
input.Get(&status)
jobSeekerId := persistence.GetSearchAttributeKeyword(keyJobSeekerId)
employerId := persistence.GetSearchAttributeKeyword(keyEmployerId)
n.svc.UpdateExternalSystem(fmt.Sprintf("notify engagement from employerId %v to jobSeekerId %v for status %v ", employerId, jobSeekerId, status))
return iwf.DeadEnd, nil
}
// GetStateOptions customize the state options
// By default, all state execution will retry infinitely (until workflow timeout).
// This may not work for some dependency as we may want to retry for only a certain times
func (n notifyExternalSystemState) GetStateOptions() *iwf.StateOptions {
return &iwf.StateOptions{
ExecuteApiRetryPolicy: &iwfidl.RetryPolicy{
BackoffCoefficient: iwfidl.PtrFloat32(2),
MaximumAttempts: iwfidl.PtrInt32(100),
MaximumAttemptsDurationSeconds: iwfidl.PtrInt32(3600),
MaximumIntervalSeconds: iwfidl.PtrInt32(60),
InitialIntervalSeconds: iwfidl.PtrInt32(3),
},
}
}