Skip to content

Commit 9b0ce3a

Browse files
committed
feat: support job run immediately option
1 parent 40dc54a commit 9b0ce3a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

cron.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ func (c *Cron) Schedule(schedule Schedule, cmd Job, entryOpts ...EntryOption) En
190190
return entry.ID
191191
}
192192

193+
var LongLongAgo = time.Unix(0, 1)
194+
193195
// EntryOption is a hook which allows the Entry to be altered before being
194196
// committed internally.
195197
type EntryOption func(*Entry)
@@ -202,6 +204,11 @@ func WithPrev(prev time.Time) EntryOption {
202204
}
203205
}
204206

207+
// WithRunImmediately this job will be run as soon as the job is added
208+
func WithRunImmediately() EntryOption {
209+
return WithPrev(LongLongAgo)
210+
}
211+
205212
// Entries returns a snapshot of the cron entries.
206213
func (c *Cron) Entries() []Entry {
207214
c.runningMu.Lock()

cron_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,10 @@ func TestJobWithCustomPrev(t *testing.T) {
687687
// expected timeline: 1s ... 4s ... stop (2 calls)
688688
// if prev was ignored, the func would only be called once (at 3s)
689689
cron.AddFunc("@every 3s", func() { atomic.AddInt64(&calls, 1) }, WithPrev(time.Now().Add(-2*time.Second)))
690+
cron.AddFunc("@every 4s", func() { atomic.AddInt64(&calls, 1) }, WithRunImmediately())
690691
cron.Start()
691692
time.Sleep(5 * time.Second)
692-
if atomic.LoadInt64(&calls) != 2 {
693+
if atomic.LoadInt64(&calls) != 4 {
693694
t.Errorf("called %d times, expected 2\n", calls)
694695
}
695696
}

0 commit comments

Comments
 (0)