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

fix(scheduler)!: reset the queue after deleting a job #83

Merged
merged 1 commit into from
Jan 4, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Scheduler interface {

// DeleteJob removes the job with the specified key from the
// scheduler's execution queue.
DeleteJob(key int) error
DeleteJob(ctx context.Context, key int) error

// Clear removes all of the scheduled jobs.
Clear() error
Expand Down
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func sampleScheduler(ctx context.Context, wg *sync.WaitGroup) {

fmt.Println(scheduledJob.Trigger().Description())
fmt.Println("Before delete: ", sched.GetJobKeys())
_ = sched.DeleteJob(cronJob.Key())
_ = sched.DeleteJob(ctx, cronJob.Key())
fmt.Println("After delete: ", sched.GetJobKeys())

time.Sleep(time.Second * 2)
Expand Down
7 changes: 5 additions & 2 deletions quartz/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Scheduler interface {

// DeleteJob removes the job with the specified key from the
// scheduler's execution queue.
DeleteJob(key int) error
DeleteJob(ctx context.Context, key int) error

// Clear removes all of the scheduled jobs.
Clear() error
Expand Down Expand Up @@ -225,13 +225,16 @@ func (sched *StdScheduler) GetScheduledJob(key int) (ScheduledJob, error) {
}

// DeleteJob removes the Job with the specified key if present.
func (sched *StdScheduler) DeleteJob(key int) error {
func (sched *StdScheduler) DeleteJob(ctx context.Context, key int) error {
sched.mtx.Lock()
defer sched.mtx.Unlock()

for i, scheduled := range sched.queue.ScheduledJobs() {
if scheduled.Job().Key() == key {
_, err := sched.queue.Remove(i)
if err == nil {
sched.reset(ctx)
}
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions quartz/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func TestScheduler(t *testing.T) {
_, err = sched.GetScheduledJob(jobKeys[0])
assertEqual(t, err, nil)

err = sched.DeleteJob(shellJob.Key())
err = sched.DeleteJob(ctx, shellJob.Key())
assertEqual(t, err, nil)

nonExistentJobKey := 1111
_, err = sched.GetScheduledJob(nonExistentJobKey)
assertNotEqual(t, err, nil)

err = sched.DeleteJob(nonExistentJobKey)
err = sched.DeleteJob(ctx, nonExistentJobKey)
assertNotEqual(t, err, nil)

scheduledJobKeys = sched.GetJobKeys()
Expand Down