Skip to content

Commit

Permalink
Gocron: Allow chaining again
Browse files Browse the repository at this point in the history
  • Loading branch information
marcsantiago committed Apr 7, 2018
1 parent fa935b6 commit 396b627
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
13 changes: 10 additions & 3 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Job struct {
startDay time.Weekday // Specific day of the week to start on
funcs map[string]interface{} // Map for the function task store
fparams map[string]([]interface{}) // Map for function and params of function
err error
}

// NewJob creates a new job with the time interval.
Expand Down Expand Up @@ -124,6 +125,11 @@ func getFunctionName(fn interface{}) string {
return runtime.FuncForPC(reflect.ValueOf((fn)).Pointer()).Name()
}

// Err should be checked to ensure an error didn't occur creating the job
func (j *Job) Err() error {
return j.err
}

// Do specifies the jobFunc that should be called every time the job runs
func (j *Job) Do(jobFun interface{}, params ...interface{}) error {
typ := reflect.TypeOf(jobFun)
Expand Down Expand Up @@ -169,14 +175,15 @@ func formatTime(t string) (int, int, error) {
// At schedules job at specific time of day
// 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, error) {
func (j *Job) At(t string) *Job {
hour, min, err := formatTime(t)
if err != nil {
return j, err
j.err = err
return j
}
// save atTime start as duration from midnight
j.atTime = time.Duration(hour)*time.Hour + time.Duration(min)*time.Minute
return j, nil
return j
}

func (j *Job) periodDuration() (time.Duration, error) {
Expand Down
39 changes: 21 additions & 18 deletions gocron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func TestTaskAt(t *testing.T) {

// Schedule every day At
startAt := fmt.Sprintf("%02d:%02d", now.Hour(), now.Minute()+1)
dayJob, err := s.Every(1).Day().At(startAt)
if err != nil {
dayJob := s.Every(1).Day().At(startAt)
if err := dayJob.Err(); err != nil {
t.Error(err)
}

Expand Down Expand Up @@ -171,10 +171,11 @@ func TestDaily(t *testing.T) {
hour := now.Hour() - 2
minute := now.Minute()
startAt := fmt.Sprintf("%02d:%02d", hour, minute)
dayJob, err := s.Every(1).Day().At(startAt)
if err != nil {
dayJob = s.Every(1).Day().At(startAt)
if err := dayJob.Err(); err != nil {
t.Error(err)
}

dayJob.scheduleNextRun()
exp = time.Date(now.Year(), now.Month(), now.Day()+1, hour, minute, 0, 0, loc)
assertEqualTime(t, dayJob.nextRun, exp)
Expand Down Expand Up @@ -267,38 +268,40 @@ func TestWeekdayAt(t *testing.T) {

// Schedule job at next week day
var weekJob *Job
var err error
switch now.Weekday() {
case time.Monday:
weekJob, err = s.Every(1).Tuesday().At(startAt)
if err != nil {
weekJob = s.Every(1).Tuesday().At(startAt)
if err := weekJob.Err(); err != nil {
t.Error(err)
}
case time.Tuesday:
weekJob, err = s.Every(1).Wednesday().At(startAt)
weekJob = s.Every(1).Wednesday().At(startAt)
if err := weekJob.Err(); err != nil {
t.Error(err)
}
case time.Wednesday:
weekJob, err = s.Every(1).Thursday().At(startAt)
if err != nil {
weekJob = s.Every(1).Thursday().At(startAt)
if err := weekJob.Err(); err != nil {
t.Error(err)
}
case time.Thursday:
weekJob, err = s.Every(1).Friday().At(startAt)
if err != nil {
weekJob = s.Every(1).Friday().At(startAt)
if err := weekJob.Err(); err != nil {
t.Error(err)
}
case time.Friday:
weekJob, err = s.Every(1).Saturday().At(startAt)
if err != nil {
weekJob = s.Every(1).Saturday().At(startAt)
if err := weekJob.Err(); err != nil {
t.Error(err)
}
case time.Saturday:
weekJob, err = s.Every(1).Sunday().At(startAt)
if err != nil {
weekJob = s.Every(1).Sunday().At(startAt)
if err := weekJob.Err(); err != nil {
t.Error(err)
}
case time.Sunday:
weekJob, err = s.Every(1).Monday().At(startAt)
if err != nil {
weekJob = s.Every(1).Monday().At(startAt)
if err := weekJob.Err(); err != nil {
t.Error(err)
}
}
Expand Down

0 comments on commit 396b627

Please sign in to comment.