Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(batch): improve perf, remove Expr.segs, fill Expr.Expr #25

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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