This repository has been archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.go
133 lines (113 loc) · 2.97 KB
/
worker.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
package qmd
import (
"encoding/json"
"fmt"
"os/exec"
"time"
"github.com/goware/disque"
"github.com/goware/lg"
"github.com/pressly/qmd/rest/api"
)
type Worker chan *disque.Job
func (qmd *Qmd) StartWorkers() {
lg.Debugf("Starting %v QMD workers", qmd.Config.MaxJobs)
for i := 0; i < qmd.Config.MaxJobs; i++ {
go qmd.startWorker(i, qmd.Workers)
}
}
func (qmd *Qmd) startWorker(id int, workers chan Worker) {
qmd.WaitWorkers.Add(1)
defer qmd.WaitWorkers.Done()
worker := make(Worker)
for {
// Mark this worker as available.
workers <- worker
select {
// Wait for a job.
case job := <-worker:
msg := fmt.Errorf("Worker %v:\tGot \"%v\" job %v/jobs/%v", id, job.Queue, qmd.Config.URL, job.ID)
lg.Error(msg)
qmd.Slack.Notify(msg.Error())
var req *api.ScriptsRequest
err := json.Unmarshal([]byte(job.Data), &req)
if err != nil {
qmd.Queue.Ack(job)
msg := fmt.Errorf("Worker %v:\tfailed: %v", err)
lg.Error(msg)
qmd.Slack.Notify(msg.Error())
break
}
script, err := qmd.GetScript(req.Script)
if err != nil {
qmd.Queue.Ack(job)
msg := fmt.Errorf("Worker %v:\tfailed: %v", err)
lg.Error(msg)
qmd.Slack.Notify(msg.Error())
break
}
// Create QMD job to run the command.
cmd, err := qmd.Cmd(exec.Command(script, req.Args...))
if err != nil {
qmd.Queue.Ack(job)
msg := fmt.Errorf("Worker %v:\tfailed: %v", err)
lg.Error(msg)
qmd.Slack.Notify(msg.Error())
break
}
cmd.JobID = job.ID
cmd.CallbackURL = req.CallbackURL
cmd.ExtraWorkDirFiles = req.Files
// Run a job.
go cmd.Run()
<-cmd.Started
select {
// Wait for the job to finish.
case <-cmd.Finished:
// Or kill it, if it doesn't finish in a specified time.
case <-time.After(time.Duration(qmd.Config.MaxExecTime) * time.Second):
cmd.Kill()
cmd.Wait()
cmd.Cleanup()
// Or kill it, if QMD is closing.
case <-qmd.ClosingWorkers:
lg.Debugf("Worker %d:\tStopping (busy)", id)
cmd.Kill()
cmd.Cleanup()
qmd.Queue.Nack(job)
msg := fmt.Errorf("Worker %d:\tNACKed job %v/jobs/%v", id, qmd.Config.URL, job.ID)
lg.Error(msg)
qmd.Slack.Notify(msg.Error())
return
}
// Response.
resp := api.ScriptsResponse{
ID: job.ID,
Script: req.Script,
Args: req.Args,
Files: req.Files,
}
// "OK" and "ERR" for backward compatibility.
if cmd.StatusCode == 0 {
resp.Status = "OK"
} else {
resp.Status = "ERR"
}
resp.EndTime = cmd.EndTime
resp.Duration = fmt.Sprintf("%f", cmd.Duration.Seconds())
resp.QmdOut = cmd.QmdOut.String()
resp.ExecLog = cmd.CmdOut.String()
resp.StartTime = cmd.StartTime
if cmd.Err != nil {
resp.Err = cmd.Err.Error()
}
qmd.DB.SaveResponse(&resp)
qmd.Queue.Ack(job)
msg = fmt.Errorf("Worker %v:\tACKed job %v/jobs/%v", id, qmd.Config.URL, job.ID)
lg.Error(msg)
qmd.Slack.Notify(msg.Error())
case <-qmd.ClosingWorkers:
lg.Debugf("Worker %d:\tStopping (idle)", id)
return
}
}
}