/
worker.go
156 lines (127 loc) · 3.4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2021 Snowfork
// SPDX-License-Identifier: LGPL-3.0-only
package workers
import (
"context"
"errors"
"os"
"os/signal"
"syscall"
"time"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
type Worker interface {
Name() string
Start(ctx context.Context, eg *errgroup.Group) error
}
type WorkerFactory func() (Worker, *WorkerConfig, error)
type WorkerPool []WorkerFactory
var WorkerDeadlocked = errors.New("Worker deadlocked")
type DeadlockHandler func() error
func (wp WorkerPool) runWorker(ctx context.Context, worker Worker) error {
childEg, childCtx := errgroup.WithContext(ctx)
err := worker.Start(childCtx, childEg)
if err != nil {
return err
}
// We wait for this worker to finish in an indepedent goroutine. This
// allows us to detect when a worker is deadlocked, i.e. all its
// goroutines are not terminating when childCtx.Done() is signaled.
// If a deadlock occurs, we have to kill the process to clean up
// the worker.
notifyWaitDone := make(chan error)
go func() {
notifyWaitDone <- childEg.Wait()
close(notifyWaitDone)
}()
select {
case err := <-notifyWaitDone:
return err
case <-childCtx.Done():
// Goroutines are either shutting down or deadlocked.
// Give them a few seconds...
select {
case <-time.After(10 * time.Second):
break
case err := <-notifyWaitDone:
// All goroutines have ended
return err
}
return WorkerDeadlocked
}
}
func (wp WorkerPool) Run() error {
return wp.run(context.Background(), nil, wp.defaultLogger())
}
func (wp WorkerPool) RunWithContext(ctx context.Context, onDeadlock DeadlockHandler, log *logrus.Entry) error {
return wp.run(ctx, onDeadlock, log)
}
func (wp WorkerPool) run(ctx context.Context, onDeadlock DeadlockHandler, log *logrus.Entry) error {
ctx, cancel := context.WithCancel(ctx)
eg, ctx := errgroup.WithContext(ctx)
if onDeadlock == nil {
onDeadlock = func() error {
cancel()
// Give other workers time to clean up
<-time.After(3 * time.Second)
relayProc, _ := os.FindProcess(os.Getpid())
relayProc.Kill()
return nil
}
}
// Ensure clean termination upon SIGINT, SIGTERM
eg.Go(func() error {
notify := make(chan os.Signal, 1)
signal.Notify(notify, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ctx.Done():
return ctx.Err()
case sig := <-notify:
log.WithField("signal", sig.String()).Info("Received signal")
cancel()
}
return nil
})
for _, f := range wp {
factory := f
eg.Go(func() error {
restarts := 0
for {
worker, config, err := factory()
if err != nil {
// It is unrecoverable if we cannot construct one of our workers
return err
}
if !config.Enabled {
return nil
}
log.WithFields(logrus.Fields{
"restarts": restarts,
"worker": worker.Name(),
}).Debug("Starting worker")
err = wp.runWorker(ctx, worker)
if err == WorkerDeadlocked {
log.WithField(
"worker",
worker.Name(),
).Error("The worker's goroutines are deadlocked. Please fix")
return onDeadlock()
} else {
log.WithError(err).WithField("worker", worker.Name()).Debug("Worker terminated")
}
select {
case <-ctx.Done():
return ctx.Err()
default:
<-time.After(time.Duration(config.RestartDelay) * time.Second)
restarts += 1
}
}
})
}
return eg.Wait()
}
func (wp WorkerPool) defaultLogger() *logrus.Entry {
return logrus.WithField("source", "WorkerPool")
}