-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathrouters.go
164 lines (148 loc) · 4.87 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
package anycommandclose
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"
)
/**
* This test workflow has 2 states, using REST controller to implement the workflow directly.
*
* State1:
* - WaitUntil wait until a signal is received
* - Execute method will fire the signal and move the State2
* State2:
* - Waits on nothing. Will execute momentarily
* - Execute method will gracefully complete workflow
*/
const (
WorkflowType = "any_command_close"
State1 = "S1"
State2 = "S2"
SignalName1 = "test-signal-name1"
SignalName2 = "test-signal-name2"
)
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 {
// Proceed after either signal is received
c.JSON(http.StatusOK, iwfidl.WorkflowStateStartResponse{
CommandRequest: &iwfidl.CommandRequest{
SignalCommands: []iwfidl.SignalCommand{
{
CommandId: ptr.Any("signal-cmd-id1"),
SignalChannelName: SignalName1,
},
{
CommandId: ptr.Any("signal-cmd-id2"),
SignalChannelName: SignalName2,
},
},
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()
h.invokeData.Store("signalCommandResultsLength", len(signalResults.SignalResults))
// Trigger signals
h.invokeData.Store("signalChannelName0", signalResults.SignalResults[0].GetSignalChannelName())
h.invokeData.Store("signalCommandId0", signalResults.SignalResults[0].GetCommandId())
h.invokeData.Store("signalStatus0", signalResults.SignalResults[0].GetSignalRequestStatus())
h.invokeData.Store("signalChannelName1", signalResults.SignalResults[1].GetSignalChannelName())
h.invokeData.Store("signalCommandId1", signalResults.SignalResults[1].GetCommandId())
h.invokeData.Store("signalStatus1", signalResults.SignalResults[1].GetSignalRequestStatus())
h.invokeData.Store("signalValue1", signalResults.SignalResults[1].GetSignalValue())
// Move to State 2
c.JSON(http.StatusOK, iwfidl.WorkflowStateDecideResponse{
StateDecision: &iwfidl.StateDecision{
NextStates: []iwfidl.StateMovement{
{
StateId: State2,
},
},
},
})
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
}