Skip to content

Commit 0fc29df

Browse files
committed
feat: optimize struct field align, add tasker Running check
1 parent d84c389 commit 0fc29df

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

batch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77

88
// Expr represents an item in array for batch check
99
type Expr struct {
10+
Err error
1011
Expr string
1112
Due bool
12-
Err error
1313
}
1414

1515
// BatchDue checks if multiple expressions are due for given time (or now).

pkg/tasker/tasker.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,26 @@ type Task struct {
3939

4040
// Tasker is the task manager.
4141
type Tasker struct {
42-
Log *log.Logger
42+
until time.Time
43+
ctx context.Context
4344
loc *time.Location
4445
gron *gronx.Gronx
45-
wg sync.WaitGroup
46-
until time.Time
46+
Log *log.Logger
4747
exprs map[string][]string
4848
tasks map[string]TaskFunc
4949
mutex map[string]uint32
50-
abort bool
51-
timeout bool
52-
verbose bool
53-
ctx context.Context
5450
ctxCancel context.CancelFunc
51+
wg sync.WaitGroup
52+
verbose bool
53+
running bool
54+
timeout bool
55+
abort bool
5556
}
5657

5758
type result struct {
59+
err error
5860
ref string
5961
code int
60-
err error
6162
}
6263

6364
var exit = os.Exit
@@ -228,6 +229,7 @@ func (t *Tasker) now() time.Time {
228229
// Run runs the task manager.
229230
func (t *Tasker) Run() {
230231
t.doSetup()
232+
t.running = true
231233

232234
first := true
233235
for !t.abort && !t.timeout {
@@ -257,6 +259,12 @@ func (t *Tasker) Run() {
257259
}
258260

259261
t.wait()
262+
t.running = false
263+
}
264+
265+
// Running tells if tasker is up and running
266+
func (t *Tasker) Running() bool {
267+
return t.running && !t.abort && !t.timeout
260268
}
261269

262270
// Stop the task manager.
@@ -396,7 +404,7 @@ func (t *Tasker) doRun(ctx context.Context, ref string, task TaskFunc, rc chan r
396404
t.mutex[ref] = 0
397405
}
398406

399-
rc <- result{ref, code, err}
407+
rc <- result{err, ref, code}
400408
}
401409

402410
func (t *Tasker) doOut(rc chan result) {

pkg/tasker/tasker_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tasker
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"strings"
98
"testing"
@@ -89,7 +88,7 @@ func TestRun(t *testing.T) {
8988
fin2 + " [tasker] task [* * * * * *][#1] ran successfully",
9089
}
9190

92-
buf, _ := ioutil.ReadFile("../../test/tasker.out")
91+
buf, _ := os.ReadFile("../../test/tasker.out")
9392
buffer := string(buf)
9493
fmt.Println(buffer)
9594

@@ -167,7 +166,7 @@ func TestWithContext(t *testing.T) {
167166
t.Errorf("task should run 2 times, ran %d times", called)
168167
}
169168

170-
buf, _ := ioutil.ReadFile("../../test/tasker-ctx.out")
169+
buf, _ := os.ReadFile("../../test/tasker-ctx.out")
171170
fmt.Println(string(buf))
172171
})
173172
}

0 commit comments

Comments
 (0)