Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/earoc/gocron
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasYang committed Feb 11, 2017
2 parents 2c42aaf + e1370fa commit eb8dc9d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package main

import (
"fmt"
"github.com/jasonlvhit/gocron"
"github.com/earoc/gocron"
)

func task() {
Expand Down
37 changes: 28 additions & 9 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"runtime"
"sort"
"sync"
"time"
)

Expand Down Expand Up @@ -63,18 +64,23 @@ type Job struct {

// Map for function and params of function
fparams map[string]([]interface{})

sync.RWMutex
}

// Create a new job with the time interval.
func NewJob(intervel uint64) *Job {
func NewJob(interval uint64) *Job {
return &Job{
intervel,
"", "", "",
time.Unix(0, 0),
time.Unix(0, 0), 0,
time.Sunday,
make(map[string]interface{}),
make(map[string]([]interface{})),
interval: interval,
jobFunc: "",
unit: "",
atTime: "",
lastRun: time.Unix(0, 0),
nextRun: time.Unix(0, 0),
period: 0,
startDay: time.Sunday,
funcs: make(map[string]interface{}),
fparams: make(map[string]([]interface{})),
}
}

Expand Down Expand Up @@ -167,7 +173,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 @@ -348,6 +356,8 @@ type Scheduler struct {

// Size of jobs which jobs holding.
size int

sync.RWMutex
}

// Scheduler implements the sort.Interface{} for sorting jobs, by the time nextRun
Expand All @@ -366,14 +376,19 @@ func (s *Scheduler) Less(i, j int) bool {

// Create a new scheduler
func NewScheduler() *Scheduler {
return &Scheduler{[MAXJOBNUM]*Job{}, 0}
return &Scheduler{
jobs: [MAXJOBNUM]*Job{},
size: 0,
}
}

// Get the current runnable jobs, which shouldRun is True
func (s *Scheduler) getRunnableJobs() (running_jobs [MAXJOBNUM]*Job, n int) {
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 @@ -392,7 +407,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 @@ -417,7 +434,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 eb8dc9d

Please sign in to comment.