-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrontab.go
190 lines (171 loc) · 4.01 KB
/
crontab.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
186
187
188
189
190
package crontab
import (
"context"
"github.com/sirupsen/logrus"
"sync/atomic"
"time"
)
type CronType int
const (
CT_Month CronType = iota + 1
CT_Week
CT_Day
CT_Hour
CT_Minute
CT_Second
)
type CronValue struct {
Month int
Week time.Weekday
Day int
Hour int
Minute int
Second int
}
type HandleFunc func(args ...interface{})
type CronTab struct {
ctx context.Context
CronType
cronv CronValue
runOnceFirst bool
running bool
opt *Options
runTimes int64
}
func NewCronTab(cron CronType, opts ...OptionHandleFunc) *CronTab {
var opt = &Options{}
for _, item := range opts {
item(opt)
}
return &CronTab{
ctx: context.TODO(),
CronType: cron,
cronv: CronValue{
Month: 1,
Day: 1,
},
runOnceFirst: true,
opt: opt,
}
}
// RunOnceFirst 先运行一次
func (ct *CronTab) RunOnceFirst(b ...bool) *CronTab {
if len(b) > 0 {
ct.runOnceFirst = b[0]
} else {
ct.runOnceFirst = true
}
return ct
}
func (ct *CronTab) SetMonth(arg int) *CronTab {
if arg < 1 {
panic("arg must > 0")
}
ct.cronv.Month = arg
return ct
}
func (ct *CronTab) SetWeek(arg time.Weekday) *CronTab {
ct.cronv.Week = arg
return ct
}
func (ct *CronTab) SetDay(arg int) *CronTab {
if arg < 0 {
panic("arg must >= 0")
}
ct.cronv.Day = arg
return ct
}
func (ct *CronTab) SetHour(arg int) *CronTab {
if arg < 0 {
panic("arg must >= 0")
}
ct.cronv.Hour = arg
return ct
}
func (ct *CronTab) SetMinute(arg int) *CronTab {
if arg < 0 {
panic("arg must >= 0")
}
ct.cronv.Minute = arg
return ct
}
func (ct *CronTab) SetSecond(arg int) *CronTab {
if arg < 0 {
panic("arg must >= 0")
}
ct.cronv.Second = arg
return ct
}
func (ct *CronTab) IsRunning() bool {
return ct.running
}
func (ct *CronTab) RunTimes() int64 {
return ct.runTimes
}
func (ct *CronTab) Run(h HandleFunc, args ...interface{}) {
if ct.opt.logger == nil {
ct.opt.logger = logrus.New()
}
ct.running = true
if ct.runOnceFirst {
go h(args...)
atomic.AddInt64(&ct.runTimes, 1)
ct.opt.logger.Infof("第%v次执行任务:%v", ct.runTimes, args)
}
for {
now := time.Now()
next := ct._run(now)
t := time.NewTimer(next.Sub(now))
defer t.Stop()
select {
case <-ct.ctx.Done():
ct.running = false
//log.Println("done ...")
return
case <-t.C:
//以下为定时执行的操作
go h(args...)
atomic.AddInt64(&ct.runTimes, 1)
ct.opt.logger.Infof("第%v次执行任务:%v", ct.runTimes, args)
}
}
}
func (ct *CronTab) _run(now time.Time) time.Time {
switch ct.CronType {
case CT_Month:
if ct.cronv.Month == 0 {
panic("Month must > 0")
}
next := now.AddDate(0, ct.cronv.Month, 0)
return time.Date(next.Year(), next.Month(), ct.cronv.Day, ct.cronv.Hour, ct.cronv.Minute, ct.cronv.Second, 0, next.Location())
case CT_Week:
var days = time.Saturday - now.Weekday() + ct.cronv.Week + 1
next := now.AddDate(0, int(days), 0)
return time.Date(next.Year(), next.Month(), next.Day(), ct.cronv.Hour, ct.cronv.Minute, ct.cronv.Second, 0, next.Location())
case CT_Day:
if ct.cronv.Day == 0 {
panic("Day must > 0")
}
next := now.AddDate(0, 0, ct.cronv.Day)
return time.Date(next.Year(), next.Month(), next.Day(), ct.cronv.Hour, ct.cronv.Minute, ct.cronv.Second, 0, next.Location())
case CT_Hour:
if ct.cronv.Hour == 0 {
panic("Hour must > 0")
}
next := now.Add(time.Hour * time.Duration(ct.cronv.Hour))
return time.Date(next.Year(), next.Month(), next.Day(), next.Hour(), ct.cronv.Minute, ct.cronv.Second, 0, next.Location())
case CT_Minute:
if ct.cronv.Minute == 0 {
panic("Minute must > 0")
}
next := now.Add(time.Minute * time.Duration(ct.cronv.Minute))
return time.Date(next.Year(), next.Month(), next.Day(), next.Hour(), next.Minute(), ct.cronv.Second, 0, next.Location())
case CT_Second:
if ct.cronv.Second == 0 {
panic("Second must > 0")
}
next := now.Add(time.Second * time.Duration(ct.cronv.Second))
return time.Date(next.Year(), next.Month(), next.Day(), next.Hour(), next.Minute(), next.Second(), 0, next.Location())
}
return time.Now()
}