-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
97 lines (76 loc) · 1.82 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
93
94
95
96
97
package yotei
import (
"context"
"fmt"
"sync/atomic"
"time"
)
// Task is a sequential task executionable in the [yotei.Scheduler].
//
// Use [NewTask] to create a new sequential task.
type Task struct {
handler Handler
weight atomic.Uint64
duration atomic.Int64
locked atomic.Bool
concurrent atomic.Bool
}
// NewTask creates a new sequential task with the given handler.
func NewTask(handler Handler) *Task {
if handler == nil {
panic("no task handler defined. please ensure the task handler is not nil")
}
task := &Task{
handler: handler,
}
task.weight.Store(1)
task.duration.Store(int64(DurationUnlimited))
task.concurrent.Store(false)
return task
}
func (task *Task) Lock() {
task.locked.Store(true)
}
func (task *Task) Unlock() {
task.locked.Store(false)
}
func (task *Task) Concurrent(value bool) *Task {
task.concurrent.Store(value)
return task
}
func (task *Task) IsLocked() bool {
return task.locked.Load()
}
func (task *Task) IsConcurrent() bool {
return task.concurrent.Load()
}
func (task *Task) Lasts(duration time.Duration) *Task {
task.duration.Store(int64(duration))
return task
}
func (task *Task) Duration() time.Duration {
return time.Duration(task.duration.Load())
}
func (task *Task) Weights(weight uint64) *Task {
task.weight.Store(weight)
return task
}
func (task *Task) Weight() uint64 {
return task.weight.Load()
}
func (task *Task) Handle(ctx context.Context) Action {
if task.handler == nil {
panic("no task handler defined. please ensure the task handler is not nil")
}
return task.handler.Handle(ctx)
}
// String returns a string representation of a task.
func (task *Task) String() string {
return fmt.Sprintf(
"Task{weight=%d, duration=%s, is_concurrent=%t, is_locked=%t}",
task.Weight(),
task.Duration(),
task.IsConcurrent(),
task.IsLocked(),
)
}