-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathperiodic_thread.go
More file actions
178 lines (145 loc) · 3.47 KB
/
Copy pathperiodic_thread.go
File metadata and controls
178 lines (145 loc) · 3.47 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package threads
import (
"context"
"fmt"
"runtime/debug"
"sync"
"time"
"github.com/tokenized/logger"
"github.com/pkg/errors"
)
// PeriodicThread is a thread that calls a function periodically.
type PeriodicThread struct {
name string
function UninterruptableFunction
period time.Duration
interrupt chan interface{}
complete chan error
wait *sync.WaitGroup
err error
isComplete bool
lock sync.Mutex
}
func NewPeriodicThread(name string, function UninterruptableFunction,
period time.Duration) *PeriodicThread {
return &PeriodicThread{
name: name,
function: function,
period: period,
}
}
func NewPeriodicThreadComplete(name string, function UninterruptableFunction, period time.Duration,
wait *sync.WaitGroup) (*PeriodicThread, <-chan error) {
complete := make(chan error, 1)
return &PeriodicThread{
name: name,
function: function,
period: period,
complete: complete,
wait: wait,
}, complete
}
func (t *PeriodicThread) SetWait(wait *sync.WaitGroup) {
t.lock.Lock()
defer t.lock.Unlock()
t.wait = wait
}
func (t *PeriodicThread) GetCompleteChannel() <-chan error {
t.lock.Lock()
defer t.lock.Unlock()
// use buffered with size of one so it doesn't wait on write if there is no reader waiting
complete := make(chan error, 1)
t.complete = complete
return complete
}
func (t *PeriodicThread) Start(ctx context.Context) {
interrupt := make(chan interface{}, 1)
t.lock.Lock()
name := t.name
function := t.function
period := t.period
t.interrupt = interrupt
complete := t.complete
wait := t.wait
t.lock.Unlock()
caller := logger.GetCaller(1) // use caller of thread start rather than thread file
if wait != nil {
wait.Add(1)
}
go func() {
logger.LogDepthWithFields(ctx, logger.LevelDebug, caller, nil, "Starting: %s (thread)",
name)
defer func() {
// Check for a panic in this thread.
if pnc := recover(); pnc != nil {
logger.LogDepthWithFields(ctx, logger.LevelError, caller, []logger.Field{
logger.String("stack", string(debug.Stack())),
}, "%s (thread): Panic: %s", name, pnc)
err := fmt.Errorf("%s (thread): panic: %s", name, pnc)
t.lock.Lock()
t.err = err
t.lock.Unlock()
if complete != nil {
complete <- err
}
}
// Mark thread complete
if complete != nil {
close(complete)
}
// Mark wait as done
if wait != nil {
wait.Done()
}
}()
var err error
stop := false
for !stop {
err = function(ctx)
if err != nil {
stop = true
} else {
select {
case <-interrupt:
stop = true
case <-time.After(period):
}
}
}
t.lock.Lock()
t.err = err
t.isComplete = true
t.lock.Unlock()
if complete != nil {
complete <- err
}
if err == nil {
logger.LogDepthWithFields(ctx, logger.LevelDebug, caller, nil,
"Finished: %s (thread)", name)
} else if errors.Cause(err) == Interrupted {
logger.LogDepthWithFields(ctx, logger.LevelDebug, caller, nil,
"Finished: %s (thread) : %s", name, err)
} else {
logger.LogDepthWithFields(ctx, logger.LevelVerbose, caller, nil,
"Finished: %s (thread): %s", name, err)
}
}()
}
func (t *PeriodicThread) Stop(ctx context.Context) {
t.lock.Lock()
defer t.lock.Unlock()
if t.interrupt != nil {
close(t.interrupt)
t.interrupt = nil
}
}
func (t *PeriodicThread) IsComplete() bool {
t.lock.Lock()
defer t.lock.Unlock()
return t.isComplete
}
func (t *PeriodicThread) Error() error {
t.lock.Lock()
defer t.lock.Unlock()
return t.err
}