-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjob.go
78 lines (66 loc) · 1.85 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
78
package main
import (
"bytes"
"context"
"encoding/json"
"net/http"
log "github.com/sirupsen/logrus"
)
func (job benchJob) run(ctx context.Context, WarmVMs <-chan runningFirecracker) {
log.WithField("job", job).Info("Handling job")
err := q.setjobReceived(ctx, job)
if err != nil {
log.WithError(err).Error("Could not set job received")
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}
// Get a ready-to-use microVM from the pool
vm := <-WarmVMs
// Defer cleanup of VM and VMM
go func() {
defer vm.vmmCancel()
vm.machine.Wait(vm.vmmCtx)
}()
defer vm.shutDown()
var reqJSON []byte
reqJSON, err = json.Marshal(agentRunReq{
ID: job.ID,
Language: job.Language,
Code: job.Code,
Variant: "TODO",
})
if err != nil {
log.WithError(err).Error("Failed to marshal JSON request")
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}
err = q.setjobRunning(ctx, job)
if err != nil {
log.WithError(err).Error("Could not set job running")
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}
var httpRes *http.Response
var agentRes agentExecRes
httpRes, err = http.Post("http://"+vm.ip.String()+":8080/run", "application/json", bytes.NewBuffer(reqJSON))
if err != nil {
log.WithError(err).Error("Failed to request execution to agent")
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}
json.NewDecoder(httpRes.Body).Decode(&agentRes)
log.WithField("result", agentRes).Info("Job execution finished")
if httpRes.StatusCode != 200 {
log.WithFields(log.Fields{
"httpRes": httpRes,
"agentRes": agentRes,
"reqJSON": string(reqJSON),
}).Error("Failed to compile and run code")
q.setjobFailed(ctx, job, agentRes)
return
}
err = q.setjobResult(ctx, job, agentRes)
if err != nil {
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
}
}