forked from bitmark-inc/bitmarkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.go
61 lines (50 loc) · 1.41 KB
/
background.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
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package background
// the shutdown and completed type for a background
type shutdown struct {
shutdown chan struct{}
finished chan struct{}
}
// handle type for the stop
type T struct {
s []shutdown
}
// type signature for background process
// and type that implements this Run is a process
type Process interface {
Run(args interface{}, shutdown <-chan struct{})
}
// list of processes to start
type Processes []Process
// start up a set of background processes
// all with the same arg value
func Start(processes Processes, args interface{}) *T {
register := new(T)
register.s = make([]shutdown, len(processes))
// start each background
for i, p := range processes {
shutdown := make(chan struct{})
finished := make(chan struct{})
register.s[i].shutdown = shutdown
register.s[i].finished = finished
go func(p Process, shutdown <-chan struct{}, finished chan<- struct{}) {
// pass the shutdown to the Run loop for shutdown signalling
p.Run(args, shutdown)
// flag for the stop routine to wait for shutdown
close(finished)
}(p, shutdown, finished)
}
return register
}
// stop a set of background processes
func (t *T) Stop() {
if nil == t {
return
}
// trigger shutdown of all background tasks
for _, shutdown := range t.s {
close(shutdown.shutdown)
}
}