-
Notifications
You must be signed in to change notification settings - Fork 45
/
worker.go
162 lines (140 loc) · 3.17 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
157
158
159
160
161
162
/*
Copyright (c) 2021 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package async
import (
"context"
"github.com/blend/go-sdk/ex"
)
// NewWorker creates a new worker.
func NewWorker(action WorkAction) *Worker {
return &Worker{
Latch: NewLatch(),
Context: context.Background(),
Action: action,
Work: make(chan interface{}),
}
}
// Worker is a worker that is pushed work over a channel.
// It is used by other work distribution types (i.e. queue and batch)
// but can also be used independently.
type Worker struct {
*Latch
Context context.Context
Action WorkAction
Finalizer WorkerFinalizer
Errors chan error
Work chan interface{}
}
// Background returns the queue worker background context.
func (w *Worker) Background() context.Context {
if w.Context != nil {
return w.Context
}
return context.Background()
}
// NotifyStarted returns the underlying latch signal.
func (w *Worker) NotifyStarted() <-chan struct{} {
return w.Latch.NotifyStarted()
}
// NotifyStopped returns the underlying latch signal.
func (w *Worker) NotifyStopped() <-chan struct{} {
return w.Latch.NotifyStarted()
}
// Enqueue adds an item to the work queue.
func (w *Worker) Enqueue(obj interface{}) {
w.Work <- obj
}
// Start starts the worker with a given context.
func (w *Worker) Start() error {
if !w.Latch.CanStart() {
return ex.New(ErrCannotStart)
}
w.Latch.Starting()
w.Dispatch()
return nil
}
// Dispatch starts the listen loop for work.
func (w *Worker) Dispatch() {
w.Latch.Started()
defer w.Latch.Stopped()
var workItem interface{}
var stopping <-chan struct{}
for {
stopping = w.Latch.NotifyStopping()
select {
case <-stopping:
return
case <-w.Background().Done():
return
default:
}
// block on work or stopping
select {
case workItem = <-w.Work:
w.Execute(w.Background(), workItem)
case <-stopping:
return
case <-w.Background().Done():
return
}
}
}
// Execute invokes the action and recovers panics.
func (w *Worker) Execute(ctx context.Context, workItem interface{}) {
defer func() {
if r := recover(); r != nil {
w.HandleError(ex.New(r))
}
if w.Finalizer != nil {
w.HandleError(w.Finalizer(ctx, w))
}
}()
if w.Action != nil {
w.HandleError(w.Action(ctx, workItem))
}
}
// Stop stop the worker.
// The work left in the queue will remain.
func (w *Worker) Stop() error {
if !w.Latch.CanStop() {
return ex.New(ErrCannotStop)
}
w.Latch.WaitStopped()
w.Latch.Reset()
return nil
}
// StopContext stops the worker in a given cancellation context.
func (w *Worker) StopContext(ctx context.Context) {
stopped := make(chan struct{})
go func() {
defer func() {
w.Latch.Reset()
close(stopped)
}()
w.Latch.WaitStopped()
if workLeft := len(w.Work); workLeft > 0 {
for x := 0; x < workLeft; x++ {
w.Execute(ctx, <-w.Work)
}
}
}()
select {
case <-stopped:
return
case <-ctx.Done():
return
}
}
// HandleError sends a non-nil err to the error
// collector if one is provided.
func (w *Worker) HandleError(err error) {
if err == nil {
return
}
if w.Errors == nil {
return
}
w.Errors <- err
}