-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
77 lines (61 loc) · 1.61 KB
/
job.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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
log "github.com/sirupsen/logrus"
)
type (
Job struct {
ID string `json:"id"`
Payload string `json:"payload"`
}
agentExecRes struct {
Message string `json:"message"`
Error string `json:"error"`
StdErr string `json:"stderr"`
StdOut string `json:"stdout"`
ExecDuration int64 `json:"exec_duration"`
MemUsage int64 `json:"mem_usage"`
}
)
func (job *Job) run(ctx context.Context, WarmContainers <-chan runningContainer) {
log.WithField("ID", job.ID).Info("Starting job")
// TODO - setjobReceived(ctx)
// Get a ready-to-use container from the pool.
container := <-WarmContainers
defer container.shutDown(ctx)
contextLogger := log.WithFields(
log.Fields{
"ID": job.ID, "containerID": container.containerID,
},
)
contextLogger.Info("Handling job")
// TODO - setjobRunning(ctx)
var httpRes *http.Response
var agentRes agentExecRes
httpRes, err := http.Post("http://"+container.addr+"/", "application/json", bytes.NewBuffer([]byte(job.Payload)))
if err != nil {
log.WithError(err).Error("Failed to request execution to agent")
return
}
if err = json.NewDecoder(httpRes.Body).Decode(&agentRes); err != nil {
log.WithError(err).Error("Response decode failed")
return
}
contextLogger.Info("Job execution finished")
if httpRes.StatusCode != 200 {
log.Error("Failed to run job")
return
}
// TODO - setjobResult(ctx, agentRes)
}
// Validate job options.
func (job *Job) Validate() error {
if job.ID == "" {
return errors.New("ID must not be empty")
}
return nil
}