Skip to content

Commit

Permalink
RunOne() method
Browse files Browse the repository at this point in the history
  • Loading branch information
papa-stiflera committed Feb 17, 2017
1 parent 3844612 commit 9757c5c
Showing 1 changed file with 69 additions and 58 deletions.
127 changes: 69 additions & 58 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Job struct {
// Map for function and params of function
fparams map[string]([]interface{})

sync.RWMutex
m sync.RWMutex
}

// Create a new job with the time interval.
Expand All @@ -86,8 +86,8 @@ func NewJob(interval uint64) *Job {

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

Expand Down Expand Up @@ -118,13 +118,13 @@ func (j *Job) Do(jobFun interface{}, params ...interface{}) {
}

fname := getFunctionName(jobFun)
j.Lock()
j.m.Lock()
j.funcs[fname] = jobFun
j.fparams[fname] = params
j.jobFunc = fname
//schedule the next run
j.scheduleNextRun()
j.Unlock()
j.m.Unlock()
}

func (j *Job) DoImmediate(jobFun interface{}, params ...interface{}) {
Expand All @@ -134,11 +134,11 @@ func (j *Job) DoImmediate(jobFun interface{}, params ...interface{}) {
}

fname := getFunctionName(jobFun)
j.Lock()
j.m.Lock()
j.funcs[fname] = jobFun
j.fparams[fname] = params
j.jobFunc = fname
j.Unlock()
j.m.Unlock()
//immediate run
j.run()
}
Expand Down Expand Up @@ -175,8 +175,8 @@ func (j *Job) at(t string) *Job {
// s.Every(1).Day().At("10:30").Do(task)
// s.Every(1).Monday().At("10:30").Do(task)
func (j *Job) At(t string) *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.at(t)
}

Expand Down Expand Up @@ -230,8 +230,8 @@ func (j *Job) second() *Job {

// Set the unit with second
func (j *Job) Second() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.second()
}

Expand All @@ -242,8 +242,8 @@ func (j *Job) seconds() *Job {

// Set the unit with seconds
func (j *Job) Seconds() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.seconds()
}

Expand All @@ -257,8 +257,8 @@ func (j *Job) minute() *Job {
}

func (j *Job) Minute() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.minute()
}

Expand All @@ -269,8 +269,8 @@ func (j *Job) minutes() *Job {
}

func (j *Job) Minutes() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.minutes()
}

Expand All @@ -283,8 +283,8 @@ func (j *Job) hour() *Job {
}

func (j *Job) Hour() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.hour()
}

Expand All @@ -295,8 +295,8 @@ func (j *Job) hours() *Job {
}

func (j *Job) Hours() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.hours()
}

Expand All @@ -309,8 +309,8 @@ func (j *Job) day() *Job {
}

func (j *Job) Day() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.day()
}

Expand All @@ -321,8 +321,8 @@ func (j *Job) days() *Job {
}

func (j *Job) Days() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.days()
}

Expand All @@ -337,8 +337,8 @@ func (j *Job) monday() *Job {
}

func (j *Job) Monday() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.monday()
}

Expand All @@ -352,8 +352,8 @@ func (j *Job) tuesday() *Job {
}

func (j *Job) Tuesday() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.tuesday()
}

Expand All @@ -368,8 +368,8 @@ func (j *Job) wednesday() *Job {
}

func (j *Job) Wednesday() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.wednesday()
}

Expand All @@ -383,8 +383,8 @@ func (j *Job) thursday() *Job {
}

func (j *Job) Thursday() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.thursday()
}

Expand All @@ -398,8 +398,8 @@ func (j *Job) friday() *Job {
}

func (j *Job) Friday() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.friday()
}

Expand All @@ -413,8 +413,8 @@ func (j *Job) saturday() *Job {
}

func (j *Job) Saturday() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.saturday()
}

Expand All @@ -428,8 +428,8 @@ func (j *Job) sunday() *Job {
}

func (j *Job) Sunday() *Job {
j.Lock()
defer j.Unlock()
j.m.Lock()
defer j.m.Unlock()
return j.sunday()
}

Expand All @@ -440,8 +440,8 @@ func (j *Job) weeks() *Job {
}

func (j *Job) Weeks() *Job {
j.Lock()
j.Unlock()
j.m.Lock()
j.m.Unlock()
return j.weeks()
}

Expand All @@ -453,14 +453,14 @@ type Scheduler struct {
// Size of jobs which jobs holding.
size int

sync.RWMutex
m sync.RWMutex
}

// Scheduler implements the sort.Interface{} for sorting jobs, by the time nextRun

func (s *Scheduler) Len() int {
s.RLock()
defer s.RUnlock()
s.m.RLock()
defer s.m.RUnlock()
return s.size
}

Expand All @@ -469,18 +469,18 @@ func (s *Scheduler) swap(i, j int) {
}

func (s *Scheduler) Swap(i, j int) {
s.Lock()
s.m.Lock()
s.swap(i, j)
defer s.Unlock()
defer s.m.Unlock()
}

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

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

Expand All @@ -497,8 +497,8 @@ func (s *Scheduler) getRunnableJobs() ([MAXJOBNUM]*Job, int) {
runnableJobs := [MAXJOBNUM]*Job{}
n := 0
sort.Sort(s)
s.RLock()
defer s.RUnlock()
s.m.RLock()
defer s.m.RUnlock()
for i := 0; i < s.size; i++ {
if s.jobs[i].shouldRun() {

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

Expand All @@ -532,8 +532,8 @@ func (s *Scheduler) every(interval uint64) *Job {
}

func (s *Scheduler) Every(interval uint64) *Job {
s.Lock()
defer s.Unlock()
s.m.Lock()
defer s.m.Unlock()
return s.every(interval)
}

Expand All @@ -551,9 +551,20 @@ 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.m.Lock()
s.jobs[i].run()
s.Unlock()
s.m.Unlock()
}
}

func (s *Scheduler) RunOne(j interface{}) {
i := 0
s.m.RLock()
defer s.m.RUnlock()
for ; i < s.size; i++ {
if s.jobs[i].jobFunc == getFunctionName(j) {
s.jobs[i].run()
}
}
}

Expand Down Expand Up @@ -582,9 +593,9 @@ func (s *Scheduler) remove(j interface{}) {
}

func (s *Scheduler) Remove(j interface{}) {
s.Lock()
s.m.Lock()
s.remove(j)
s.Unlock()
s.m.Unlock()
}

// Delete all scheduled jobs
Expand Down

0 comments on commit 9757c5c

Please sign in to comment.