-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrand-routes.go
309 lines (254 loc) · 7.18 KB
/
errand-routes.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
package main
import (
"errors"
"fmt"
"net/http"
gin "github.com/gin-gonic/gin"
"github.com/polygon-io/errands-server/metrics"
schemas "github.com/polygon-io/errands-server/schemas"
utils "github.com/polygon-io/errands-server/utils"
)
const inactive = "inactive"
const active = "active"
type UpdateRequest struct {
Progress float64 `json:"progress"`
Logs []string `json:"logs"`
}
func (s *ErrandsServer) updateErrand(c *gin.Context) {
var (
updatedErrand *schemas.Errand
updateReq UpdateRequest
)
if err := c.ShouldBind(&updateReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func(errand *schemas.Errand) error {
if errand.Status != active {
return errors.New("errand must be in active state to update progress")
}
// Update this errand attributes:
if updateReq.Progress != 0 {
if updateReq.Progress < 0 || updateReq.Progress >= 101 {
return errors.New("progress must be between 0 - 100")
}
errand.Progress = updateReq.Progress
}
return nil
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return
}
s.AddNotification("updated", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"results": updatedErrand,
})
}
type FailedRequest struct {
Reason string `json:"reason" binding:"required"`
}
func (s *ErrandsServer) failedErrand(c *gin.Context) {
var (
updatedErrand *schemas.Errand
failedReq FailedRequest
)
if err := c.ShouldBind(&failedReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func(errand *schemas.Errand) error {
return failErrand(errand, failedReq)
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return
}
s.AddNotification("failed", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"results": updatedErrand,
})
}
func failErrand(errand *schemas.Errand, failureRequest FailedRequest) error {
// Update this errand attributes:
if err := errand.AddToLogs("ERROR", failureRequest.Reason); err != nil {
return err
}
errand.Failed = utils.GetTimestamp()
errand.Status = schemas.StatusFailed
errand.Progress = 0
if errand.Options.Retries > 0 {
// If we should retry this errand:
if errand.Attempts <= errand.Options.Retries {
errand.Status = inactive
} else {
// If this errand is out of retries
metrics.ErrandFailed(errand.Type)
}
} else {
// If this errand was not configured with retries
metrics.ErrandFailed(errand.Type)
}
return nil
}
type CompletedRequest struct {
Results *gin.H `json:"results"`
}
func (s *ErrandsServer) completeErrand(c *gin.Context) {
var (
updatedErrand *schemas.Errand
compReq CompletedRequest
)
if err := c.ShouldBind(&compReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
})
return
}
shouldDelete := false
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func(errand *schemas.Errand) error {
// Update this errand attributes:
if err := errand.AddToLogs("INFO", "Completed!"); err != nil {
return err
}
errand.Completed = utils.GetTimestamp()
errand.Status = schemas.StatusCompleted
errand.Progress = 100
// errand.Results = compReq.Results
// If we should delete this errand upon completion:
if errand.Options.DeleteOnCompleted {
shouldDelete = true
}
metrics.ErrandCompleted(errand.Type)
return nil
})
if err == nil && shouldDelete && updatedErrand.ID != "" {
s.deleteErrandByID(updatedErrand.ID)
}
if shouldDelete && updatedErrand.ID != "" {
s.deleteErrandByID(updatedErrand.ID)
}
s.AddNotification("completed", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"results": updatedErrand,
})
}
func (s *ErrandsServer) retryErrand(c *gin.Context) {
var updatedErrand *schemas.Errand
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func(errand *schemas.Errand) error {
if errand.Status == inactive {
return errors.New("cannot retry errand which is in inactive state")
}
// Update this errand attributes:
if err := errand.AddToLogs("INFO", "Retrying!"); err != nil {
return err
}
errand.Status = inactive
errand.Progress = 0
return nil
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return
}
s.AddNotification("retry", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"results": updatedErrand,
})
}
func (s *ErrandsServer) logToErrand(c *gin.Context) {
var logReq schemas.Log
if err := c.ShouldBind(&logReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func(errand *schemas.Errand) error {
if errand.Status != active {
return errors.New("errand must be in active state to log to")
}
// Update this errand attributes:
return errand.AddToLogs(logReq.Severity, logReq.Message)
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"results": updatedErrand,
})
}
func (s *ErrandsServer) deleteErrand(c *gin.Context) {
s.ErrandStore.Delete(c.Param("id"))
s.deleteErrandByID(c.Param("id"))
c.JSON(http.StatusOK, gin.H{
"status": "OK",
})
}
func (s *ErrandsServer) getErrand(c *gin.Context) {
errandObj, found := s.ErrandStore.Get(c.Param("id"))
if !found {
c.JSON(http.StatusNotFound, nil)
}
errand := errandObj.(schemas.Errand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"results": errand,
})
}
func (s *ErrandsServer) deleteErrandByID(id string) {
s.ErrandStore.Delete(id)
}
// UpdateErrandByID Lets you pass in a function which will be called allowing you to update the errand. If no error is returned, the errand will be saved in the DB with the new attributes.
func (s *ErrandsServer) UpdateErrandByID(id string, update func(*schemas.Errand) error) (*schemas.Errand, error) {
errandObj, found := s.ErrandStore.Get(id)
if !found {
return nil, errors.New("errand with this ID not found")
}
errand := errandObj.(schemas.Errand)
if err := update(&errand); err != nil {
return nil, fmt.Errorf("error in given update function (fn): %w", err)
}
s.updateErrandInPipeline(&errand)
s.ErrandStore.SetDefault(id, errand)
return &errand, nil
}
func (s *ErrandsServer) UpdateErrandsByFilter(filter func(*schemas.Errand) bool, update func(*schemas.Errand) error) error {
for _, itemObj := range s.ErrandStore.Items() {
errand := itemObj.Object.(schemas.Errand)
if filter(&errand) {
if err := update(&errand); err != nil {
return fmt.Errorf("error in given update function (fn): %w", err)
}
s.updateErrandInPipeline(&errand)
s.ErrandStore.SetDefault(errand.ID, errand)
}
}
return nil
}