-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
67 lines (50 loc) · 847 Bytes
/
worker.go
File metadata and controls
67 lines (50 loc) · 847 Bytes
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
package healthcheck
import (
"os"
"os/signal"
"syscall"
)
type Worker struct {
jobs map[JobId]*Job
}
func NewWorker() *Worker {
return &Worker{jobs: map[JobId]*Job{}}
}
func (w *Worker) AddJob(j *Job) {
w.jobs[j.id] = j
j.Start()
}
func (w *Worker) CancelJob(jid JobId) {
job, ok := w.jobs[jid]
if ok {
job.Cancel()
delete(w.jobs, job.id)
}
}
func (w *Worker) CancelJobs() {
for _, job := range w.jobs {
job.Cancel()
delete(w.jobs, job.id)
}
}
func (w *Worker) Size() int {
return len(w.jobs)
}
func (w *Worker) Work() <-chan struct{} {
quit := make(chan struct{})
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
defer close(quit)
for {
select {
case <-signals:
w.CancelJobs()
return
default:
//
}
}
}()
return quit
}