1
- // This library implements a cron spec parser and runner. See the README for
2
- // more details.
3
1
package cron
4
2
5
3
import (
@@ -19,6 +17,7 @@ type Cron struct {
19
17
snapshot chan []* Entry
20
18
running bool
21
19
ErrorLog * log.Logger
20
+ location * time.Location
22
21
}
23
22
24
23
// Job is an interface for submitted cron jobs.
@@ -74,15 +73,21 @@ func (s byTime) Less(i, j int) bool {
74
73
return s [i ].Next .Before (s [j ].Next )
75
74
}
76
75
77
- // New returns a new Cron job runner.
76
+ // New returns a new Cron job runner, in the Local time zone .
78
77
func New () * Cron {
78
+ return NewWithLocation (time .Now ().Location ())
79
+ }
80
+
81
+ // NewWithLocation returns a new Cron job runner.
82
+ func NewWithLocation (location * time.Location ) * Cron {
79
83
return & Cron {
80
84
entries : nil ,
81
85
add : make (chan * Entry ),
82
86
stop : make (chan struct {}),
83
87
snapshot : make (chan []* Entry ),
84
88
running : false ,
85
89
ErrorLog : nil ,
90
+ location : location ,
86
91
}
87
92
}
88
93
@@ -131,6 +136,11 @@ func (c *Cron) Entries() []*Entry {
131
136
return c .entrySnapshot ()
132
137
}
133
138
139
+ // Location gets the time zone location
140
+ func (c * Cron ) Location () * time.Location {
141
+ return c .location
142
+ }
143
+
134
144
// Start the cron scheduler in its own go-routine, or no-op if already started.
135
145
func (c * Cron ) Start () {
136
146
if c .running {
@@ -140,6 +150,15 @@ func (c *Cron) Start() {
140
150
go c .run ()
141
151
}
142
152
153
+ // Run the cron scheduler, or no-op if already running.
154
+ func (c * Cron ) Run () {
155
+ if c .running {
156
+ return
157
+ }
158
+ c .running = true
159
+ c .run ()
160
+ }
161
+
143
162
func (c * Cron ) runWithRecovery (j Job ) {
144
163
defer func () {
145
164
if r := recover (); r != nil {
@@ -156,7 +175,7 @@ func (c *Cron) runWithRecovery(j Job) {
156
175
// access to the 'running' state variable.
157
176
func (c * Cron ) run () {
158
177
// Figure out the next activation times for each entry.
159
- now := time .Now ().Local ( )
178
+ now := time .Now ().In ( c . location )
160
179
for _ , entry := range c .entries {
161
180
entry .Next = entry .Schedule .Next (now )
162
181
}
@@ -177,6 +196,7 @@ func (c *Cron) run() {
177
196
timer := time .NewTimer (effective .Sub (now ))
178
197
select {
179
198
case now = <- timer .C :
199
+ now = now .In (c .location )
180
200
// Run every entry whose next time was this effective time.
181
201
for _ , e := range c .entries {
182
202
if e .Next != effective {
@@ -191,7 +211,7 @@ func (c *Cron) run() {
191
211
192
212
case newEntry := <- c .add :
193
213
c .entries = append (c .entries , newEntry )
194
- newEntry .Next = newEntry .Schedule .Next (time .Now ().Local ( ))
214
+ newEntry .Next = newEntry .Schedule .Next (time .Now ().In ( c . location ))
195
215
196
216
case <- c .snapshot :
197
217
c .snapshot <- c .entrySnapshot ()
@@ -202,7 +222,7 @@ func (c *Cron) run() {
202
222
}
203
223
204
224
// 'now' should be updated after newEntry and snapshot cases.
205
- now = time .Now ().Local ( )
225
+ now = time .Now ().In ( c . location )
206
226
timer .Stop ()
207
227
}
208
228
}
0 commit comments