From 0fc29dfd426fd0eae5714e37efb24ed42ebcbff7 Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Sun, 18 Aug 2024 08:10:31 +0700 Subject: [PATCH] feat: optimize struct field align, add tasker Running check --- batch.go | 2 +- pkg/tasker/tasker.go | 26 +++++++++++++++++--------- pkg/tasker/tasker_test.go | 5 ++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/batch.go b/batch.go index 63d85ec..37fddb9 100644 --- a/batch.go +++ b/batch.go @@ -7,9 +7,9 @@ import ( // Expr represents an item in array for batch check type Expr struct { + Err error Expr string Due bool - Err error } // BatchDue checks if multiple expressions are due for given time (or now). diff --git a/pkg/tasker/tasker.go b/pkg/tasker/tasker.go index ed0e5dc..05506f7 100644 --- a/pkg/tasker/tasker.go +++ b/pkg/tasker/tasker.go @@ -39,25 +39,26 @@ type Task struct { // Tasker is the task manager. type Tasker struct { - Log *log.Logger + until time.Time + ctx context.Context loc *time.Location gron *gronx.Gronx - wg sync.WaitGroup - until time.Time + Log *log.Logger exprs map[string][]string tasks map[string]TaskFunc mutex map[string]uint32 - abort bool - timeout bool - verbose bool - ctx context.Context ctxCancel context.CancelFunc + wg sync.WaitGroup + verbose bool + running bool + timeout bool + abort bool } type result struct { + err error ref string code int - err error } var exit = os.Exit @@ -228,6 +229,7 @@ func (t *Tasker) now() time.Time { // Run runs the task manager. func (t *Tasker) Run() { t.doSetup() + t.running = true first := true for !t.abort && !t.timeout { @@ -257,6 +259,12 @@ func (t *Tasker) Run() { } t.wait() + t.running = false +} + +// Running tells if tasker is up and running +func (t *Tasker) Running() bool { + return t.running && !t.abort && !t.timeout } // Stop the task manager. @@ -396,7 +404,7 @@ func (t *Tasker) doRun(ctx context.Context, ref string, task TaskFunc, rc chan r t.mutex[ref] = 0 } - rc <- result{ref, code, err} + rc <- result{err, ref, code} } func (t *Tasker) doOut(rc chan result) { diff --git a/pkg/tasker/tasker_test.go b/pkg/tasker/tasker_test.go index a60c80d..cf3eba0 100644 --- a/pkg/tasker/tasker_test.go +++ b/pkg/tasker/tasker_test.go @@ -3,7 +3,6 @@ package tasker import ( "context" "fmt" - "io/ioutil" "os" "strings" "testing" @@ -89,7 +88,7 @@ func TestRun(t *testing.T) { fin2 + " [tasker] task [* * * * * *][#1] ran successfully", } - buf, _ := ioutil.ReadFile("../../test/tasker.out") + buf, _ := os.ReadFile("../../test/tasker.out") buffer := string(buf) fmt.Println(buffer) @@ -167,7 +166,7 @@ func TestWithContext(t *testing.T) { t.Errorf("task should run 2 times, ran %d times", called) } - buf, _ := ioutil.ReadFile("../../test/tasker-ctx.out") + buf, _ := os.ReadFile("../../test/tasker-ctx.out") fmt.Println(string(buf)) }) }