-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
59 lines (49 loc) · 1012 Bytes
/
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
package main
import (
"os"
"sync"
"syscall"
"time"
log "github.com/sirupsen/logrus"
)
// TODO
// Cleanup channels
// Register restart handlers below correctly
var (
stop = make(chan struct{})
done = make(chan struct{})
)
func worker(wg *sync.WaitGroup, q *Queue) {
defer wg.Done()
log.Printf("Worker started")
LOOP:
for {
time.Sleep(175 * time.Millisecond) // this is work to be done by worker.
select {
case <-stop:
break LOOP
default:
if q.queue.Len() > 0 {
// Take only the last item pushed
e := q.queue.Back()
j := e.Value.(*Job)
q.queue.Init()
log.Printf("Processing job: %+v %+v", j.uuid, j.cmd)
r, t := j.exec()
log.Printf("Processed job: %+v, %+v %+v (in %+v)", j.uuid, j.cmd, r, t)
}
}
}
done <- struct{}{}
}
func termHandler(sig os.Signal) {
log.Println("terminating...")
stop <- struct{}{}
if sig == syscall.SIGQUIT {
<-done
}
}
func reloadHandler(sig os.Signal) error {
log.Println("configuration reloaded")
return nil
}