Skip to content

Commit

Permalink
Remove stopChan from Scheduler (#105)
Browse files Browse the repository at this point in the history
Remove stopChan from Scheduler to restrict stopping the scheduler only to the Stop function.
  • Loading branch information
arjunmahishi authored Jan 23, 2021
1 parent e0835f6 commit 3d5128b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
7 changes: 4 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func ExampleScheduler_Stop() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
s.StartAsync()
time.Sleep(time.Second * 5)
s.Stop()
}

Expand Down Expand Up @@ -120,7 +121,7 @@ func ExampleJob_LastRun() {
time.Sleep(time.Second)
}
}()
<-s.StartAsync()
s.StartBlocking()
}

func ExampleJob_NextRun() {
Expand All @@ -132,7 +133,7 @@ func ExampleJob_NextRun() {
time.Sleep(time.Second)
}
}()
<-s.StartAsync()
s.StartAsync()
}

func ExampleJob_RunCount() {
Expand All @@ -144,7 +145,7 @@ func ExampleJob_RunCount() {
time.Sleep(time.Second)
}
}()
<-s.StartAsync()
s.StartAsync()
}

func ExampleJob_RemoveAfterLastRun() {
Expand Down
21 changes: 6 additions & 15 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ type Scheduler struct {
location *time.Location

runningMutex sync.RWMutex
running bool // represents if the scheduler is running at the moment or not
stopChan chan struct{} // signal to stop scheduling
running bool // represents if the scheduler is running at the moment or not

time timeWrapper // wrapper around time.Time
}
Expand All @@ -31,28 +30,21 @@ func NewScheduler(loc *time.Location) *Scheduler {
jobs: make([]*Job, 0),
location: loc,
running: false,
stopChan: make(chan struct{}, 1),
time: &trueTime{},
}
}

// StartBlocking starts all jobs and blocks the current thread
func (s *Scheduler) StartBlocking() {
<-s.StartAsync()
s.StartAsync()
<-make(chan bool)
}

// StartAsync starts all jobs without blocking the current thread
func (s *Scheduler) StartAsync() chan struct{} {
if s.IsRunning() {
return s.stopChan
func (s *Scheduler) StartAsync() {
if !s.IsRunning() {
s.start()
}
s.start()
go func() {
<-s.stopChan
s.setRunning(false)
return
}()
return s.stopChan
}

//start starts the scheduler, scheduling and running jobs
Expand Down Expand Up @@ -408,7 +400,6 @@ func (s *Scheduler) Stop() {

func (s *Scheduler) stop() {
s.setRunning(false)
s.stopChan <- struct{}{}
}

// Do specifies the jobFunc that should be called every time the Job runs
Expand Down
10 changes: 5 additions & 5 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func TestExecutionSeconds(t *testing.T) {
}
})

stop := sched.StartAsync()
sched.StartAsync()
<-jobDone // Wait job done
close(stop)
sched.Stop()

mu.RLock()
defer mu.RUnlock()
Expand Down Expand Up @@ -387,11 +387,11 @@ func TestScheduler_Stop(t *testing.T) {
s.Stop()
assert.False(t, s.IsRunning())
})
t.Run("stops a running scheduler through StartAsync chan", func(t *testing.T) {
t.Run("stops a running scheduler calling .Stop()", func(t *testing.T) {
s := NewScheduler(time.UTC)
c := s.StartAsync()
s.StartAsync()
assert.True(t, s.IsRunning())
close(c)
s.Stop()
time.Sleep(1 * time.Millisecond) // wait for stop goroutine to catch up
assert.False(t, s.IsRunning())
})
Expand Down

0 comments on commit 3d5128b

Please sign in to comment.