Skip to content

changed fix size array to slice and made iteration a little more idiomatic #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 22 additions & 40 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ func ChangeLoc(newLocation *time.Location) {
loc = newLocation
}

// Max number of jobs, hack it if you need.
const MAXJOBNUM = 10000

type Job struct {

// pause interval * unit bettween runs
Expand Down Expand Up @@ -67,9 +64,9 @@ type Job struct {
}

// Create a new job with the time interval.
func NewJob(intervel uint64) *Job {
func NewJob(interval uint64) *Job {
return &Job{
intervel,
interval,
"", "", "",
time.Unix(0, 0),
time.Unix(0, 0), 0,
Expand All @@ -84,7 +81,7 @@ func (j *Job) shouldRun() bool {
return time.Now().After(j.nextRun)
}

//Run the job and immdiately reschedulei it
//Run the job and immediately reschedule it
func (j *Job) run() (result []reflect.Value, err error) {
f := reflect.ValueOf(j.funcs[j.jobFunc])
params := j.fparams[j.jobFunc]
Expand Down Expand Up @@ -347,16 +344,13 @@ func (j *Job) Weeks() *Job {
// Class Scheduler, the only data member is the list of jobs.
type Scheduler struct {
// Array store jobs
jobs [MAXJOBNUM]*Job

// Size of jobs which jobs holding.
size int
jobs []*Job
}

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

func (s *Scheduler) Len() int {
return s.size
return len(s.jobs)
}

func (s *Scheduler) Swap(i, j int) {
Expand All @@ -369,20 +363,16 @@ func (s *Scheduler) Less(i, j int) bool {

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

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

runnableJobs[n] = s.jobs[i]
//fmt.Println(runnableJobs)
n++
runnableJobs = append(runnableJobs, s.jobs[i])
} else {
break
}
Expand All @@ -392,7 +382,7 @@ func (s *Scheduler) getRunnableJobs() (running_jobs [MAXJOBNUM]*Job, n int) {

// Datetime when the next job should run.
func (s *Scheduler) NextRun() (*Job, time.Time) {
if s.size <= 0 {
if len(s.jobs) <= 0 {
return nil, time.Now()
}
sort.Sort(s)
Expand All @@ -402,8 +392,7 @@ func (s *Scheduler) NextRun() (*Job, time.Time) {
// Schedule a new periodic job
func (s *Scheduler) Every(interval uint64) *Job {
job := NewJob(interval)
s.jobs[s.size] = job
s.size++
s.jobs = append(s.jobs, job)
return job
}

Expand All @@ -420,41 +409,34 @@ 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.jobs[i].run()
for _, job := range s.jobs {
job.run()
}
}

// Run all jobs with delay seconds
func (s *Scheduler) RunAllwithDelay(d int) {
for i := 0; i < s.size; i++ {
s.jobs[i].run()
for _, job := range s.jobs {
job.run()
time.Sleep(time.Duration(d))
}
}

// Remove specific job j
func (s *Scheduler) Remove(j interface{}) {
i := 0
for ; i < s.size; i++ {
if s.jobs[i].jobFunc == getFunctionName(j) {
var i int
var job *Job
for i, job = range s.jobs {
if job.jobFunc == getFunctionName(j) {
break
}
}

for j := (i + 1); j < s.size; j++ {
s.jobs[i] = s.jobs[j]
i++
}
s.size = s.size - 1
s.jobs = append(s.jobs[:i], s.jobs[i+1:]...)
}

// Delete all scheduled jobs
func (s *Scheduler) Clear() {
for i := 0; i < s.size; i++ {
s.jobs[i] = nil
}
s.size = 0
s.jobs = []*Job{}
}

// Start all the pending jobs
Expand Down