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 2443395
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/dapr/kit => ../../dapr/kit
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/dapr/kit v0.13.1-0.20241015130326-866002abe68a h1:3+EDN84/gtelE14Ka3TW8pP52vC8QdSHKeNIe3JpsYU=
github.com/dapr/kit v0.13.1-0.20241015130326-866002abe68a/go.mod h1:Hz1W2LmWfA4UX/12MdA+brsf+np6f/1dJt6C6F63cjI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
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 2443395

Please sign in to comment.