-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaborers.go
122 lines (106 loc) · 3.25 KB
/
laborers.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
package komi
import (
"errors"
"fmt"
"sync"
)
// Submit sends a job to the pool for processing.
func (p Pool[I, _]) Submit(job I) error {
if p.IsClosed() {
return errors.New("can't submit a job to the closed pool")
}
p.inputs <- job
p.jobsWaiting.Add(1)
return nil
}
// Outputs returns a channel of outputs generated by the pool, nil
// if pool is closed or doesn't produce outputs.
func (p Pool[_, O]) Outputs() (chan O, error) {
if p.IsClosed() {
return nil, errors.New("no outputs from a closed pool")
}
if !p.producesOutputs() {
return nil, errors.New("the pool doesn't produce outputs")
}
if p.IsConnected() {
return nil, fmt.Errorf("the pool is connected to a parent %s", p.parent.Name())
}
return p.outputs, nil
}
// Errors returns a channel of errors generated by the pool, nil
// if pool is closed, doesn't produce errors, or an error handler is set.
func (p Pool[I, _]) Errors() (chan PoolError[I], error) {
if p.IsClosed() {
return nil, errors.New("no errors from a closed pool")
}
if !p.producesErrors() {
return nil, errors.New("the pool doesn't produce errors")
}
return p.errors, nil
}
// startLaborers will start laborers.
func (p *Pool[I, O]) startLaborers() {
// Create the group to wait on.
p.laborersActive = &sync.WaitGroup{}
// Create the channel
p.laborersStopSignal = make(chan Signal)
// Create the number given by the settings.
for i := 0; i < p.settings.Laborers; i++ {
go func(p *Pool[I, O]) {
for {
select {
case job := <-p.inputs:
// Run the work performer on each new job.
p.workPerformer(job)
continue
case <-p.laborersStopSignal:
// When stop signal received, mark the laborer as inactive
// and kill the current scope.
p.laborersActive.Done()
return
}
}
}(p)
// Record the laborer as an active laborer.
p.laborersActive.Add(1)
}
p.log.Debug("Started laborers", "count", p.settings.Laborers)
}
// stopLaborers will send blocking closure signals to all laborers and wait (blocking)
// until they all gracefully leave.
func (p *Pool[_, _]) stopLaborers() {
p.log.Debug("Sending signals to kill laborers...", "count", p.settings.Laborers)
for i := 0; i < p.settings.Laborers; i++ {
p.laborersStopSignal <- signal
}
// Wait for all the laborers to quit.
p.laborersActive.Wait()
// After laborers quit, close the channel.
close(p.laborersStopSignal)
// Log the laborers closure.
p.log.Debug("All laborers quit", "count", p.settings.Laborers)
}
// Wait wil block until the pool has no waiting jobs, see `With...` options.
func (p Pool[_, _]) Wait() {
p.currentlyWaitingForJobs.Store(true)
defer p.currentlyWaitingForJobs.Store(false)
// If no jobs waiting, leave immediately
if p.JobsWaiting() == 0 {
return
}
// Wait for the `performedWork` to send a signal
<-p.noJobsCurrentlyWaitingSignal
}
// JobsCompleted will return the number of jobs completed by the pool.
func (p Pool[_, _]) JobsCompleted() int64 {
return p.jobsCompleted.Load()
}
// JobsWaiting will return the number of jobs currently waiting and executing
// by the pool.
func (p Pool[_, _]) JobsWaiting() int64 {
return p.jobsWaiting.Load()
}
// JobsSucceeded will return the number of jobs succeeded (non-nil errors).
func (p Pool[_, _]) JobsSucceeded() int64 {
return p.jobsSucceeded.Load()
}