Skip to content

Commit

Permalink
Schedule: support Second precision as adverted
Browse files Browse the repository at this point in the history
Updates the schedule API to support seconds precision, as is advertised.

Signed-off-by: joshvanl <me@joshvanl.dev>
  • Loading branch information
JoshVanL committed Jan 15, 2025
1 parent fc76564 commit d5e0f3a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
15 changes: 14 additions & 1 deletion internal/scheduler/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ type Builder struct {
// clock is the clock used to get the current time. Used for manipulating
// time in tests.
clock clock.Clock

// parser parses job schedules.
parser cron.Parser
}

// NewBuilder creates a new scheduler builder.
func NewBuilder() *Builder {
return &Builder{
clock: clock.RealClock{},
parser: cron.NewParser(
cron.Second |
cron.Minute |
cron.Hour |
cron.Dom |
cron.Month |
cron.Dow |
cron.Descriptor,
),
}
}

Expand All @@ -43,7 +55,8 @@ func (b *Builder) Schedule(job *stored.Job) (Interface, error) {
}, nil
}

cronSched, err := cron.ParseStandard(job.GetJob().GetSchedule())
cronSched, err := b.parser.Parse(job.GetJob().GetSchedule())
//cronSched, err := cron.ParseStandard(job.GetJob().GetSchedule())
if err != nil {
return nil, err
}
Expand Down
32 changes: 31 additions & 1 deletion internal/scheduler/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ func Test_Schedule(t *testing.T) {

cronSched, err := cron.ParseStandard("@every 1h")
require.NoError(t, err)
cronSched2, err := cron.NewParser(
cron.Second |
cron.Minute |
cron.Hour |
cron.Dom |
cron.Month |
cron.Dow |
cron.Descriptor,
).Parse("1 2 3 4 5 6")
require.NoError(t, err)

tests := map[string]struct {
job *stored.Job
Expand Down Expand Up @@ -82,6 +92,25 @@ func Test_Schedule(t *testing.T) {
},
expErr: false,
},
"if schedule with cron, expect repeats with exp nil": {
job: &stored.Job{
Begin: &stored.Job_Start{
Start: timestamppb.New(now),
},
Expiration: nil,
Job: &api.Job{
Schedule: ptr.Of("1 2 3 4 5 6"),
Repeats: ptr.Of(uint32(100)),
},
},
expScheduler: &repeats{
start: ptr.Of(now),
exp: nil,
cron: cronSched2,
total: ptr.Of(uint32(100)),
},
expErr: false,
},
"if bad schedule string, expect error": {
job: &stored.Job{
Begin: &stored.Job_Start{
Expand All @@ -102,7 +131,8 @@ func Test_Schedule(t *testing.T) {
testInLoop := test
t.Run(name, func(t *testing.T) {
t.Parallel()
builder := &Builder{clock: clock}
builder := NewBuilder()
builder.clock = clock
gotScheduler, gotErr := builder.Schedule(testInLoop.job)
assert.Equal(t, testInLoop.expScheduler, gotScheduler)
assert.Equal(t, testInLoop.expErr, gotErr != nil, "%v", gotErr)
Expand Down

0 comments on commit d5e0f3a

Please sign in to comment.