-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathrouters.go
110 lines (97 loc) · 2.86 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
package wf_ignore_already_started
import (
"github.com/gin-gonic/gin"
"github.com/indeedeng/iwf/gen/iwfidl"
"github.com/indeedeng/iwf/service"
"github.com/indeedeng/iwf/service/common/ptr"
"log"
"net/http"
"sync"
"testing"
)
/**
* This test workflow has 1 state, using REST controller to implement the workflow directly.
*
* State1:
* - Waits on a timer
* - Execute method will gracefully complete workflow
*/
const (
WorkflowType = "wf_ignore_already_started"
State1 = "S1"
)
type handler struct {
invokeHistory sync.Map
}
func NewHandler() *handler {
return &handler{
invokeHistory: 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 {
c.JSON(http.StatusOK, iwfidl.WorkflowStateStartResponse{
CommandRequest: &iwfidl.CommandRequest{
TimerCommands: []iwfidl.TimerCommand{
{
CommandId: ptr.Any("timer-cmd-id"),
DurationSeconds: iwfidl.PtrInt64(10), // fire after 10s
},
},
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 {
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
})
return invokeHistory, nil
}