-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjob.go
More file actions
76 lines (62 loc) · 1.55 KB
/
Copy pathjob.go
File metadata and controls
76 lines (62 loc) · 1.55 KB
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
package krongo
import "time"
// Job is an interface that should be satisfied by all items
// pushed to the scheduler
type Job interface {
// Given the time, return whether or not this job should run
ShouldRun(now time.Time) bool
// Run the job, and return a bool signifying whether or not this
// job should be removed from the scheduler, and an error if the
// job that was run failed.
Run(now time.Time) error
// DeleteOnRun returns whether or not the job is considered finished
// after calling run
DeleteOnRun() bool
}
// Every creates a new job that runs f every t
func Every(t time.Duration, f func() error) Job {
j := repeatedJob{
duration: t,
lastRun: time.Unix(0, 0),
f: f,
}
return &j
}
// At creates a new job that runs f once at time t.
// If the time t has already passed, the job will choose to run at the
// next tick
func At(t time.Time, f func() error) Job {
j := oneShotJob{
when: t,
f: f,
}
return &j
}
type repeatedJob struct {
duration time.Duration
lastRun time.Time
f func() error
}
func (j *repeatedJob) ShouldRun(now time.Time) bool {
return now.Sub(j.lastRun) >= j.duration
}
func (j *repeatedJob) Run(now time.Time) error {
j.lastRun = now
return j.f()
}
func (j *repeatedJob) DeleteOnRun() bool {
return false
}
type oneShotJob struct {
when time.Time
f func() error
}
func (j *oneShotJob) ShouldRun(now time.Time) bool {
return now.After(j.when) || now.Equal(j.when)
}
func (j *oneShotJob) Run(now time.Time) error {
return j.f()
}
func (j *oneShotJob) DeleteOnRun() bool {
return true
}