reugn/go-quartz

Job with cron for particular time executes many times

markwiat opened this issue · 5 comments

I have a cron expression 4 47 11 5 10 ? 2023 and I expect the job will be executed once at 2023-10-05T11:47:04Z.
The job executes at given time but many times. Number of executions is various in different tries but usually is about one million times.
Version of code is v0.7.0
Go version is 1.20
Could you please fix (or explain) the issue?

The code to reproduce is

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/reugn/go-quartz/quartz"
)

type scheduleJob struct {
	scheduleID string
	counter    int
}

func (sj *scheduleJob) Description() string {
	return fmt.Sprintf("Schedule: %s", sj.scheduleID)
}

func (sj *scheduleJob) Key() int {
	return quartz.HashCode(sj.scheduleID)
}

func (sj *scheduleJob) Execute(ctx context.Context) {
	sj.counter++
	fmt.Printf("Execute %d time at %v\n", sj.counter, time.Now().UTC())
}

func main() {
	// at 2023-10-05T11:47:04Z
	cron := "4 47 11 5 10 ? 2023"
	ctx := context.Background()
	sched := quartz.NewStdScheduler()
	sched.Start(ctx)
	trigger, err := quartz.NewCronTrigger(cron)
	if err != nil {
		fmt.Printf("Bad expression: %s", err)
	}

	job := &scheduleJob{scheduleID: "aa"}
	err = sched.ScheduleJob(ctx, job, trigger)
	if err != nil {
		fmt.Printf("Failed to schedule: %s", err)
	}

	time.Sleep(5 * time.Minute)
}
reugn commented

@markwiat, this was probably due to the cron expression expiration not taken care of. CronTrigger has been fixed and now returns an error in such scenario, see #70. Use the master branch to verify the fix.

Thanks. It works on master.

Are you planning release with the fix?

@reugn , should I close the issue?

reugn commented

@markwiat, I'll update on the next release here and close the issue.

reugn commented

v0.8.0 is available.