Skip to content

Commit

Permalink
feat(scheduler)!: handle errors in the GetJobKeys method (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Mar 30, 2024
1 parent 375ceeb commit e55b059
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Scheduler interface {
// For a job key to be returned, the job must satisfy all of the
// matchers specified.
// Given no matchers, it returns the keys of all scheduled jobs.
GetJobKeys(...Matcher[ScheduledJob]) []*JobKey
GetJobKeys(...Matcher[ScheduledJob]) ([]*JobKey, error)

// GetScheduledJob returns the scheduled job with the specified key.
GetScheduledJob(jobKey *JobKey) (ScheduledJob, error)
Expand Down
6 changes: 4 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ func sampleScheduler(ctx context.Context, wg *sync.WaitGroup) {
}

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

time.Sleep(time.Second * 2)
sched.Stop()
Expand Down
39 changes: 22 additions & 17 deletions matcher/job_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,40 @@ func TestMatcher_JobAll(t *testing.T) {
}
sched.Start(context.Background())

assert.Equal(t, len(sched.GetJobKeys(matcher.JobActive())), 4)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobPaused())), 0)
assert.Equal(t, jobCount(sched, matcher.JobActive()), 4)
assert.Equal(t, jobCount(sched, matcher.JobPaused()), 0)

assert.Equal(t, len(sched.GetJobKeys(matcher.JobGroupEquals(quartz.DefaultGroup))), 2)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobGroupContains("_"))), 2)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobGroupStartsWith("group_"))), 2)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobGroupEndsWith("_update"))), 1)
assert.Equal(t, jobCount(sched, matcher.JobGroupEquals(quartz.DefaultGroup)), 2)
assert.Equal(t, jobCount(sched, matcher.JobGroupContains("_")), 2)
assert.Equal(t, jobCount(sched, matcher.JobGroupStartsWith("group_")), 2)
assert.Equal(t, jobCount(sched, matcher.JobGroupEndsWith("_update")), 1)

assert.Equal(t, len(sched.GetJobKeys(matcher.JobNameEquals("job_monitor"))), 2)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobNameContains("_"))), 4)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobNameStartsWith("job_"))), 4)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobNameEndsWith("_update"))), 2)
assert.Equal(t, jobCount(sched, matcher.JobNameEquals("job_monitor")), 2)
assert.Equal(t, jobCount(sched, matcher.JobNameContains("_")), 4)
assert.Equal(t, jobCount(sched, matcher.JobNameStartsWith("job_")), 4)
assert.Equal(t, jobCount(sched, matcher.JobNameEndsWith("_update")), 2)

// multiple matchers
assert.Equal(t, len(sched.GetJobKeys(
assert.Equal(t, jobCount(sched,
matcher.JobNameEquals("job_monitor"),
matcher.JobGroupEquals(quartz.DefaultGroup),
matcher.JobActive(),
)), 1)
), 1)

assert.Equal(t, len(sched.GetJobKeys(
assert.Equal(t, jobCount(sched,
matcher.JobNameEquals("job_monitor"),
matcher.JobGroupEquals(quartz.DefaultGroup),
matcher.JobPaused(),
)), 0)
), 0)

// no matchers
assert.Equal(t, len(sched.GetJobKeys()), 4)
assert.Equal(t, jobCount(sched), 4)

err = sched.PauseJob(quartz.NewJobKey("job_monitor"))
assert.IsNil(t, err)

assert.Equal(t, len(sched.GetJobKeys(matcher.JobActive())), 3)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobPaused())), 1)
assert.Equal(t, jobCount(sched, matcher.JobActive()), 3)
assert.Equal(t, jobCount(sched, matcher.JobPaused()), 1)

sched.Stop()
}
Expand Down Expand Up @@ -112,3 +112,8 @@ func TestMatcher_CustomStringOperator(t *testing.T) {
var op matcher.StringOperator = func(_, _ string) bool { return true }
assert.NotEqual(t, matcher.NewJobGroup(&op, "group1"), nil)
}

func jobCount(sched quartz.Scheduler, matchers ...quartz.Matcher[quartz.ScheduledJob]) int {
keys, _ := sched.GetJobKeys(matchers...)
return len(keys)
}
11 changes: 7 additions & 4 deletions quartz/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Scheduler interface {
// For a job key to be returned, the job must satisfy all of the
// matchers specified.
// Given no matchers, it returns the keys of all scheduled jobs.
GetJobKeys(...Matcher[ScheduledJob]) []*JobKey
GetJobKeys(...Matcher[ScheduledJob]) ([]*JobKey, error)

// GetScheduledJob returns the scheduled job with the specified key.
GetScheduledJob(jobKey *JobKey) (ScheduledJob, error)
Expand Down Expand Up @@ -235,16 +235,19 @@ func (sched *StdScheduler) IsStarted() bool {
// GetJobKeys returns the keys of scheduled jobs.
// For a job key to be returned, the job must satisfy all of the matchers specified.
// Given no matchers, it returns the keys of all scheduled jobs.
func (sched *StdScheduler) GetJobKeys(matchers ...Matcher[ScheduledJob]) []*JobKey {
func (sched *StdScheduler) GetJobKeys(matchers ...Matcher[ScheduledJob]) ([]*JobKey, error) {
sched.mtx.Lock()
defer sched.mtx.Unlock()

scheduledJobs, _ := sched.queue.ScheduledJobs(matchers)
scheduledJobs, err := sched.queue.ScheduledJobs(matchers)
if err != nil {
return nil, err
}
keys := make([]*JobKey, 0, len(scheduledJobs))
for _, scheduled := range scheduledJobs {
keys = append(keys, scheduled.JobDetail().jobKey)
}
return keys
return keys, nil
}

// GetScheduledJob returns the ScheduledJob with the specified key.
Expand Down
19 changes: 13 additions & 6 deletions quartz/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func TestScheduler(t *testing.T) {
assert.IsNil(t, err)

time.Sleep(time.Second)
scheduledJobKeys := sched.GetJobKeys()
scheduledJobKeys, err := sched.GetJobKeys()
assert.IsNil(t, err)
assert.Equal(t, scheduledJobKeys, []*quartz.JobKey{jobKeys[0], jobKeys[3]})

_, err = sched.GetScheduledJob(jobKeys[0])
Expand All @@ -74,12 +75,13 @@ func TestScheduler(t *testing.T) {
err = sched.DeleteJob(nonExistentJobKey)
assert.ErrorIs(t, err, quartz.ErrJobNotFound)

scheduledJobKeys = sched.GetJobKeys()
scheduledJobKeys, err = sched.GetJobKeys()
assert.IsNil(t, err)
assert.Equal(t, len(scheduledJobKeys), 1)
assert.Equal(t, scheduledJobKeys, []*quartz.JobKey{jobKeys[3]})

_ = sched.Clear()
assert.Equal(t, len(sched.GetJobKeys()), 0)
assert.Equal(t, jobCount(sched), 0)
sched.Stop()
_, err = curlJob.DumpResponse(true)
assert.IsNil(t, err)
Expand Down Expand Up @@ -433,9 +435,9 @@ func TestScheduler_PauseResumeErrors(t *testing.T) {
err = sched.PauseJob(quartz.NewJobKey("funcJob2"))
assert.ErrorIs(t, err, quartz.ErrJobNotFound)

assert.Equal(t, len(sched.GetJobKeys(matcher.JobPaused())), 1)
assert.Equal(t, len(sched.GetJobKeys(matcher.JobActive())), 0)
assert.Equal(t, len(sched.GetJobKeys()), 1)
assert.Equal(t, jobCount(sched, matcher.JobPaused()), 1)
assert.Equal(t, jobCount(sched, matcher.JobActive()), 0)
assert.Equal(t, jobCount(sched), 1)

sched.Stop()
}
Expand Down Expand Up @@ -484,3 +486,8 @@ func TestScheduler_StartStop(t *testing.T) {
sched.Stop()
assert.Equal(t, sched.IsStarted(), false)
}

func jobCount(sched quartz.Scheduler, matchers ...quartz.Matcher[quartz.ScheduledJob]) int {
keys, _ := sched.GetJobKeys(matchers...)
return len(keys)
}

0 comments on commit e55b059

Please sign in to comment.