Skip to content

Commit

Permalink
Schedule: support Second precision as adverted (#65)
Browse files Browse the repository at this point in the history
* Schedule: support Second precision as adverted

Updates the schedule API to support seconds precision, as is advertised.

Signed-off-by: joshvanl <me@joshvanl.dev>

* Don't return struct if error is not nil

Signed-off-by: joshvanl <me@joshvanl.dev>

---------

Signed-off-by: joshvanl <me@joshvanl.dev>
  • Loading branch information
JoshVanL authored Jan 15, 2025
1 parent fc76564 commit e29e00e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
15 changes: 12 additions & 3 deletions internal/engine/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func (r *Retry) Get(ctx context.Context, name string) (*api.Job, error) {
job, err = a.Get(ctx, name)
return err
})
return job, err
if err != nil {
return nil, err
}
return job, nil
}

func (r *Retry) Delete(ctx context.Context, name string) error {
Expand All @@ -72,7 +75,10 @@ func (r *Retry) List(ctx context.Context, prefix string) (*api.ListResponse, err
resp, err = a.List(ctx, prefix)
return err
})
return resp, err
if err != nil {
return nil, err
}
return resp, nil
}

func (r *Retry) DeliverablePrefixes(ctx context.Context, prefixes ...string) (context.CancelFunc, error) {
Expand All @@ -82,7 +88,10 @@ func (r *Retry) DeliverablePrefixes(ctx context.Context, prefixes ...string) (co
cancel, err = a.DeliverablePrefixes(ctx, prefixes...)
return err
})
return cancel, err
if err != nil {
return nil, err
}
return cancel, nil
}

func (r *Retry) handle(ctx context.Context, fn func(internalapi.Interface) error) error {
Expand Down
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 e29e00e

Please sign in to comment.