Skip to content

Commit cbac89e

Browse files
Julius BachnickJulius Bachnick
Julius Bachnick
authored and
Julius Bachnick
committedApr 14, 2020
Updated used cron library to version 3
1 parent c1c9c3e commit cbac89e

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed
 

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8-
- Nothing so far
8+
- Updated the used cron library to version 3
99

1010
## [v1.0.0] - 2020-02-28
1111
- Use error wrapping of standard library instead of github.com/pkg/errors

‎README.md

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func main() {
4545
// emit your own custom event every day at 09:00
4646
cron.ScheduleEvent("0 9 * * *", MyEvent{}),
4747

48+
// emit your own custom event every day at 09:00:30
49+
cron.ScheduleEvent("30 0 9 * * *", MyEvent{}),
50+
4851
// cron expressions can be hard to read and might be overkill
4952
cron.ScheduleEventEvery(time.Hour, MyEvent{}),
5053

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.14
55
require (
66
github.com/go-joe/joe v0.6.0
77
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
8+
github.com/robfig/cron/v3 v3.0.0
89
github.com/stretchr/testify v1.3.0
910
go.uber.org/zap v1.9.1
1011
)

‎go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
88
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
99
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 h1:x7xEyJDP7Hv3LVgvWhzioQqbC/KtuUhTigKlH/8ehhE=
1010
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
11+
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
12+
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
13+
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
14+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
15+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
1116
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
1217
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1318
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=

‎module.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ import (
99
"time"
1010

1111
"github.com/go-joe/joe"
12-
"github.com/robfig/cron"
12+
"github.com/robfig/cron/v3"
1313
"go.uber.org/zap"
1414
)
1515

16+
// defaultParser accepts standard cron schedules with optional seconds.
17+
var defaultParser = cron.NewParser(
18+
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
19+
)
20+
1621
// A Job is a joe.Module that runs a single cron job on a given interval.
1722
type Job struct {
1823
cron *cron.Cron
@@ -42,7 +47,7 @@ func ScheduleEvent(schedule string, events ...interface{}) *Job {
4247
events = []interface{}{Event{}}
4348
}
4449

45-
s, err := cron.Parse(schedule)
50+
s, err := defaultParser.Parse(schedule)
4651
if err != nil {
4752
err = fmt.Errorf("invalid cron schedule: %w", err)
4853
}
@@ -63,11 +68,13 @@ func ScheduleEvent(schedule string, events ...interface{}) *Job {
6368
}
6469

6570
// ScheduleFunc creates a joe.Module that runs the given function on a given
66-
// cron schedule (e.g. "0 0 * * *"). If the passed schedule is not a valid cron
71+
// cron schedule (e.g. "0 0 * * *"). Optionally, the cron schedule can also
72+
// contain seconds, i.e. "30 0 0 * * *".
73+
// If the passed schedule is not a valid cron
6774
// schedule as accepted by https://godoc.org/github.com/robfig/cron the error
6875
// will be returned when the bot is started.
6976
func ScheduleFunc(schedule string, fun func()) *Job {
70-
s, err := cron.Parse(schedule)
77+
s, err := defaultParser.Parse(schedule)
7178
if err != nil {
7279
err = fmt.Errorf("invalid cron schedule: %w", err)
7380
}
@@ -162,8 +169,13 @@ func (j *Job) Start(logger *zap.Logger, events joe.EventEmitter) error {
162169
)
163170

164171
job := j.fun(events)
165-
j.cron = cron.New()
166-
j.cron.ErrorLog, _ = zap.NewStdLogAt(logger, zap.ErrorLevel)
172+
cronLogger, _ := zap.NewStdLogAt(logger, zap.ErrorLevel)
173+
j.cron = cron.New(
174+
cron.WithParser(defaultParser),
175+
cron.WithLogger(
176+
cron.PrintfLogger(cronLogger),
177+
),
178+
)
167179
j.cron.Schedule(j.schedule, job)
168180
j.cron.Start()
169181

‎module_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func TestJob_InvalidSchedule(t *testing.T) {
214214
job := cron.ScheduleEvent("foobar")
215215

216216
err := job.Start(logger, brain)
217-
require.EqualError(t, err, "invalid cron schedule: Expected 5 to 6 fields, found 1: foobar")
217+
require.EqualError(t, err, "invalid cron schedule: expected 5 to 6 fields, found 1: [foobar]")
218218
}
219219

220220
func TestExampleSchedule(t *testing.T) {
@@ -284,7 +284,7 @@ func TestScheduleEvent_Error(t *testing.T) {
284284
job := cron.ScheduleEvent("foobar")
285285

286286
err := job.Start(logger, brain)
287-
require.EqualError(t, err, "invalid cron schedule: Expected 5 to 6 fields, found 1: foobar")
287+
require.EqualError(t, err, "invalid cron schedule: expected 5 to 6 fields, found 1: [foobar]")
288288

289289
assert.NoError(t, job.Close())
290290
brain.Finish()
@@ -299,7 +299,7 @@ func TestScheduleFunc_Error(t *testing.T) {
299299
})
300300

301301
err := job.Start(logger, brain)
302-
require.EqualError(t, err, "invalid cron schedule: Expected 5 to 6 fields, found 1: foobar")
302+
require.EqualError(t, err, "invalid cron schedule: expected 5 to 6 fields, found 1: [foobar]")
303303

304304
assert.NoError(t, job.Close())
305305
brain.Finish()

0 commit comments

Comments
 (0)
Please sign in to comment.