forked from gochore/dcron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_option.go
58 lines (47 loc) · 1.54 KB
/
job_option.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
package dcron
import (
"context"
"time"
)
// JobOption represents a modification to the default behavior of a Job.
type JobOption func(job *innerJob)
// BeforeFunc represents the function could be called before Run.
type BeforeFunc func(task Task) (skip bool)
// RunFunc represents the function could be called by a cron.
type RunFunc func(ctx context.Context) error
// AfterFunc represents the function could be called after Run.
type AfterFunc func(task Task)
// RetryInterval indicates how long should delay before retrying when run failed `triedTimes` times.
type RetryInterval func(triedTimes int) time.Duration
// WithBeforeFunc specifies what to do before Run.
func WithBeforeFunc(before BeforeFunc) JobOption {
return func(job *innerJob) {
job.before = before
}
}
// WithAfterFunc specifies what to do after Run.
func WithAfterFunc(after AfterFunc) JobOption {
return func(job *innerJob) {
job.after = after
}
}
// WithRetryTimes specifies max times to retry,
// retryTimes will be set as 1 if it is less than 1.
func WithRetryTimes(retryTimes int) JobOption {
return func(job *innerJob) {
job.retryTimes = retryTimes
}
}
// WithRetryInterval indicates how long should delay before retrying when run failed `triedTimes` times.
func WithRetryInterval(retryInterval RetryInterval) JobOption {
return func(job *innerJob) {
job.retryInterval = retryInterval
}
}
// WithNoLock means the job will run at multiple cron instances,
// even though the cron has Lock.
func WithNoLock() JobOption {
return func(job *innerJob) {
job.noLock = true
}
}