-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
123 lines (107 loc) · 1.96 KB
/
runner.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
package runner
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"sync/atomic"
)
func NewRunner(ctx context.Context, name string, numWorkers int) *Runner {
if numWorkers < 1 {
numWorkers = 1
}
runner := &Runner{
Name: name,
ctx: ctx,
numWorkers: numWorkers,
}
return runner
}
type Runner struct {
Name string
ctx context.Context
numWorkers int
jobs []*Job
}
func (r *Runner) Add(task Task, desc ...string) {
r.jobs = append(r.jobs, &Job{
Task: task,
Name: fmt.Sprintf("[%s] %s", r.Name, strings.Join(desc, ";")),
})
}
func (r *Runner) Commit() (err error) {
total := int64(len(r.jobs))
if total == 0 {
return nil
}
errLocker := sync.RWMutex{}
setErr := func(e error) {
errLocker.Lock()
defer errLocker.Unlock()
if err == nil {
err = e
}
}
queue := make(chan *Job, total)
defer close(queue)
for i := int64(0); i < total; i++ {
queue <- r.jobs[i]
}
r.jobs = nil
numberWorks := int64(r.numWorkers)
if numberWorks > total {
numberWorks = total
}
goroutines := sync.WaitGroup{}
for i := int64(0); i < numberWorks; i++ {
goroutines.Add(1)
go func() {
defer goroutines.Done()
for job := range queue {
atomic.AddInt64(&total, -1)
e := dispatch(r.ctx, job)
if e != nil {
setErr(e)
break
}
// when task down
if atomic.LoadInt64(&total) == 0 {
break
}
}
}()
}
goroutines.Wait()
return
}
func dispatch(ctx context.Context, job *Job) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
interrupt := make(chan os.Signal)
signal.Notify(interrupt, os.Interrupt)
defer signal.Stop(interrupt)
select {
case <-interrupt:
return &Error{
Name: job.Name,
Type: ErrTypeInterrupt,
}
case <-ctx.Done():
return &Error{
Name: job.Name,
Type: ErrTypeTimeout,
}
default:
return job.Task(ctx)
}
}
type Task func(ctx context.Context) error
type Job struct {
Name string
Task Task
}