-
Notifications
You must be signed in to change notification settings - Fork 3
/
job.go
185 lines (164 loc) · 4.49 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package scheduler
import (
"fmt"
"github.com/GoFarsi/scheduler/errs"
"github.com/GoFarsi/scheduler/helper"
"github.com/GoFarsi/scheduler/types"
"log"
"reflect"
"time"
)
var (
jobLocker Locker
)
type Locker interface {
Lock(string) (bool, error)
Unlock(string) error
}
// Job information about a job
type Job struct {
JobFunction string
Functions map[string]interface{}
FuncParams map[string][]interface{}
Interval uint64
JobUnit types.TimeUnit
Tags []string
AtTime time.Duration
TimeLocation *time.Location
LastRun time.Time
NextRun time.Time
FirstWeekDay time.Weekday
JobError error
LockJob bool
}
// NewJob creates a new job
func NewJob(interval uint64) *Job {
return &Job{
Interval: interval,
TimeLocation: types.TimeZone,
LastRun: time.Unix(0, 0),
NextRun: time.Unix(0, 0),
FirstWeekDay: time.Sunday,
Functions: make(map[string]interface{}),
FuncParams: make(map[string][]interface{}),
Tags: []string{},
}
}
// Run the job and reschedule it
func (j *Job) Run() ([]reflect.Value, error) {
if j.LockJob {
if jobLocker == nil {
return nil, fmt.Errorf("%v %v", errs.ERROR_TRY_LOCK_JOB, j.JobFunction)
}
hashedKey := helper.GetFunctionHashedKey(j.JobFunction)
if _, err := jobLocker.Lock(hashedKey); err != nil {
return nil, fmt.Errorf("%v %v", errs.ERROR_TRY_LOCK_JOB, j.JobFunction)
}
defer jobLocker.Unlock(hashedKey)
}
result, err := helper.CallJobFuncWithParams(j.Functions[j.JobFunction], j.FuncParams[j.JobFunction])
if err != nil {
return nil, err
}
return result, nil
}
// Do specify the jobFunc that should be executed every time the job runs
func (j *Job) Do(jobFunction interface{}, params ...interface{}) error {
if j.JobError != nil {
return j.JobError
}
jobType := reflect.TypeOf(jobFunction)
if jobType.Kind() != reflect.Func {
return errs.ERROR_NOT_A_FUNCTION
}
funcName := helper.GetFunctionName(jobFunction)
j.Functions[funcName] = jobFunction
j.FuncParams[funcName] = params
j.JobFunction = funcName
now := time.Now().In(j.TimeLocation)
if !j.NextRun.After(now) {
err := j.NextJobRun()
if err != nil {
return err
}
}
return nil
}
// DoJobSafely does the same thing as Do, except it logs unexpected panics rather than unwinding them up the chain
func (j *Job) DoJobSafely(jobFunction interface{}, params ...interface{}) error {
recoveryFunction := func() {
defer func() {
if r := recover(); r != nil {
log.Printf("internal panic happen: %v", r)
}
}()
_, _ = helper.CallJobFuncWithParams(jobFunction, params)
}
return j.Do(recoveryFunction)
}
// At schedules the job to run at the given time
//
// s.Every(1).Day().At("20:30:01").Do(task)
// s.Every(1).Monday().At("20:30:01").Do(task)
func (j *Job) At(t string) *Job {
h, m, s, err := helper.TimeFormat(t)
if err != nil {
j.JobError = errs.ERROR_TIME_FORMAT
return j
}
j.AtTime = time.Duration(h)*time.Hour + time.Duration(m)*time.Minute + time.Duration(s)*time.Second
return j
}
// NextJobRun sets the next run time for the job
func (j *Job) NextJobRun() error {
now := time.Now()
if j.LastRun == time.Unix(0, 0) {
j.LastRun = now
}
periodDuration, err := j.PeriodDuration()
if err != nil {
return err
}
switch j.JobUnit {
case types.Seconds, types.Minutes, types.Hours:
j.NextRun = j.LastRun.Add(periodDuration)
case types.Days:
j.NextRun = j.RoundToMidNight(j.LastRun)
j.NextRun = j.NextRun.Add(j.AtTime)
case types.Weeks:
j.NextRun = j.RoundToMidNight(j.LastRun)
dayDiff := int(j.FirstWeekDay)
dayDiff -= int(j.NextRun.Weekday())
if dayDiff != 0 {
j.NextRun = j.NextRun.Add(time.Duration(dayDiff) * 24 * time.Hour)
}
j.NextRun = j.NextRun.Add(j.AtTime)
}
// next possible schedule advance
for j.NextRun.Before(now) || j.NextRun.Before(j.LastRun) {
j.NextRun = j.NextRun.Add(periodDuration)
}
return nil
}
// PeriodDuration returns the duration of the job
func (j *Job) PeriodDuration() (time.Duration, error) {
interval := time.Duration(j.Interval)
var periodDuration time.Duration
switch j.JobUnit {
case types.Seconds:
periodDuration = interval * time.Second
case types.Minutes:
periodDuration = interval * time.Minute
case types.Hours:
periodDuration = interval * time.Hour
case types.Days:
periodDuration = interval * time.Hour * 24
case types.Weeks:
periodDuration = interval * time.Hour * 24 * 7
case types.Months:
periodDuration = interval * time.Hour * 24 * 30
default:
return 0, errs.ERROR_JOB_PREIOD
}
return periodDuration, nil
}