forked from cyverse-de/job-status-listener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (146 loc) · 4.13 KB
/
main.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
package main
import (
"encoding/json"
_ "expvar"
"flag"
"fmt"
"net/http"
"strings"
"github.com/cyverse-de/configurate"
"github.com/spf13/viper"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/cyverse-de/messaging"
)
var log = logrus.WithFields(logrus.Fields{
"service": "job-status-listener",
"art-id": "job-status-listener",
"group": "org.cyverse",
})
var (
cfgPath = flag.String("config", "", "Path to the configuration file.")
cfg *viper.Viper
)
func update(publisher JobUpdatePublisher, state messaging.JobState, jobID string, hostname string, msg string) (*messaging.UpdateMessage, error) {
updateMessage := &messaging.UpdateMessage{
Job: messaging.JobDetails{InvocationID: jobID},
State: state,
Message: msg,
Sender: hostname,
}
err := publisher.PublishJobUpdate(updateMessage)
if err == nil {
log.Infof("%s (%s) [%s]: %s", jobID, state, hostname, msg)
return updateMessage, nil
}
// The first attempt to record the message failed.
log.Errorf("failed to publish job status update: %s", err)
// Attempt to reestablish the connection.
log.Info("attempting to reestablish the messaging connection")
err = publisher.Reconnect()
if err != nil {
log.Errorf("unable to reestablish the messaging connection: %s", err)
return nil, err
}
// Attempt to record the message one more time.
err = publisher.PublishJobUpdate(updateMessage)
if err == nil {
log.Infof("%s (%s) [%s]: %s", jobID, state, hostname, msg)
return updateMessage, nil
}
log.Errorf("failed to publish job status update again - giving up: %s", err)
return nil, err
}
// MessagePost describes the structure of the job status update request body.
type MessagePost struct {
Hostname string
Message string
State string
}
func getState(state string) (messaging.JobState, error) {
switch strings.ToLower(state) {
case "submitted":
return messaging.SubmittedState, nil
case "running":
return messaging.RunningState, nil
case "completed":
return messaging.SucceededState, nil
case "succeeded":
return messaging.SucceededState, nil
case "failed":
return messaging.FailedState, nil
default:
return "", fmt.Errorf("Unknown job state: %s", state)
}
}
func postUpdate(publisher JobUpdatePublisher, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
out := json.NewEncoder(w)
var updateMessage MessagePost
vars := mux.Vars(r)
jobID := vars["uuid"]
err := json.NewDecoder(r.Body).Decode(&updateMessage)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Error(err)
_ = out.Encode(map[string]string{
"error": err.Error(),
})
return
}
state, err := getState(updateMessage.State)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Error(err)
_ = out.Encode(map[string]string{
"error": err.Error(),
})
return
}
msg, err := update(publisher, state, jobID, updateMessage.Hostname, updateMessage.Message)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Error(err)
_ = out.Encode(map[string]string{
"error": err.Error(),
})
log.Fatal("failed to record a valid job status update - aborting")
}
_ = out.Encode(msg)
}
func init() {
flag.Parse()
logrus.SetFormatter(&logrus.JSONFormatter{})
}
func loadConfig(cfgPath string) {
var err error
cfg, err = configurate.Init(cfgPath)
if err != nil {
log.Fatal(err)
}
}
func newRouter(publisher JobUpdatePublisher) *mux.Router {
r := mux.NewRouter()
r.Handle("/debug/vars", http.DefaultServeMux)
r.Path("/{uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}/status").Methods("POST").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
postUpdate(publisher, w, r)
},
)
return r
}
func main() {
log.Info("Starting up the job-status-listener service.")
loadConfig(*cfgPath)
uri := cfg.GetString("amqp.uri")
exchange := cfg.GetString("amqp.exchange.name")
publisher, err := NewDefaultJobUpdatePublisher(uri, exchange)
if err != nil {
log.Fatal(err)
}
defer publisher.Close()
r := newRouter(publisher)
listenPortSpec := ":" + "60000"
log.Infof("Listening on %s", listenPortSpec)
log.Fatal(http.ListenAndServe(listenPortSpec, r))
}