Skip to content

Commit

Permalink
lastrun and next run times, with fake clock and scheduler now
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Sep 23, 2023
1 parent d338803 commit d572d73
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ type Scheduler interface {

type SchedulerOption func(*scheduler) error

func WithFakeClock(clock clockwork.Clock) SchedulerOption {
return func(s *scheduler) error {
if clock == nil {
return fmt.Errorf("gocron: WithFakeClock: clock must not be nil")
}
s.clock = clock
return nil
}
}

func WithLocation(location *time.Location) SchedulerOption {
return func(s *scheduler) error {
if location == nil {
Expand Down Expand Up @@ -111,16 +121,23 @@ func NewScheduler(options ...SchedulerOption) (Scheduler, error) {
for {
select {
case id := <-s.exec.jobIDsOut:
lastRun := s.clock.Now()
j := s.jobs[id]
j.lastRun = lastRun

j.lastRun = j.nextRun
next := j.next(j.lastRun)
j.nextRun = next
j.timer = s.clock.AfterFunc(next.Sub(s.now()), func() {
s.exec.jobsIDsIn <- id
})
s.jobs[id] = j

case j := <-s.newJobs:
if _, ok := s.jobs[j.id]; !ok {
if s.started {
next := j.next(time.Now())
next := j.next(s.now())
j.nextRun = next
id := j.id
j.timer = s.clock.AfterFunc(time.Until(next), func() {
j.timer = s.clock.AfterFunc(next.Sub(s.now()), func() {
s.exec.jobsIDsIn <- id
})
}
Expand All @@ -143,16 +160,18 @@ func NewScheduler(options ...SchedulerOption) (Scheduler, error) {
case <-s.start:
s.started = true
for id, j := range s.jobs {
next := j.next(time.Now())
next := j.next(s.now())
j.nextRun = next

jobId := id
j.timer = s.clock.AfterFunc(time.Until(next), func() {
j.timer = s.clock.AfterFunc(next.Sub(s.now()), func() {
s.exec.jobsIDsIn <- jobId
})
s.jobs[id] = j
}
case <-s.ctx.Done():
for _, j := range s.jobs {
j.timer.Stop()
j.cancel()
}
return
Expand All @@ -163,6 +182,10 @@ func NewScheduler(options ...SchedulerOption) (Scheduler, error) {
return s, nil
}

func (s *scheduler) now() time.Time {
return s.clock.Now().In(s.location)
}

func (s *scheduler) NewJob(definition JobDefinition) (uuid.UUID, error) {
j := job{
id: uuid.New(),
Expand Down

0 comments on commit d572d73

Please sign in to comment.