-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
76 lines (64 loc) · 1.52 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
package goschedtask
import (
"errors"
"reflect"
"time"
)
type Job struct {
JobFunc interface{}
JobParams []interface{}
RunLoop bool
Interval time.Duration
TimeRun time.Time
MustDeleted bool
}
var (
Jobs = []Job{}
)
func RegisterJob(jobFunc interface{}, interval time.Duration, jobParams ...interface{}) {
if err := JobParamsChecking(jobFunc, jobParams); err != nil {
panic(err)
}
job := Job{
JobFunc: jobFunc,
JobParams: jobParams,
RunLoop: true,
Interval: interval,
TimeRun: time.Time{},
MustDeleted: false,
}
Jobs = append(Jobs, job)
}
func RegisterJobRunOnce(jobFunc interface{}, interval time.Duration, jobParams ...interface{}) {
if err := JobParamsChecking(jobFunc, jobParams); err != nil {
panic(err)
}
job := Job{
JobFunc: jobFunc,
JobParams: jobParams,
RunLoop: false,
Interval: interval,
TimeRun: time.Time{},
MustDeleted: false,
}
Jobs = append(Jobs, job)
}
func RegisterJobRunAt(jobFunc interface{}, timeRun time.Time, jobParams ...interface{}) {
if err := JobParamsChecking(jobFunc, jobParams); err != nil {
panic(err)
}
job := Job{
JobFunc: jobFunc,
JobParams: jobParams,
RunLoop: false,
TimeRun: timeRun,
MustDeleted: false,
}
Jobs = append(Jobs, job)
}
func JobParamsChecking(jobFunc interface{}, jobParams []interface{}) error {
if len(jobParams) != reflect.ValueOf(jobFunc).Type().NumIn() {
return errors.New("the number of job function params is not the same as the function")
}
return nil
}