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

refact: rewriting scheduler logic #54

Merged
merged 15 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
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
41 changes: 10 additions & 31 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
type Job struct {
interval uint64 // pause interval * unit between runs
unit timeUnit // time units, ,e.g. 'minutes', 'hours'...
periodDuration time.Duration // interval * unit
startsImmediately bool // if the Job should run upon scheduler start
jobFunc string // the Job jobFunc to run, func[jobFunc]
atTime time.Duration // optional time at which this Job runs
err error // error related to Job
lastRun time.Time // datetime of last run
nextRun time.Time // datetime of next run
startDay time.Weekday // Specific day of the week to start on
scheduledWeekday *time.Weekday // Specific day of the week to start on
dayOfTheMonth int // Specific day of the month to run the job
funcs map[string]interface{} // Map for the function task store
fparams map[string][]interface{} // Map for function and params of function
Expand All @@ -26,12 +25,10 @@ type Job struct {

// NewJob creates a new Job with the provided interval
func NewJob(interval uint64) *Job {
th := newTimeWrapper()
return &Job{
interval: interval,
lastRun: th.Unix(0, 0),
nextRun: th.Unix(0, 0),
startDay: time.Sunday,
lastRun: time.Time{},
nextRun: time.Time{},
funcs: make(map[string]interface{}),
fparams: make(map[string][]interface{}),
tags: []string{},
Expand All @@ -43,6 +40,10 @@ func (j *Job) run() {
callJobFuncWithParams(j.funcs[j.jobFunc], j.fparams[j.jobFunc])
}

func (j Job) neverRan() bool {
return j.lastRun.IsZero()
}

// Err returns an error if one ocurred while creating the Job
func (j *Job) Err() error {
return j.err
Expand Down Expand Up @@ -74,28 +75,6 @@ func (j *Job) Tags() []string {
return j.tags
}

func (j *Job) setPeriodDuration() error {
interval := time.Duration(j.interval)

switch j.unit {
case seconds:
j.periodDuration = interval * time.Second
case minutes:
j.periodDuration = interval * time.Minute
case hours:
j.periodDuration = interval * time.Hour
case days:
j.periodDuration = interval * time.Hour * 24
case weeks:
j.periodDuration = interval * time.Hour * 24 * 7
case months:
// periodDuration doesn't apply here
default:
return ErrPeriodNotSpecified
}
return nil
}

// ScheduledTime returns the time of the Job's next scheduled run
func (j *Job) ScheduledTime() time.Time {
return j.nextRun
Expand All @@ -109,8 +88,8 @@ func (j *Job) ScheduledAtTime() string {
// Weekday returns which day of the week the Job will run on and
// will return an error if the Job is not scheduled weekly
func (j *Job) Weekday() (time.Weekday, error) {
if j.unit == weeks {
return j.startDay, nil
if j.scheduledWeekday == nil {
return time.Sunday, ErrNotScheduledWeekday
}
return time.Sunday, ErrNotScheduledWeekday
return *j.scheduledWeekday, nil
}
34 changes: 4 additions & 30 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestGetScheduledTime(t *testing.T) {

func TestGetWeekday(t *testing.T) {
s := NewScheduler(time.UTC)
weedayJob, _ := s.Every(1).Weekday(time.Wednesday).Do(task)
wednesday := time.Wednesday
weedayJob, _ := s.Every(1).Weekday(wednesday).Do(task)
nonWeekdayJob, _ := s.Every(1).Minute().Do(task)

testCases := []struct {
Expand All @@ -36,7 +37,7 @@ func TestGetWeekday(t *testing.T) {
expectedWeekday time.Weekday
expectedError error
}{
{"success", weedayJob, time.Wednesday, nil},
{"success", weedayJob, wednesday, nil},
{"fail - not set for weekday", nonWeekdayJob, time.Sunday, ErrNotScheduledWeekday},
}

Expand All @@ -46,36 +47,9 @@ func TestGetWeekday(t *testing.T) {
if tc.expectedError != nil {
assert.Error(t, tc.expectedError, err)
} else {
assert.Equal(t, tc.expectedWeekday, weekday)
assert.Nil(t, err)
}

assert.Equal(t, tc.expectedWeekday, weekday)
})
}
}

func TestSetPeriodDuration(t *testing.T) {

testCases := []struct {
desc string
job *Job
expectedDuration time.Duration
expectedError error
}{
{"seconds", &Job{interval: 1, unit: seconds}, time.Duration(1) * time.Second, nil},
{"minutes", &Job{interval: 1, unit: minutes}, time.Duration(1) * time.Minute, nil},
{"hours", &Job{interval: 1, unit: hours}, time.Duration(1) * time.Hour, nil},
{"days", &Job{interval: 1, unit: days}, time.Duration(1) * time.Hour * 24, nil},
{"weeks", &Job{interval: 1, unit: weeks}, time.Duration(1) * time.Hour * 24 * 7, nil},
{"none", &Job{interval: 1}, 0, ErrPeriodNotSpecified},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
err := tc.job.setPeriodDuration()
assert.Equal(t, tc.expectedError, err)
assert.Equal(t, tc.expectedDuration, tc.job.periodDuration)
})
}

}
Loading