forked from eremetic-framework/eremetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.go
62 lines (50 loc) · 1.25 KB
/
callback.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
package eremetic
import (
"bytes"
"encoding/json"
"net/http"
"github.com/Sirupsen/logrus"
)
// CallbackData holds information about the status update.
type CallbackData struct {
Time int64 `json:"time"`
Status string `json:"status"`
TaskID string `json:"task_id"`
}
// NotifyCallback handles posting a JSON back to the URI given with the task.
func NotifyCallback(task *Task) {
if len(task.CallbackURI) == 0 {
return
}
if len(task.Status) == 0 {
return
}
status := task.Status[len(task.Status)-1]
data := CallbackData{
Time: status.Time,
Status: status.Status.String(),
TaskID: task.ID,
}
body, err := json.Marshal(data)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"task_id": task.ID,
"callback_uri": task.CallbackURI,
}).Error("Unable to create callback message")
return
}
go func() {
_, err = http.Post(task.CallbackURI, "application/json", bytes.NewBuffer(body))
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"task_id": task.ID,
"callback_uri": task.CallbackURI,
}).Error("Unable to POST to Callback URI")
return
}
logrus.WithFields(logrus.Fields{
"task_id": task.ID,
"callback_uri": task.CallbackURI,
}).Debug("Sent callback")
}()
}