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

feat(scheduler): add a channel to handle misfired jobs #129

Merged
merged 2 commits into from
Apr 15, 2024
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
14 changes: 13 additions & 1 deletion quartz/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ type StdSchedulerOptions struct {
// using a custom implementation of the JobQueue, where operations
// may timeout or fail.
RetryInterval time.Duration

// MisfiredChan allows the creation of event listeners to handle jobs that
// have failed to be executed on time and have been skipped by the scheduler.
//
// Misfires can occur due to insufficient resources or scheduler downtime.
// Adjust OutdatedThreshold to establish an acceptable delay time and
// ensure regular job execution.
MisfiredChan chan ScheduledJob
}

// Verify StdScheduler satisfies the Scheduler interface.
Expand Down Expand Up @@ -516,7 +524,11 @@ func (sched *StdScheduler) validateJob(job ScheduledJob) (bool, func() (int64, e
now := NowNano()
if job.NextRunTime() < now-sched.opts.OutdatedThreshold.Nanoseconds() {
duration := time.Duration(now - job.NextRunTime())
logger.Debugf("Job %s skipped as outdated %s.", job.JobDetail().jobKey, duration)
logger.Infof("Job %s is outdated %s.", job.JobDetail().jobKey, duration)
select {
case sched.opts.MisfiredChan <- job:
default:
}
return false, func() (int64, error) { return job.Trigger().NextFireTime(now) }
} else if job.NextRunTime() > now {
logger.Debugf("Job %s is not due to run yet.", job.JobDetail().jobKey)
Expand Down
26 changes: 26 additions & 0 deletions quartz/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,32 @@ func TestScheduler_JobWithRetriesCtxDone(t *testing.T) {
sched.Stop()
}

func TestScheduler_MisfiredJob(t *testing.T) {
funcJob := job.NewFunctionJob(func(_ context.Context) (string, error) {
time.Sleep(20 * time.Millisecond)
return "ok", nil
})

misfiredChan := make(chan quartz.ScheduledJob, 1)
sched := quartz.NewStdSchedulerWithOptions(quartz.StdSchedulerOptions{
BlockingExecution: true,
OutdatedThreshold: time.Millisecond,
RetryInterval: time.Millisecond,
MisfiredChan: misfiredChan,
}, nil)

jobDetail := quartz.NewJobDetail(funcJob, quartz.NewJobKey("funcJob"))
err := sched.ScheduleJob(jobDetail, quartz.NewSimpleTrigger(2*time.Millisecond))
assert.IsNil(t, err)

sched.Start(context.Background())

job := <-misfiredChan
assert.Equal(t, job.JobDetail().JobKey().Name(), "funcJob")

sched.Stop()
}

func TestScheduler_PauseResume(t *testing.T) {
var n int32
funcJob := job.NewFunctionJob(func(_ context.Context) (string, error) {
Expand Down
Loading