Repository: go-co-op/gocron
Pull request #25 (2020-04-10 20:24:25)

creates StartAsync and StartBlocking methods


Patch:
diff --git a/README.md b/README.md
index c18c057..1f5ea62 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ goCron is a Golang job scheduling package which lets you run Go functions period
 
 goCron is a Golang implementation of Ruby module [clockwork](https://github.com/tomykaira/clockwork) and Python job scheduling package [schedule](https://github.com/dbader/schedule).
 
-See also this two great articles:
+See also these two great articles:
 
 - [Rethinking Cron](http://adam.herokuapp.com/past/2010/4/13/rethinking_cron/)
 - [Replace Cron with Clockwork](http://adam.herokuapp.com/past/2010/6/30/replace_cron_with_clockwork/)
@@ -34,9 +34,13 @@ func taskWithParams(a int, b string) {
 }
 
 func main() {
+    // defines a new scheduler that schedules and runs jobs
     s1 := gocron.NewScheduler(time.UTC)
+
     s1.Every(3).Seconds().Do(task)
-    <- s1.Start() // starts running (blocks current thread)
+
+    // scheduler starts running jobs and current thread continues to execute
+    s1.StartAsync()
 
     // Do jobs without params
     s2 := gocron.NewScheduler(time.UTC)
@@ -82,8 +86,10 @@ func main() {
     // Clear all scheduled jobs
     s2.Clear()
 
-    // Start all the pending jobs
-    <- s2.Start()
+    // executes the scheduler and blocks current thread
+    s2.StartBlocking()
+
+    // this line is never reached
 }
 ```
 
diff --git a/example/lock.go b/example/lock.go
index d263e0f..e145f44 100644
--- a/example/lock.go
+++ b/example/lock.go
@@ -70,5 +70,5 @@ func main() {
 
 	s := gocron.NewScheduler(time.UTC)
 	s.Every(1).Second().Lock().Do(lockedTask, arg)
-	<-s.Start()
+	s.StartBlocking()
 }
diff --git a/go.mod b/go.mod
index 5baf982..0605225 100644
--- a/go.mod
+++ b/go.mod
@@ -9,4 +9,4 @@ require (
 	github.com/stretchr/testify v1.4.0
 	golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
 	golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
-)
+)
\ No newline at end of file
diff --git a/scheduler.go b/scheduler.go
index aec7db1..876340c 100644
--- a/scheduler.go
+++ b/scheduler.go
@@ -24,8 +24,13 @@ func NewScheduler(loc *time.Location) *Scheduler {
 	}
 }
 
-// Start all the pending Jobs using a per second ticker
-func (s *Scheduler) Start() chan struct{} {
+// StartBlocking starts all the pending jobs using a second-long ticker and blocks the current thread
+func (s *Scheduler) StartBlocking() {
+	<-s.StartAsync()
+}
+
+// StartAsync starts a goroutine that runs all the pending using a second-long ticker
+func (s *Scheduler) StartAsync() chan struct{} {
 	stopped := make(chan struct{})
 	ticker := s.time.NewTicker(1 * time.Second)
 


Test patch:
diff --git a/example_test.go b/example_test.go
index 272c2e9..0afc134 100644
--- a/example_test.go
+++ b/example_test.go
@@ -7,21 +7,25 @@ import (
 	"github.com/go-co-op/gocron"
 )
 
-func ExampleScheduler_Start() {
+var task = func() {
+	fmt.Println("I am a task")
+}
+
+func ExampleScheduler_StartBlocking() {
 	s := gocron.NewScheduler(time.UTC)
-	s.Every(3).Seconds().Do(func() { fmt.Println("I am a task") })
-	<-s.Start()
+	s.Every(3).Seconds().Do(task)
+	s.StartBlocking()
 }
 
 func ExampleScheduler_At() {
 	s := gocron.NewScheduler(time.UTC)
-	s.Every(1).Day().At("10:30").Do(func() { fmt.Println("I am a task") })
-	s.Every(1).Monday().At("10:30:01").Do(func() { fmt.Println("I am a task") })
+	s.Every(1).Day().At("10:30").Do(task)
+	s.Every(1).Monday().At("10:30:01").Do(task)
 }
 
 func ExampleJob_GetScheduledTime() {
 	s := gocron.NewScheduler(time.UTC)
-	job, _ := s.Every(1).Day().At("10:30").Do(func() { fmt.Println("I am a task") })
+	job, _ := s.Every(1).Day().At("10:30").Do(task)
 	fmt.Println(job.GetScheduledTime())
 	// Output: 10:30
 }
diff --git a/gocron_test.go b/gocron_test.go
index b8775b9..72fc29b 100644
--- a/gocron_test.go
+++ b/gocron_test.go
@@ -168,9 +168,9 @@ func TestLocker(t *testing.T) {
 		s3 := NewScheduler(time.UTC)
 		s3.Every(1).Seconds().Lock().Do(task, "C", i)
 
-		stop1 := s1.Start()
-		stop2 := s2.Start()
-		stop3 := s3.Start()
+		stop1 := s1.StartAsync()
+		stop2 := s2.StartAsync()
+		stop3 := s3.StartAsync()
 
 		time.Sleep(time.Millisecond * 100)
 
diff --git a/scheduler_test.go b/scheduler_test.go
index ca57e28..b7b797a 100644
--- a/scheduler_test.go
+++ b/scheduler_test.go
@@ -39,7 +39,7 @@ func TestExecutionSeconds(t *testing.T) {
 		}
 	})
 
-	stop := sched.Start()
+	stop := sched.StartAsync()
 	<-jobDone // Wait job done
 	close(stop)
 
@@ -90,9 +90,9 @@ func TestAt(t *testing.T) {
 	nextRun := dayJob.NextScheduledTime()
 	assert.Equal(t, expectedStartTime, nextRun)
 
-	sStop := s.Start()
+	stop := s.StartAsync()
 	<-dayJobDone // Wait job done
-	close(sStop)
+	close(stop)
 	time.Sleep(time.Second) // wait for scheduler to reschedule job
 
 	// Expected next start time 1 day after