-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
92 lines (71 loc) · 1.47 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package engine
import (
"context"
"time"
)
type Kind uint8
const (
KindNormal = iota
)
var (
stdTimeout time.Duration = 0
)
type TaskMeta[KEY Key] struct {
id uint64
Kind Kind
Key KEY
Priority int
Describe string
createdAt time.Time
execBeginAt time.Time
execEndAt time.Time
timeout time.Duration
TaskStatistics
}
func (t *TaskMeta[KEY]) SortKey() int {
return t.Priority
}
func (t *TaskMeta[KEY]) SetPriority(priority int) {
t.Priority = priority
}
func (t *TaskMeta[KEY]) SetKind(k Kind) {
t.Kind = k
}
func (t *TaskMeta[KEY]) SetKey(key KEY) {
t.Key = key
}
func (t *TaskMeta[KEY]) Id() uint64 {
return t.id
}
type TaskStatistics struct {
reDoTimes uint
errTimes int
}
type Task[KEY Key] struct {
ctx context.Context
TaskMeta[KEY]
TaskFunc[KEY]
errs []error
}
func (t *Task[KEY]) Errs() []error {
return t.errs
}
func (t *Task[KEY]) SetContext(ctx context.Context) {
t.ctx = ctx
}
type TaskInterface[KEY Key] interface {
Do(ctx context.Context) ([]*Task[KEY], error)
}
type Tasks[KEY Key] []*Task[KEY]
func (tasks Tasks[KEY]) Less(i, j int) bool {
return tasks[i].Priority > tasks[j].Priority
}
// ---------------
type ErrHandle func(context.Context, error)
type TaskFunc[KEY Key] func(ctx context.Context) ([]*Task[KEY], error)
func (t TaskFunc[KEY]) Do(ctx context.Context) ([]*Task[KEY], error) {
return t(ctx)
}
func emptyTaskFunc[KEY Key](ctx context.Context) ([]*Task[KEY], error) {
return nil, nil
}