Skip to content

Commit

Permalink
fix: atomic.Bool for scheduler.running
Browse files Browse the repository at this point in the history
  • Loading branch information
rfyiamcool committed May 6, 2023
1 parent 9076f20 commit 93aaac6
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/robfig/cron/v3"
Expand All @@ -22,8 +23,7 @@ type Scheduler struct {

locationMutex sync.RWMutex
location *time.Location
runningMutex sync.RWMutex
running bool // represents if the scheduler is running at the moment or not
running *atomic.Bool // represents if the scheduler is running at the moment or not

time TimeWrapper // wrapper around time.Time
timer func(d time.Duration, f func()) *time.Timer
Expand Down Expand Up @@ -58,7 +58,7 @@ func NewScheduler(loc *time.Location) *Scheduler {
return &Scheduler{
jobs: make([]*Job, 0),
location: loc,
running: false,
running: &atomic.Bool{},
time: &trueTime{},
executor: &executor,
tagsUnique: false,
Expand Down Expand Up @@ -111,16 +111,12 @@ func (s *Scheduler) runJobs(jobs []*Job) {
}

func (s *Scheduler) setRunning(b bool) {
s.runningMutex.Lock()
defer s.runningMutex.Unlock()
s.running = b
s.running.Store(b)
}

// IsRunning returns true if the scheduler is running
func (s *Scheduler) IsRunning() bool {
s.runningMutex.RLock()
defer s.runningMutex.RUnlock()
return s.running
return s.running.Load()
}

// Jobs returns the list of Jobs from the Scheduler
Expand Down

0 comments on commit 93aaac6

Please sign in to comment.