-
Notifications
You must be signed in to change notification settings - Fork 7
/
slave.go
63 lines (54 loc) · 942 Bytes
/
slave.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
package slaves
import (
"time"
)
type work struct {
work func(interface{}) interface{}
afterWork func(interface{})
}
type slave struct {
ready int32
exit chan struct{}
work *work
jobs Jobs
Owner *SlavePool
Type string
}
// Open Starts the slave creating goroutine
// that waits job notification
func (s *slave) Open() error {
if s.work == nil {
return errworkIsNil
}
s.ready = 1
s.exit = make(chan struct{})
go func() {
for {
select {
case <-s.exit:
return
default:
for {
data := s.jobs.get()
if data == nil {
break
}
ret := s.work.work(data)
if s.work.afterWork != nil {
s.work.afterWork(ret)
}
s.Owner.wg.Add(-1)
}
}
time.Sleep(time.Millisecond * 10)
}
}()
return nil
}
// Close the slave waiting to finish his tasks
func (s *slave) Close() {
s.exit <- struct{}{}
}
func (s *slave) GetJobs() int {
return len(s.jobs.jobs)
}