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

Remove stopChan from Scheduler #105

Merged
merged 2 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
8 changes: 4 additions & 4 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 @@ -389,9 +389,9 @@ func TestScheduler_Stop(t *testing.T) {
})
t.Run("stops a running scheduler through StartAsync chan", func(t *testing.T) {
arjunmahishi marked this conversation as resolved.
Show resolved Hide resolved
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