Skip to content

Commit

Permalink
Merge pull request #21 from adhocore/18-batch-check
Browse files Browse the repository at this point in the history
Batch due check
  • Loading branch information
adhocore authored Apr 7, 2023
2 parents 00d0da6 + a174bc4 commit 31f3843
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ gron.IsDue(expr) // true|false, nil
gron.IsDue(expr, time.Date(2021, time.April, 1, 1, 1, 0, 0, time.UTC)) // true|false, nil
```

### Batch Due Check

If you have multiple cron expressions to check due on same reference time use `BatchDue()`:
```go
gron := gronx.New()
exprs := []string{"* * * * *", "0 */5 * * * *"}

// gives []gronx.Expr{} array, each item has Due flag and Err enountered.
dues := gron.BatchDue(exprs)

for _, expr := range dues {
if expr.Err != nil {
// Handle err
} else if expr.Due {
// Handle due
}
}

// Or with given time
ref := time.Now()
gron.BatchDue(exprs, ref)
```

### Next Tick

To find out when is the cron due next (onwards):
Expand Down
40 changes: 40 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gronx

import (
"time"
)

// Expr represents an item in array for batch check
type Expr struct {
Expr string
Due bool
Err error
segs []string
}

// BatchDue checks if multiple expressions are due for given time (or now).
// It returns []Expr with filled in Due and Err values.
func (g *Gronx) BatchDue(exprs []string, ref ...time.Time) []Expr {
if len(ref) > 0 {
g.C.SetRef(ref[0])
} else {
g.C.SetRef(time.Now())
}

batch := make([]Expr, len(exprs))
for i := range exprs {
if batch[i].segs, batch[i].Err = Segments(exprs[i]); batch[i].Err != nil {
continue
}
due := true
for pos, seg := range batch[i].segs {
if seg != "*" && seg != "?" {
if due, batch[i].Err = g.C.CheckDue(seg, pos); !due || batch[i].Err != nil {
break
}
}
}
batch[i].Due = due
}
return batch
}
40 changes: 40 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gronx

import (
"fmt"
"testing"
"time"
)

func TestBatch(t *testing.T) {
gron := New()

t.Run("batch no error", func(t *testing.T) {
ref := time.Now()
exprs := []string{"@everysecond", "* * * * * *", "* * * * * *"}
exprs = append(exprs, fmt.Sprintf("* %d * * * * %d", ref.Minute(), ref.Year()))
exprs = append(exprs, fmt.Sprintf("* * * * * * %d-%d", ref.Year()-1, ref.Year()+1))

for _, expr := range gron.BatchDue(exprs) {
if expr.Err != nil {
t.Errorf("%s error: %#v", expr.Expr, expr.Err)
}
if !expr.Due {
t.Errorf("%s must be due", expr.Expr)
}
}
})

t.Run("batch error", func(t *testing.T) {
exprs := []string{"* * * *", "A B C D E F"}
ref, _ := time.Parse(FullDateFormat, "2022-02-02 02:02:02")
for _, expr := range gron.BatchDue(exprs, ref) {
if expr.Err == nil {
t.Errorf("%s expected error", expr.Expr)
}
if expr.Due {
t.Errorf("%s must not be due when there is error", expr.Expr)
}
}
})
}

0 comments on commit 31f3843

Please sign in to comment.