-
Notifications
You must be signed in to change notification settings - Fork 7
/
notifier.go
36 lines (30 loc) · 794 Bytes
/
notifier.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
package wfl
type Notifier struct {
jch chan *Job
}
// NewNotifier creates a job notifier which allows to synchronize
// multiple job workflows executed concurrently in go functions.
// Note that there is an internal buffer of 1024 jobs which causes
// SendJob() to block if the buffer is full.
func NewNotifier() *Notifier {
return &Notifier{
jch: make(chan *Job, 1024),
}
}
// SendJob sends a job to the notifier.
func (n *Notifier) SendJob(job *Job) {
n.jch <- job
}
// ReceiveJob returns a job sent to the notifier.
func (n *Notifier) ReceiveJob() *Job {
return <-n.jch
}
// Destroy closes the job channel inside the notfier.
func (n *Notifier) Destroy() {
close(n.jch)
}
// Notify send the job to a notifier.
func (j *Job) Notify(n *Notifier) *Job {
n.SendJob(j)
return j
}