-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathrouters.go
174 lines (157 loc) · 5.34 KB
/
routers.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
package anytimersignal
import (
"github.com/gin-gonic/gin"
"github.com/indeedeng/iwf/gen/iwfidl"
"github.com/indeedeng/iwf/integ/workflow/common"
"github.com/indeedeng/iwf/service"
"github.com/indeedeng/iwf/service/common/ptr"
"log"
"net/http"
"sync"
"testing"
"time"
)
/**
* This test workflow has 2 states, using REST controller to implement the workflow directly.
*
* State1:
* - WaitUntil will wait for the signals to trigger.
* - Execute method will trigger signals and retry State1 once, then trigger signals and move the State2
* State2:
* - Waits on nothing. Will execute momentarily
* - Execute method will gracefully complete workflow
*/
const (
WorkflowType = "any_timer_signal"
State1 = "S1"
State2 = "S2"
SignalName = "test-signal-name"
)
type handler struct {
invokeHistory sync.Map
invokeData sync.Map
}
func NewHandler() common.WorkflowHandler {
return &handler{
invokeHistory: sync.Map{},
invokeData: sync.Map{},
}
}
// ApiV1WorkflowStartPost - for a workflow
func (h *handler) ApiV1WorkflowStateStart(c *gin.Context, t *testing.T) {
var req iwfidl.WorkflowStateStartRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
log.Println("received state start request, ", req)
if req.GetWorkflowType() == WorkflowType {
if value, ok := h.invokeHistory.Load(req.GetWorkflowStateId() + "_start"); ok {
h.invokeHistory.Store(req.GetWorkflowStateId()+"_start", value.(int64)+1)
} else {
h.invokeHistory.Store(req.GetWorkflowStateId()+"_start", int64(1))
}
if req.GetWorkflowStateId() == State1 {
var timerCommands []iwfidl.TimerCommand
context := req.GetContext()
// Fire timer after 1s on first start attempt
if context.GetStateExecutionId() == State1+"-"+"1" {
now := time.Now().Unix()
timerCommands = []iwfidl.TimerCommand{
{
FiringUnixTimestampSeconds: iwfidl.PtrInt64(now + 1), // fire after 1s
},
}
}
c.JSON(http.StatusOK, iwfidl.WorkflowStateStartResponse{
CommandRequest: &iwfidl.CommandRequest{
SignalCommands: []iwfidl.SignalCommand{
{
CommandId: ptr.Any("signal-cmd-id"),
SignalChannelName: SignalName,
},
},
TimerCommands: timerCommands,
CommandWaitingType: ptr.Any(iwfidl.ANY_COMPLETED),
},
})
return
}
if req.GetWorkflowStateId() == State2 {
// Go straight to the decide methods without any commands
c.JSON(http.StatusOK, iwfidl.WorkflowStateStartResponse{
CommandRequest: &iwfidl.CommandRequest{
DeciderTriggerType: iwfidl.ALL_COMMAND_COMPLETED.Ptr(),
},
})
return
}
}
c.JSON(http.StatusBadRequest, struct{}{})
}
func (h *handler) ApiV1WorkflowStateDecide(c *gin.Context, t *testing.T) {
var req iwfidl.WorkflowStateDecideRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
log.Println("received state decide request, ", req)
if req.GetWorkflowType() == WorkflowType {
if value, ok := h.invokeHistory.Load(req.GetWorkflowStateId() + "_decide"); ok {
h.invokeHistory.Store(req.GetWorkflowStateId()+"_decide", value.(int64)+1)
} else {
h.invokeHistory.Store(req.GetWorkflowStateId()+"_decide", int64(1))
}
if req.GetWorkflowStateId() == State1 {
signalResults := req.GetCommandResults()
var movements []iwfidl.StateMovement
context := req.GetContext()
// On first State 1 attempt, trigger signals and stay on the first state
if context.GetStateExecutionId() == State1+"-"+"1" {
h.invokeData.Store("signalChannelName1", signalResults.SignalResults[0].GetSignalChannelName())
h.invokeData.Store("signalCommandId1", signalResults.SignalResults[0].GetCommandId())
h.invokeData.Store("signalStatus1", signalResults.SignalResults[0].GetSignalRequestStatus())
movements = []iwfidl.StateMovement{{StateId: State1}}
} else {
// After the first State 1 attempt, trigger signals and move to next state
h.invokeData.Store("signalChannelName2", signalResults.SignalResults[0].GetSignalChannelName())
h.invokeData.Store("signalCommandId2", signalResults.SignalResults[0].GetCommandId())
h.invokeData.Store("signalStatus2", signalResults.SignalResults[0].GetSignalRequestStatus())
h.invokeData.Store("signalValue2", signalResults.SignalResults[0].GetSignalValue())
movements = []iwfidl.StateMovement{{StateId: State2}}
}
c.JSON(http.StatusOK, iwfidl.WorkflowStateDecideResponse{
StateDecision: &iwfidl.StateDecision{
NextStates: movements,
},
})
return
} else if req.GetWorkflowStateId() == State2 {
// Move to completion
c.JSON(http.StatusOK, iwfidl.WorkflowStateDecideResponse{
StateDecision: &iwfidl.StateDecision{
NextStates: []iwfidl.StateMovement{
{
StateId: service.GracefulCompletingWorkflowStateId,
},
},
},
})
return
}
}
c.JSON(http.StatusBadRequest, struct{}{})
}
func (h *handler) GetTestResult() (map[string]int64, map[string]interface{}) {
invokeHistory := make(map[string]int64)
h.invokeHistory.Range(func(key, value interface{}) bool {
invokeHistory[key.(string)] = value.(int64)
return true
})
invokeData := make(map[string]interface{})
h.invokeData.Range(func(key, value interface{}) bool {
invokeData[key.(string)] = value
return true
})
return invokeHistory, invokeData
}