Skip to content
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

add job limit and remove after last run to chain #116

Merged
merged 5 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 34 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"github.com/go-co-op/gocron"
)

var task = func() {
fmt.Println("I am a task")
}
var task = func() {}

// ---------------------------------------------------------------------
// -------------------SCHEDULER-FUNCTIONS-------------------------------
// ---------------------------------------------------------------------

func ExampleScheduler_Location() {
s := gocron.NewScheduler(time.UTC)
Expand Down Expand Up @@ -105,9 +107,37 @@ func ExampleScheduler_Seconds() {
// these are all the same
_, _ = s.Every(1).Do(task)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Seconds().Do(task)
}

func ExampleScheduler_LimitRunsTo() {
s := gocron.NewScheduler(time.UTC)

j, _ := s.Every(1).Second().LimitRunsTo(1).Do(task)
s.StartAsync()

fmt.Println(j.RunCount())
// Output:
// 1
}

func ExampleScheduler_RemoveAfterLastRun() {
s := gocron.NewScheduler(time.UTC)

j, _ := s.Every(1).Second().LimitRunsTo(1).RemoveAfterLastRun().Do(task)
s.StartAsync()
s.Stop()
fmt.Println(j.RunCount())
fmt.Println(s.Len())
// Output:
// 1
// 0
}

// ---------------------------------------------------------------------
// ----------------------JOB-FUNCTIONS----------------------------------
// ---------------------------------------------------------------------

func ExampleJob_ScheduledTime() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30").Do(task)
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module github.com/go-co-op/gocron

go 1.13
go 1.15

require github.com/stretchr/testify v1.4.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
13 changes: 9 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 0 additions & 8 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
package gocron

import (
"crypto/sha256"
"errors"
"fmt"
"reflect"
"regexp"
"runtime"
Expand Down Expand Up @@ -65,12 +63,6 @@ func getFunctionName(fn interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
}

func getFunctionKey(funcName string) string {
h := sha256.New()
h.Write([]byte(funcName))
return fmt.Sprintf("%x", h.Sum(nil))
}

func parseTime(t string) (hour, min, sec int, err error) {
var timeLayout string
switch {
Expand Down
39 changes: 21 additions & 18 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ func (j *Job) Weekday() (time.Weekday, error) {
return *j.scheduledWeekday, nil
}

// LimitRunsTo limits the number of executions of this
// job to n. However, the job will still remain in the
// scheduler
// LimitRunsTo limits the number of executions of this job to n. However,
// the job will still remain in the scheduler
// Note: if a job is added to a running scheduler and this method is used
// you may see the job run more than the limit as it's scheduled immediately
// upon being added to the scheduler. It's recommended to use the LimitRunsTo()
// when scheduling the job scheduler.LimitRunsTo(1).Do()
JohnRoesler marked this conversation as resolved.
Show resolved Hide resolved
func (j *Job) LimitRunsTo(n int) {
j.Lock()
defer j.Unlock()
Expand All @@ -187,6 +190,21 @@ func (j *Job) LimitRunsTo(n int) {
}
}

// RemoveAfterLastRun sets the job to be removed after it's last run (when limited)
func (j *Job) RemoveAfterLastRun() *Job {
j.Lock()
defer j.Unlock()
j.runConfig.removeAfterLastRun = true
return j
}

// TODO: this method seems unnecessary as we could always remove after the run count has expired. Maybe remove this in the future?
JohnRoesler marked this conversation as resolved.
Show resolved Hide resolved
func (j *Job) getRemoveAfterLastRun() bool {
j.RLock()
defer j.RUnlock()
return j.runConfig.removeAfterLastRun
}

// shouldRun evaluates if this job should run again
// based on the runConfig
func (j *Job) shouldRun() bool {
Expand Down Expand Up @@ -234,14 +252,6 @@ func (j *Job) setRunCount(i int) {
j.runCount = i
}

// RemoveAfterLastRun update the job in order to remove the job after its last exec
func (j *Job) RemoveAfterLastRun() *Job {
j.Lock()
defer j.Unlock()
j.runConfig.removeAfterLastRun = true
return j
}

func (j *Job) getFiniteRuns() bool {
j.RLock()
defer j.RUnlock()
Expand All @@ -254,13 +264,6 @@ func (j *Job) getMaxRuns() int {
return j.runConfig.maxRuns
}

// TODO: this method seems unnecessary as we could always remove after the run count has expired. Maybe remove this in the future?
func (j *Job) getRemoveAfterLastRun() bool {
j.RLock()
defer j.RUnlock()
return j.runConfig.removeAfterLastRun
}

func (j *Job) stopTimer() {
if j.timer != nil {
j.timer.Stop()
Expand Down
28 changes: 16 additions & 12 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,22 @@ func removeAtIndex(jobs []*Job, i int) []*Job {
return jobs
}

// Scheduled checks if specific job function was already added
// LimitRunsTo limits the number of executions of this job to n. However,
// the job will still remain in the scheduler
func (s *Scheduler) LimitRunsTo(i int) *Scheduler {
job := s.getCurrentJob()
job.LimitRunsTo(i)
return s
}

// RemoveAfterLastRun sets the job to be removed after it's last run (when limited)
func (s *Scheduler) RemoveAfterLastRun() *Scheduler {
job := s.getCurrentJob()
job.RemoveAfterLastRun()
return s
}

// Scheduled checks if specific Job j was already added
func (s *Scheduler) Scheduled(j interface{}) bool {
for _, job := range s.Jobs() {
if job.jobFunc == getFunctionName(j) {
Expand Down Expand Up @@ -474,17 +489,6 @@ func (s *Scheduler) StartAt(t time.Time) *Scheduler {
return s
}

// shouldRun returns true if the Job should be run now
func (s *Scheduler) shouldRun(j *Job) bool {

// option remove the job's in the scheduler after its last execution
if j.getRemoveAfterLastRun() && (j.getMaxRuns()-j.RunCount()) == 1 {
s.RemoveByReference(j)
}

return j.shouldRun() && s.now().Unix() >= j.NextRun().Unix()
}

// setUnit sets the unit type
func (s *Scheduler) setUnit(unit timeUnit) {
currentJob := s.getCurrentJob()
Expand Down
Loading