From ec135dd0fd377b35da59ce804ddc6419dcfd2c5f Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Sun, 18 Aug 2024 08:21:09 +0700 Subject: [PATCH] feat: optimize struct field align, add tasker Running check --- batch.go | 2 +- pkg/tasker/tasker.go | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 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) {