-
Notifications
You must be signed in to change notification settings - Fork 0
/
statemachine.go
325 lines (280 loc) · 7.68 KB
/
statemachine.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"recipe-walkthrough/models"
"strconv"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/guregu/null.v3"
)
type RecipeInfo struct {
ID null.Int `json:"recipe_id"`
JobIDs []int64 `json:"job_ids"`
CurrentStep *models.Step `json:"current_step"`
PrevStep *models.Step `json:"prev_step"`
NextStep *models.Step `json:"next_step"`
TotalSteps int `json:"total_steps"`
recipe *models.Recipe `json:"-"`
}
type JobPayload struct {
Service string `json:"service"`
ActionKey interface{} `json:"action_key"`
ActionParams interface{} `json:"action_params"`
TriggerKeys []string `json:"trigger_keys"`
TriggerParams []interface{} `json:"trigger_params"`
}
type JobResponse struct {
Status string `json:"status"`
JobID int64 `json:"job_id"`
}
type ClearJob struct {
ID int64 `json:"id"`
}
type ClearJobResponse struct {
ID int64 `json:"id"`
}
// Any special setup for triggers/actions before sending to trigger-queue
// i.e dynamic triggers or actions
func (j *JobPayload) specialJobSetup() error {
for i, trigger := range j.TriggerKeys {
// Convert the delay to a timestamp
if trigger == "timer" {
loc, _ := time.LoadLocation("UTC")
j.TriggerParams[i] = time.Now().In(loc).Add(time.Second * time.Duration(j.TriggerParams[i].(int)))
}
}
return nil
}
// SendJob to the trigger queue
// returns JobID, error
func (j *JobPayload) SendJob() (int64, error) {
j.specialJobSetup()
url := os.Getenv("TRIGGER_QUEUE_API") + "/add"
payload, _ := json.Marshal(j)
log.Infof("Sending to trigger-queue: %s", payload)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return -1, err
}
req.Header.Add("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return -1, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var resp JobResponse
err = decoder.Decode(&resp)
if err != nil {
return -1, err
}
return resp.JobID, nil
}
func (j *ClearJob) clear() (bool, error) {
url := os.Getenv("TRIGGER_QUEUE_API") + "/delete/walk-through/" + strconv.FormatInt(j.ID, 10)
req, _ := http.NewRequest("DELETE", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var resp ClearJobResponse
err := decoder.Decode(&resp)
if err != nil {
return false, err
}
return res.StatusCode == http.StatusOK, nil
}
// incrementNSteps forwards
// returns recipeCompleted, error
func (r *RecipeInfo) incrementNSteps(n int, isAction bool) (bool, error) {
if r.CurrentStep == nil {
return false, errors.New("cannot increment step: no recipe currently active")
}
// Execute jobs if we are incrementing by one step
if n == 1 && len(r.JobIDs) > 0 && !isAction {
log.Info("Execute all current active jobs")
r.executeJobs()
return false, nil
}
log.Info("Incrementing " + strconv.Itoa(n) + " step(s)")
return r.initStep(int(r.CurrentStep.StepNumber.Int64 + int64(n-1)))
}
// Setup a newRecipe given a Recipe ID
func (r *RecipeInfo) newRecipe(id int) error {
// Get recipe from the database
recipe, err := new(models.Recipe).GetByID(database, queries, id)
if err != nil {
return err
}
// Setup Recipe info
r.ID = recipe.ID
r.TotalSteps = len(recipe.Steps)
r.CurrentStep = recipe.Steps[0]
r.PrevStep = nil
r.NextStep = nil
r.JobIDs = make([]int64, 0)
if r.TotalSteps > 1 {
r.NextStep = recipe.Steps[1]
}
r.recipe = recipe
done, err := r.initStep(0)
if err != nil {
return err
}
if done {
log.Warn("Just setup a newRecipe that is already done")
}
return err
}
// clear a Recipe from the state machine and clean up any triggers
// return (successful cleaning up triggers)
func (r *RecipeInfo) clear() error {
r.TotalSteps = 0
r.CurrentStep = nil
r.NextStep = nil
r.PrevStep = nil
log.Info("Clearing Jobs")
_, err := r.clearJobs()
r.JobIDs = make([]int64, 0)
return err
}
func (r *RecipeInfo) clearJobs() ([]int64, error) {
// Clear the jobs from the trigger queue
nonSuccessfulIDs := make([]int64, 0)
erred := false
for _, jobID := range CurrentRecipe.JobIDs {
j := &ClearJob{ID: jobID}
success, err := j.clear()
if err != nil || !success {
erred = true
nonSuccessfulIDs = append(nonSuccessfulIDs, jobID)
if err != nil {
log.Error(err)
}
}
}
if erred {
msg := "could not clear jobs ["
for i, id := range nonSuccessfulIDs {
msg += strconv.FormatInt(id, 10)
if i != len(nonSuccessfulIDs)-1 {
msg += ", "
} else {
msg += "]"
}
}
return nonSuccessfulIDs, errors.New(msg)
}
return nonSuccessfulIDs, nil
}
// Setup a new step in the recipe
// returns recipeCompleted, error
func (r *RecipeInfo) initStep(step int) (bool, error) {
// Check if the recipe is done
if step == r.TotalSteps || r.ID.ValueOrZero() == -1 {
return true, r.clear()
}
// Check if we are given a valid step in our recipe
if step < 0 || step > r.TotalSteps {
log.Errorf("invalid step (%d) when there are only (%d) steps", step, r.TotalSteps)
return false, errors.New("invalid step")
}
// Clear past step jobs
var err error
_, err = CurrentRecipe.clearJobs()
CurrentRecipe.JobIDs = make([]int64, 0)
if err != nil {
log.Error(err.Error())
}
// Setup triggers/jobs
jobs := make([]*JobPayload, 0)
// Construct each JobPayload from each TriggerGroup
for _, triggerGroup := range r.recipe.Steps[step].TriggerGroups {
var job JobPayload
if triggerGroup.ActionParams.Valid {
job.ActionParams = triggerGroup.ActionParams
}
if triggerGroup.ActionKey.Valid {
job.ActionKey = triggerGroup.ActionKey
}
if triggerGroup.Service.Valid {
job.Service = triggerGroup.Service.String
}
// Loop through all the triggers in the trigger group
for _, trigger := range triggerGroup.Triggers {
l, err := strconv.Atoi(trigger.TriggerParams.String)
if err != nil {
log.Error(err.Error())
continue
}
job.TriggerParams = append(job.TriggerParams, l)
job.TriggerKeys = append(job.TriggerKeys, trigger.TriggerType.Key.String)
}
jobs = append(jobs, &job)
}
// Send the jobs to the trigger-queue
for _, j := range jobs {
var id int64
var err error
id, err = j.SendJob()
if err != nil {
log.Error("error sending job %+v", j)
log.Error(err.Error())
} else {
r.JobIDs = append(r.JobIDs, id)
}
}
// Update self
if step > 0 {
r.PrevStep = r.recipe.Steps[step-1]
} else {
r.PrevStep = nil
}
r.CurrentStep = r.recipe.Steps[step]
if step+1 < r.TotalSteps {
r.NextStep = r.recipe.Steps[step+1]
} else {
r.NextStep = nil
}
return false, nil
}
// Send the current step info to the NLP to say to the user
func (r *RecipeInfo) SayCurrentStep() error {
url := os.Getenv("NLP_API") + "/send_message/" + r.CurrentStep.Data.String
log.Infof("Sending to NLP: %s", r.CurrentStep.Data.String)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
log.Infof("NLP Response: %s", body)
return err
}
func (r *RecipeInfo) executeJobs() {
baseURL := os.Getenv("TRIGGER_QUEUE_API") + "/execute/walk-through/"
ids := r.JobIDs
r.JobIDs = make([]int64, 0)
for _, id := range ids {
log.Info("Preparing to execute job %d", id)
url := baseURL + strconv.FormatInt(id, 10)
req, _ := http.NewRequest("POST", url, nil)
res, err := http.DefaultClient.Do(req)
log.Info("Executed job %d", id)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error("error executing job %d", id)
} else {
log.Infof("Trigger Queue Job %d execute response: %s", id, body)
}
}
}