-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
51 lines (39 loc) · 1.2 KB
/
job.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
package ozero
import "time"
type job struct {
Data interface{}
}
func (pool *Pool) addJob(data interface{}) {
pool.mutex.RLock()
defer pool.mutex.RUnlock()
// If closed, do nothing.
if !pool.closed {
pool.jobsCh <- job{data}
}
}
// SendJob sends a new job to the pool to be processed by the worker.
// It returns inmediately no matter how busy the pool is.
func (pool *Pool) SendJob(data interface{}) {
go pool.addJob(data)
}
// SendJobSync sends a new job to the pool to be processed by the worker.
// It waits until a worker gets the job and then returns.
func (pool *Pool) SendJobSync(data interface{}) {
pool.addJob(data)
}
// SetTries sets the default amount of times a failing job gets re-executed before giving up and calling error function.
// The default amount of times is 1. Set to zero to retry indefinitely.
func (pool *Pool) SetTries(count int) *Pool {
pool.mutex.Lock()
defer pool.mutex.Unlock()
pool.totalTryCount = count
return pool
}
// SetRetryDelay sets the default timeout after a failing function gets retried.
// Default is retry inmediately.
func (pool *Pool) SetRetryDelay(d time.Duration) *Pool {
pool.mutex.Lock()
defer pool.mutex.Unlock()
pool.retryTimeout = d
return pool
}