Skip to content

Commit

Permalink
add IsRunning() to report the job's state (#233)
Browse files Browse the repository at this point in the history
* add IsRunning() to report the job's state

* go simple

* fix test race
  • Loading branch information
JohnRoesler authored Sep 14, 2021
1 parent 5f492c0 commit 231ac66
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
22 changes: 22 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ func ExampleJob_Error() {
// the given time format is not supported
}

func ExampleJob_IsRunning() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(10).Seconds().Do(func() { time.Sleep(2 * time.Second) })

fmt.Println(j.IsRunning())

s.StartAsync()

time.Sleep(time.Second)
fmt.Println(j.IsRunning())

time.Sleep(time.Second)
s.Stop()

time.Sleep(1 * time.Second)
fmt.Println(j.IsRunning())
// Output:
// false
// true
// false
}

func ExampleJob_LastRun() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
Expand Down
5 changes: 5 additions & 0 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gocron
import (
"context"
"sync"
"sync/atomic"

"golang.org/x/sync/semaphore"
)
Expand Down Expand Up @@ -77,7 +78,9 @@ func (e *executor) start() {

switch f.runConfig.mode {
case defaultMode:
atomic.AddInt64(f.runState, 1)
callJobFuncWithParams(f.function, f.parameters)
atomic.AddInt64(f.runState, -1)
case singletonMode:
_, _, _ = f.limiter.Do("main", func() (interface{}, error) {
select {
Expand All @@ -87,7 +90,9 @@ func (e *executor) start() {
return nil, nil
default:
}
atomic.AddInt64(f.runState, 1)
callJobFuncWithParams(f.function, f.parameters)
atomic.AddInt64(f.runState, -1)
return nil, nil
})
}
Expand Down
13 changes: 11 additions & 2 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/robfig/cron/v3"
Expand Down Expand Up @@ -39,6 +40,7 @@ type jobFunction struct {
limiter *singleflight.Group // limits inflight runs of job to one
ctx context.Context // for cancellation
cancel context.CancelFunc // for cancellation
runState *int64 // will be non-zero when jobs are running
}

type runConfig struct {
Expand All @@ -61,14 +63,16 @@ const (
// newJob creates a new Job with the provided interval
func newJob(interval int, startImmediately bool) *Job {
ctx, cancel := context.WithCancel(context.Background())
var zero int64
return &Job{
interval: interval,
unit: seconds,
lastRun: time.Time{},
nextRun: time.Time{},
jobFunction: jobFunction{
ctx: ctx,
cancel: cancel,
ctx: ctx,
cancel: cancel,
runState: &zero,
},
tags: []string{},
startsImmediately: startImmediately,
Expand Down Expand Up @@ -267,3 +271,8 @@ func (j *Job) stop() {
}
j.cancel()
}

// IsRunning reports whether any instances of the job function are currently running
func (j *Job) IsRunning() bool {
return atomic.LoadInt64(j.runState) != 0
}
21 changes: 21 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ func TestTags(t *testing.T) {
assert.ElementsMatch(t, j.Tags(), []string{"tags", "tag", "some"})
}

func TestJob_IsRunning(t *testing.T) {
s := NewScheduler(time.UTC)
j, err := s.Every(10).Seconds().Do(func() { time.Sleep(2 * time.Second) })
require.NoError(t, err)
assert.False(t, j.IsRunning())

s.StartAsync()

time.Sleep(time.Second)
assert.True(t, j.IsRunning())

time.Sleep(time.Second)
s.Stop()

assert.False(t, j.IsRunning())
}

func TestGetScheduledTime(t *testing.T) {
t.Run("valid", func(t *testing.T) {
j, err := NewScheduler(time.UTC).Every(1).Day().At("10:30").Do(task)
Expand Down Expand Up @@ -127,12 +144,16 @@ func TestJob_CommonExports(t *testing.T) {
assert.True(t, j.NextRun().IsZero())

s.StartAsync()
s.Stop()

assert.False(t, j.NextRun().IsZero())

j.runCount = 5
assert.Equal(t, 5, j.RunCount())

lastRun := time.Now()
j.mu.Lock()
j.lastRun = lastRun
j.mu.Unlock()
assert.Equal(t, lastRun, j.LastRun())
}

0 comments on commit 231ac66

Please sign in to comment.