-
Notifications
You must be signed in to change notification settings - Fork 388
/
worker.go
149 lines (135 loc) · 3.78 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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package pool
import (
"runtime/debug"
"sync"
"sync/atomic"
"time"
"github.com/rs/zerolog"
)
//go:generate go run ../../tools/plc4xgenerator/gen.go -type=worker
type worker struct {
id int
executor interface {
isTraceWorkers() bool
getWorksItems() chan workItem
getWorkerWaitGroup() *sync.WaitGroup
}
lastReceived atomic.Value
stateChange sync.Mutex
running atomic.Bool
shutdown atomic.Bool
interrupted atomic.Bool
interrupter chan struct{}
log zerolog.Logger `ignore:"true"`
}
func newWorker(localLog zerolog.Logger, workerId int, executor interface {
isTraceWorkers() bool
getWorksItems() chan workItem
getWorkerWaitGroup() *sync.WaitGroup
}) *worker {
w := &worker{
id: workerId,
executor: executor,
log: localLog.With().Int("workerId", workerId).Logger(),
}
w.initialize()
return w
}
func (w *worker) initialize() {
w.stateChange.Lock()
defer w.stateChange.Unlock()
w.lastReceived.Store(time.Time{})
w.running.Store(false)
w.shutdown.Store(false)
w.interrupted.Store(false)
w.interrupter = make(chan struct{}, 1)
}
func (w *worker) start() {
w.stateChange.Lock()
defer w.stateChange.Unlock()
if w.running.Load() {
w.log.Warn().Int("Worker id", w.id).Msg("Worker already started")
return
}
if w.executor.isTraceWorkers() {
w.log.Debug().Stringer("worker", w).Msg("Starting worker")
}
w.executor.getWorkerWaitGroup().Add(1)
w.running.Store(true)
go w.work()
}
func (w *worker) stop(interrupt bool) {
w.stateChange.Lock()
defer w.stateChange.Unlock()
if !w.running.Load() {
w.log.Warn().Int("Worker id", w.id).Msg("Worker not running")
return
}
if w.executor.isTraceWorkers() {
w.log.Debug().Stringer("worker", w).Msg("Stopping worker")
}
w.shutdown.Store(true)
if interrupt {
w.interrupted.Store(true)
close(w.interrupter)
}
}
func (w *worker) work() {
defer w.executor.getWorkerWaitGroup().Done()
defer func() {
if err := recover(); err != nil {
w.log.Error().
Str("stack", string(debug.Stack())).
Interface("err", err).
Msg("panic-ed")
if !w.shutdown.Load() {
// if we are not in shutdown we continue
w.start()
}
}
}()
defer w.running.Store(false)
workerLog := w.log
if !w.executor.isTraceWorkers() {
workerLog = zerolog.Nop()
}
for !w.shutdown.Load() {
workerLog.Trace().Msg("Working")
select {
case _workItem := <-w.executor.getWorksItems():
w.lastReceived.Store(time.Now())
workItemLog := workerLog.With().Stringer("workItem", &_workItem).Logger()
workItemLog.Debug().Msg("Got work item")
if _workItem.completionFuture.cancelRequested.Load() || (w.shutdown.Load() && w.interrupted.Load()) {
workerLog.Debug().Msg("We need to stop")
// TODO: do we need to complete with a error?
} else {
workItemLog.Debug().Msg("Running work item")
_workItem.runnable()
_workItem.completionFuture.complete()
workItemLog.Debug().Msg("work item completed")
}
case <-w.interrupter:
workerLog.Debug().Msg("We got interrupted")
}
}
workerLog.Trace().Msg("done")
}