Skip to content

Commit 212db8e

Browse files
authored
issue-740: ascending time function (#744)
* issue-740: ascending time function * issue-740: update documentation * issue-740: un-export ascending function * issue-740: sentence correction
1 parent 64d6e48 commit 212db8e

File tree

5 files changed

+12
-27
lines changed

5 files changed

+12
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Jobs can be run every x weeks on specific days of the week and at specific times
9898
- [**Monthly**](https://pkg.go.dev/github.com/go-co-op/gocron/v2#MonthlyJob):
9999
Jobs can be run every x months on specific days of the month and at specific times.
100100
- [**One time**](https://pkg.go.dev/github.com/go-co-op/gocron/v2#OneTimeJob):
101-
Jobs can be run once at a specific time. These are non-recurring jobs.
101+
Jobs can be run at specific time(s) (either once or many times).
102102

103103
### Concurrency Limits
104104
Jobs can be limited individually or across the entire scheduler.

job.go

+3-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"math/rand"
9-
"sort"
109
"strings"
1110
"time"
1211

@@ -448,11 +447,9 @@ type oneTimeJobDefinition struct {
448447

449448
func (o oneTimeJobDefinition) setup(j *internalJob, _ *time.Location, now time.Time) error {
450449
sortedTimes := o.startAt(j)
451-
sort.Slice(sortedTimes, func(i, j int) bool {
452-
return sortedTimes[i].Before(sortedTimes[j])
453-
})
450+
slices.SortStableFunc(sortedTimes, ascendingTime)
454451
// keep only schedules that are in the future
455-
idx, found := slices.BinarySearchFunc(sortedTimes, now, timeCmp())
452+
idx, found := slices.BinarySearchFunc(sortedTimes, now, ascendingTime)
456453
if found {
457454
idx++
458455
}
@@ -499,18 +496,6 @@ func OneTimeJob(startAt OneTimeJobStartAtOption) JobDefinition {
499496
}
500497
}
501498

502-
func timeCmp() func(element time.Time, target time.Time) int {
503-
return func(element time.Time, target time.Time) int {
504-
if element.Equal(target) {
505-
return 0
506-
}
507-
if element.Before(target) {
508-
return -1
509-
}
510-
return 1
511-
}
512-
}
513-
514499
// -----------------------------------------------
515500
// -----------------------------------------------
516501
// ----------------- Job Options -----------------
@@ -917,7 +902,7 @@ type oneTimeJob struct {
917902
// lastRun: 8 => [idx=3,found=found] => next is none
918903
// lastRun: 9 => [idx=3,found=found] => next is none
919904
func (o oneTimeJob) next(lastRun time.Time) time.Time {
920-
idx, found := slices.BinarySearchFunc(o.sortedTimes, lastRun, timeCmp())
905+
idx, found := slices.BinarySearchFunc(o.sortedTimes, lastRun, ascendingTime)
921906
// if found, the next run is the following index
922907
if found {
923908
idx++

scheduler.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,7 @@ func (s *scheduler) selectExecJobsOutForRescheduling(id uuid.UUID) {
311311
// always grab the last element in the slice as that is the furthest
312312
// out in the future and the time from which we want to calculate
313313
// the subsequent next run time.
314-
slices.SortStableFunc(j.nextScheduled, func(a, b time.Time) int {
315-
return a.Compare(b)
316-
})
314+
slices.SortStableFunc(j.nextScheduled, ascendingTime)
317315
scheduleFrom = j.nextScheduled[len(j.nextScheduled)-1]
318316
}
319317

scheduler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2190,8 +2190,8 @@ func TestScheduler_AtTimesJob(t *testing.T) {
21902190
},
21912191
},
21922192
{
2193-
name: "two runs in the future",
2194-
atTimes: []time.Time{n.Add(1 * time.Millisecond), n.Add(3 * time.Millisecond)},
2193+
name: "two runs in the future - order is maintained even if times are provided out of order",
2194+
atTimes: []time.Time{n.Add(3 * time.Millisecond), n.Add(1 * time.Millisecond)},
21952195
fakeClock: clockwork.NewFakeClockAt(n),
21962196
advanceAndAsserts: []func(t *testing.T, j Job, clock clockwork.FakeClock, runs *atomic.Uint32){
21972197
func(t *testing.T, j Job, clock clockwork.FakeClock, runs *atomic.Uint32) {

util.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ func convertAtTimesToDateTime(atTimes AtTimes, location *time.Location) ([]time.
8888
}
8989
atTimesDate = append(atTimesDate, at.time(location))
9090
}
91-
slices.SortStableFunc(atTimesDate, func(a, b time.Time) int {
92-
return a.Compare(b)
93-
})
91+
slices.SortStableFunc(atTimesDate, ascendingTime)
9492
return atTimesDate, nil
9593
}
9694

95+
func ascendingTime(a, b time.Time) int {
96+
return a.Compare(b)
97+
}
98+
9799
type waitGroupWithMutex struct {
98100
wg sync.WaitGroup
99101
mu sync.Mutex

0 commit comments

Comments
 (0)