Skip to content

Commit

Permalink
Merge pull request #25 from adhocore/refactor-batch
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Apr 25, 2023
2 parents 609673f + 75433bf commit 0175ee2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ project_name: tasker

release:
prerelease: auto
name_template: "v{{.Version}}"
draft: true
name_template: "Version v{{.Version}}"
# draft: true
mode: "keep-existing"

before:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func main() {
})

// run task without overlap, set concurrent flag to false:
concurrent := false
taskr.Task("* * * * * *", , tasker.Taskify("sleep 2", tasker.Option{}), concurrent)
concurrent := false
taskr.Task("* * * * * *", , tasker.Taskify("sleep 2", tasker.Option{}), concurrent)

// every 10 minute with arbitrary command
taskr.Task("@10minutes", taskr.Taskify("command --option val -- args", tasker.Option{Shell: "/bin/sh -c"}))
Expand Down
22 changes: 18 additions & 4 deletions batch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gronx

import (
"strings"
"time"
)

Expand All @@ -9,7 +10,6 @@ type Expr struct {
Expr string
Due bool
Err error
segs []string
}

// BatchDue checks if multiple expressions are due for given time (or now).
Expand All @@ -18,20 +18,34 @@ func (g *Gronx) BatchDue(exprs []string, ref ...time.Time) []Expr {
ref = append(ref, time.Now())
g.C.SetRef(ref[0])

batch := make([]Expr, len(exprs))
var segs []string

cache, batch := map[string]Expr{}, make([]Expr, len(exprs))
for i := range exprs {
if batch[i].segs, batch[i].Err = Segments(exprs[i]); batch[i].Err != nil {
batch[i].Expr = exprs[i]
segs, batch[i].Err = Segments(exprs[i])
key := strings.Join(segs, " ")
if batch[i].Err != nil {
cache[key] = batch[i]
continue
}

if c, ok := cache[key]; ok {
batch[i] = c
batch[i].Expr = exprs[i]
continue
}

due := true
for pos, seg := range batch[i].segs {
for pos, seg := range segs {
if seg != "*" && seg != "?" {
if due, batch[i].Err = g.C.CheckDue(seg, pos); !due || batch[i].Err != nil {
break
}
}
}
batch[i].Due = due
cache[key] = batch[i]
}
return batch
}
4 changes: 2 additions & 2 deletions pkg/tasker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func main() {
})

// run task without overlap, set concurrent flag to false:
concurrent := false
taskr.Task("* * * * * *", , tasker.Taskify("sleep 2", tasker.Option{}), concurrent)
concurrent := false
taskr.Task("* * * * * *", , tasker.Taskify("sleep 2", tasker.Option{}), concurrent)

// every 10 minute with arbitrary command
taskr.Task("@10minutes", taskr.Taskify("command --option val -- args", tasker.Option{Shell: "/bin/sh -c"}))
Expand Down

0 comments on commit 0175ee2

Please sign in to comment.