-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
114 lines (97 loc) · 2.38 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package goschedule
import (
"time"
"github.com/streadway/amqp"
)
type Job struct {
identifier string
isPeriodic bool
latestRunAt time.Time
at time.Time
every time.Duration
firstRun bool
f *function
isRabbitEvent bool
rabbitEvent *rabbitMQEvent
}
type JobInfo struct {
Identifier string
LatestRunAt time.Time
NextRunAt time.Time
Every time.Duration
RanBefore bool
FunctionInfo *functionInfo
IsRabbitEvent bool
RabbitMQEventInfo *rabbitMQEventInfo
}
func newFunctionJob(identifier string, f interface{}, params []interface{}) *Job {
return &Job{
identifier: identifier,
isPeriodic: false,
firstRun: true,
f: newFunction(f, params, identifier),
isRabbitEvent: false,
}
}
func newEventJob(identifier string, ch *amqp.Channel, exchange string, routingKey string, mandatory bool, immediate bool, contentType string, body []byte) *Job {
e := newRabbitMQEvent(ch, exchange, routingKey, mandatory, immediate, contentType, body, identifier)
return &Job{
identifier: identifier,
isPeriodic: false,
firstRun: true,
rabbitEvent: e,
isRabbitEvent: true,
}
}
func (j *Job) At(at time.Time) *Job {
j.at = at
return j
}
func (j *Job) Every(every time.Duration) *Job {
j.isPeriodic = true
j.every = every
return j
}
func (j *Job) GetIdentifier() string {
return j.identifier
}
func (j *Job) GetNextRunTime() time.Time {
return j.at
}
func (j *Job) GetIntervalsBetweenRuns() time.Duration {
return j.every
}
func (j *Job) GetNextRunUnixNanoTime() int64 {
return j.at.UnixNano()
}
func (j *Job) IsPeriodic() bool {
return j.isPeriodic
}
func (j *Job) shouldRun() bool {
return time.Now().After(j.at)
}
func (j *Job) updateForNextRun() {
j.at = time.Now().Add(j.every)
}
func (j *Job) JobInfo() *JobInfo {
if j.isRabbitEvent {
return &JobInfo{
Identifier: j.identifier,
LatestRunAt: j.latestRunAt,
NextRunAt: j.at,
Every: j.every,
RanBefore: !j.firstRun,
IsRabbitEvent: j.isRabbitEvent,
RabbitMQEventInfo: j.rabbitEvent.GetRabbitEventInfo(),
}
}
return &JobInfo{
Identifier: j.identifier,
LatestRunAt: j.latestRunAt,
NextRunAt: j.at,
Every: j.every,
RanBefore: !j.firstRun,
FunctionInfo: j.f.GetFuncInfo(),
IsRabbitEvent: j.isRabbitEvent,
}
}