Skip to content

Commit dbb6ed5

Browse files
committed
feat: added cron.WithSecondOptional()
1 parent 9b0ce3a commit dbb6ed5

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

README.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,47 @@
55

66
a cron library for go, optimized.
77

8-
*forked from robfig/cron@Commits on Jan 6, 2021*
8+
*forked from robfig/cron/v3@Commits on Jan 6, 2021*
99

10-
## Merge pull request
10+
## ✨ Features
1111

12-
- [feat: support overriding the Prev time for a new job/entry](https://github.com/robfig/cron/pull/446)
13-
- [Reduce LARGE CPU consumption by using min-heap instead of sorting all entries every time](https://github.com/robfig/cron/pull/423)
14-
- [add schedule.Prev()](https://github.com/robfig/cron/pull/361)
12+
- Added `cron.WithSecondOptional()`
13+
- Support `WithRunImmediately()` option for job scheduling
14+
- Merge pull request
15+
- [feat: support overriding the Prev time for a new job/entry](https://github.com/robfig/cron/pull/446)
16+
- [Reduce LARGE CPU consumption by using min-heap instead of sorting all entries every time](https://github.com/robfig/cron/pull/423)
17+
- [add schedule.Prev()](https://github.com/robfig/cron/pull/361)
18+
19+
## ⚡️ Quickstart
20+
21+
```go
22+
package main
23+
24+
import (
25+
"fmt"
26+
"time"
27+
28+
"github.com/fufuok/cron"
29+
)
30+
31+
func main() {
32+
c := cron.New(cron.WithSecondOptional())
33+
c.Start()
34+
defer c.Stop()
35+
36+
id, _ := c.AddFunc(
37+
"* * * * *",
38+
func() { fmt.Println("Job.0s:", time.Now()) },
39+
cron.WithRunImmediately(),
40+
)
41+
fmt.Println("Next:", c.Entry(id).Next, "Prev", c.Entry(id).Prev)
42+
43+
_, _ = c.AddFunc("* * * * * *", func() { fmt.Println("Job.s:", time.Now()) })
44+
_, _ = c.AddFunc("1,20-40/5 * * * * *", func() { fmt.Println("Job/5:", time.Now()) })
45+
46+
time.Sleep(2 * time.Minute)
47+
}
48+
```
1549

1650
------
1751

option.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func WithSeconds() Option {
2222
))
2323
}
2424

25+
// WithSecondOptional overrides the parser used for interpreting job schedules to
26+
// include a seconds field as the first one.
27+
// The seconds field as the first is optional.
28+
func WithSecondOptional() Option {
29+
return WithParser(NewParser(
30+
SecondOptional | Minute | Hour | Dom | Month | Dow | Descriptor,
31+
))
32+
}
33+
2534
// WithParser overrides the parser used for interpreting job schedules.
2635
func WithParser(p ScheduleParser) Option {
2736
return func(c *Cron) {

option_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ func TestWithVerboseLogger(t *testing.T) {
4040
t.Error("expected to see some actions, got:", out)
4141
}
4242
}
43+
44+
func TestWithSecondOptional(t *testing.T) {
45+
c := New(WithSecondOptional())
46+
id1, err := c.AddFunc("5 * * * *", func() {})
47+
if err != nil {
48+
t.Errorf("add func %v", err)
49+
}
50+
id2, err := c.AddFunc("* 5 * * * *", func() {})
51+
if err != nil {
52+
t.Errorf("add func %v", err)
53+
}
54+
c.Start()
55+
next1 := c.Entry(id1).Next
56+
next2 := c.Entry(id2).Next
57+
if next1 != next2 {
58+
t.Errorf("expect the same execution time: %s, %s", next1, next2)
59+
}
60+
c.Stop()
61+
}

0 commit comments

Comments
 (0)