Skip to content

Commit

Permalink
Fix data race
Browse files Browse the repository at this point in the history
  • Loading branch information
papa-stiflera committed Feb 6, 2017
1 parent 37088f5 commit cce577e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package main

import (
"fmt"
"github.com/jasonlvhit/gocron"
"github.com/papa-stiflera/gocron"
)

func task() {
Expand Down
16 changes: 8 additions & 8 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ func NewJob(interval uint64) *Job {

// True if the job should be run now
func (j *Job) shouldRun() bool {
j.RLock()
defer j.RUnlock()
return time.Now().After(j.nextRun)
}

Expand Down Expand Up @@ -159,8 +157,6 @@ func (j *Job) At(t string) *Job {

//Compute the instant when this job should run next
func (j *Job) scheduleNextRun() {
j.Lock()
defer j.Unlock()
if j.lastRun == time.Unix(0, 0) {
if j.unit == "weeks" {
i := time.Now().Weekday() - j.startDay
Expand All @@ -176,7 +172,9 @@ func (j *Job) scheduleNextRun() {

if j.period != 0 {
// translate all the units to the Seconds
j.Lock()
j.nextRun = j.lastRun.Add(j.period * time.Second)
j.Unlock()
} else {
switch j.unit {
case "minutes":
Expand Down Expand Up @@ -371,8 +369,6 @@ func (s *Scheduler) Swap(i, j int) {
}

func (s *Scheduler) Less(i, j int) bool {
s.RLock()
defer s.RUnlock()
return s.jobs[j].nextRun.After(s.jobs[i].nextRun)
}

Expand All @@ -386,11 +382,11 @@ func NewScheduler() *Scheduler {

// Get the current runnable jobs, which shouldRun is True
func (s *Scheduler) getRunnableJobs() (running_jobs [MAXJOBNUM]*Job, n int) {
s.Lock()
defer s.Unlock()
runnableJobs := [MAXJOBNUM]*Job{}
n = 0
s.Lock()
sort.Sort(s)
s.Unlock()
for i := 0; i < s.size; i++ {
if s.jobs[i].shouldRun() {

Expand All @@ -409,7 +405,9 @@ func (s *Scheduler) NextRun() (*Job, time.Time) {
if s.size <= 0 {
return nil, time.Now()
}
s.Lock()
sort.Sort(s)
s.Unlock()
return s.jobs[0], s.jobs[0].nextRun
}

Expand All @@ -435,7 +433,9 @@ func (s *Scheduler) RunPending() {
// Run all jobs regardless if they are scheduled to run or not
func (s *Scheduler) RunAll() {
for i := 0; i < s.size; i++ {
s.Lock()
s.jobs[i].run()
s.Unlock()
}
}

Expand Down

0 comments on commit cce577e

Please sign in to comment.