Repository: go-co-op/gocron
Pull request #83 (2020-11-26 15:13:56)

Expose the Job's lastRun, nextRun and runCount 
### What does this do?
Add 3 exported methods to the Job struct to expose lastRun, nextRun and runCount

### Have you included tests for your changes?
Yes

### Did you document any new/modified functionality?

- [x] Updated `example_test.go`
- [ ] Updated `README.md`

### Notes


arjunmahishi: Thanks for the review guys!  Will close this on the weekend. A little busy today and tomorrow. 

Patch:
diff --git a/job.go b/job.go
index ade04cf..c2865b9 100644
--- a/job.go
+++ b/job.go
@@ -22,10 +22,10 @@ type Job struct {
 	scheduledWeekday  *time.Weekday            // Specific day of the week to start on
 	dayOfTheMonth     int                      // Specific day of the month to run the job
 	funcs             map[string]interface{}   // Map for the function task store
-	fparams           map[string][]interface{} // Map for function and  params of function
+	fparams           map[string][]interface{} // Map for function and params of function
 	tags              []string                 // allow the user to tag Jobs with certain labels
 	runConfig         runConfig                // configuration for how many times to run the job
-	runCount          int                      // number of time the job ran
+	runCount          int                      // number of times the job ran
 }
 
 type runConfig struct {
@@ -123,3 +123,27 @@ func (j *Job) LimitRunsTo(n int) {
 func (j *Job) shouldRun() bool {
 	return !j.runConfig.finiteRuns || j.runCount < j.runConfig.maxRuns
 }
+
+// LastRun returns the time the job was run last
+func (j *Job) LastRun() time.Time {
+	j.Lock()
+	defer j.Unlock()
+	lastRun := j.lastRun
+	return lastRun
+}
+
+// NextRun returns the time the job will run next
+func (j *Job) NextRun() time.Time {
+	j.Lock()
+	defer j.Unlock()
+	nextRun := j.nextRun
+	return nextRun
+}
+
+// RunCount returns the number of time the job ran so far
+func (j *Job) RunCount() int {
+	j.Lock()
+	defer j.Unlock()
+	runCount := j.runCount
+	return runCount
+}


Test patch:
diff --git a/example_test.go b/example_test.go
index 681622e..6c62dfc 100644
--- a/example_test.go
+++ b/example_test.go
@@ -96,3 +96,39 @@ func ExampleJob_LimitRunsTo() {
 	job.LimitRunsTo(2)
 	s.StartAsync()
 }
+
+func ExampleJob_LastRun() {
+	s := gocron.NewScheduler(time.UTC)
+	job, _ := s.Every(1).Second().Do(task)
+	go func() {
+		for {
+			fmt.Println("Last run", job.LastRun())
+			time.Sleep(time.Second)
+		}
+	}()
+	<-s.StartAsync()
+}
+
+func ExampleJob_NextRun() {
+	s := gocron.NewScheduler(time.UTC)
+	job, _ := s.Every(1).Second().Do(task)
+	go func() {
+		for {
+			fmt.Println("Next run", job.NextRun())
+			time.Sleep(time.Second)
+		}
+	}()
+	<-s.StartAsync()
+}
+
+func ExampleJob_RunCount() {
+	s := gocron.NewScheduler(time.UTC)
+	job, _ := s.Every(1).Second().Do(task)
+	go func() {
+		for {
+			fmt.Println("Run count", job.RunCount())
+			time.Sleep(time.Second)
+		}
+	}()
+	<-s.StartAsync()
+}
diff --git a/job_test.go b/job_test.go
index 470308c..e788207 100644
--- a/job_test.go
+++ b/job_test.go
@@ -107,3 +107,21 @@ func TestJob_LimitRunsTo(t *testing.T) {
 	j.run()
 	assert.Equal(t, j.shouldRun(), false, "Not expecting it to run again")
 }
+
+func TestJob_CommonExports(t *testing.T) {
+	s := NewScheduler(time.Local)
+	j, _ := s.Every(1).Second().Do(func() {})
+	assert.Equal(t, 0, j.RunCount())
+	assert.True(t, j.LastRun().IsZero())
+	assert.True(t, j.NextRun().IsZero())
+
+	s.StartAsync()
+	assert.False(t, j.NextRun().IsZero())
+
+	j.runCount = 5
+	assert.Equal(t, 5, j.RunCount())
+
+	lastRun := time.Now()
+	j.lastRun = lastRun
+	assert.Equal(t, lastRun, j.LastRun())
+}