Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-740: ascending time function #744

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Jobs can be run every x weeks on specific days of the week and at specific times
- [**Monthly**](https://pkg.go.dev/github.com/go-co-op/gocron/v2#MonthlyJob):
Jobs can be run every x months on specific days of the month and at specific times.
- [**One time**](https://pkg.go.dev/github.com/go-co-op/gocron/v2#OneTimeJob):
Jobs can be run once at a specific time. These are non-recurring jobs.
Jobs can be run at a specific time(s) (either once or many times).

### Concurrency Limits
Jobs can be limited individually or across the entire scheduler.
Expand Down
21 changes: 3 additions & 18 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"math/rand"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -448,11 +447,9 @@ type oneTimeJobDefinition struct {

func (o oneTimeJobDefinition) setup(j *internalJob, _ *time.Location, now time.Time) error {
sortedTimes := o.startAt(j)
sort.Slice(sortedTimes, func(i, j int) bool {
return sortedTimes[i].Before(sortedTimes[j])
})
slices.SortStableFunc(sortedTimes, AscendingTime)
// keep only schedules that are in the future
idx, found := slices.BinarySearchFunc(sortedTimes, now, timeCmp())
idx, found := slices.BinarySearchFunc(sortedTimes, now, AscendingTime)
if found {
idx++
}
Expand Down Expand Up @@ -499,18 +496,6 @@ func OneTimeJob(startAt OneTimeJobStartAtOption) JobDefinition {
}
}

func timeCmp() func(element time.Time, target time.Time) int {
return func(element time.Time, target time.Time) int {
if element.Equal(target) {
return 0
}
if element.Before(target) {
return -1
}
return 1
}
}

// -----------------------------------------------
// -----------------------------------------------
// ----------------- Job Options -----------------
Expand Down Expand Up @@ -917,7 +902,7 @@ type oneTimeJob struct {
// lastRun: 8 => [idx=3,found=found] => next is none
// lastRun: 9 => [idx=3,found=found] => next is none
func (o oneTimeJob) next(lastRun time.Time) time.Time {
idx, found := slices.BinarySearchFunc(o.sortedTimes, lastRun, timeCmp())
idx, found := slices.BinarySearchFunc(o.sortedTimes, lastRun, AscendingTime)
// if found, the next run is the following index
if found {
idx++
Expand Down
4 changes: 1 addition & 3 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,7 @@ func (s *scheduler) selectExecJobsOutForRescheduling(id uuid.UUID) {
// always grab the last element in the slice as that is the furthest
// out in the future and the time from which we want to calculate
// the subsequent next run time.
slices.SortStableFunc(j.nextScheduled, func(a, b time.Time) int {
return a.Compare(b)
})
slices.SortStableFunc(j.nextScheduled, AscendingTime)
scheduleFrom = j.nextScheduled[len(j.nextScheduled)-1]
}

Expand Down
4 changes: 2 additions & 2 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,8 +2190,8 @@ func TestScheduler_AtTimesJob(t *testing.T) {
},
},
{
name: "two runs in the future",
atTimes: []time.Time{n.Add(1 * time.Millisecond), n.Add(3 * time.Millisecond)},
name: "two runs in the future - order is maintained even if times are provided out of order",
atTimes: []time.Time{n.Add(3 * time.Millisecond), n.Add(1 * time.Millisecond)},
fakeClock: clockwork.NewFakeClockAt(n),
advanceAndAsserts: []func(t *testing.T, j Job, clock clockwork.FakeClock, runs *atomic.Uint32){
func(t *testing.T, j Job, clock clockwork.FakeClock, runs *atomic.Uint32) {
Expand Down
8 changes: 5 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@
}
atTimesDate = append(atTimesDate, at.time(location))
}
slices.SortStableFunc(atTimesDate, func(a, b time.Time) int {
return a.Compare(b)
})
slices.SortStableFunc(atTimesDate, AscendingTime)
return atTimesDate, nil
}

func AscendingTime(a, b time.Time) int {

Check failure on line 95 in util.go

View workflow job for this annotation

GitHub Actions / lint and test (1.21)

exported: exported function AscendingTime should have comment or be unexported (revive)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't need to be exported

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I didn't realize that it was all in the same package. Pushed 👍

return a.Compare(b)
}

type waitGroupWithMutex struct {
wg sync.WaitGroup
mu sync.Mutex
Expand Down
Loading