-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
167 lines (151 loc) · 4.94 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"context"
"fmt"
"log"
"math/rand"
"strconv"
"time"
lighttaskscheduler "github.com/memory-overflow/light-task-scheduler"
"github.com/memory-overflow/light-task-scheduler/actuator"
memeorycontainer "github.com/memory-overflow/light-task-scheduler/container/memory_container"
)
// AddTask add 任务结构
type AddTask struct {
StartTime time.Time
A, B int32
}
func add(ctx context.Context, ftask *lighttaskscheduler.Task) (data interface{}, err error) {
task, ok := ftask.TaskItem.(AddTask)
if !ok {
return nil, fmt.Errorf("TaskItem not be set to AddTask")
}
log.Printf("start run task %s, Attempts: %d\n", ftask.TaskId, ftask.TaskAttemptsTime)
// 模拟 25 % 的概率出错
if rand.Intn(4) == 0 {
return nil, fmt.Errorf("error test")
}
// 模拟 25% 概率超时
time.Sleep(time.Duration(rand.Intn(4000))*time.Millisecond + 2*time.Second)
return task.A + task.B, nil
}
type callbackReceiver struct {
}
// modePolling 默认模式,状态轮询模式
func modePolling() *lighttaskscheduler.TaskScheduler {
container := memeorycontainer.MakeQueueContainer(10000, 100*time.Millisecond)
actuator, err := actuator.MakeFucntionActuator(add, nil)
if err != nil {
log.Fatal("make fucntionActuato error: ", err)
}
sch, err := lighttaskscheduler.MakeScheduler(
container, actuator, nil,
lighttaskscheduler.Config{
TaskLimit: 2,
TaskTimeout: 5 * time.Second, // 5s 超时
EnableFinshedTaskList: true, // 开启已完成任务返回功能
MaxFailedAttempts: 3, // 失败重试次数
SchedulingPollInterval: 0,
StatePollInterval: 0,
},
)
if err != nil {
log.Fatal("make scheduler error: ", err)
}
return sch
}
type addCallbackReceiver struct {
taskChannel chan lighttaskscheduler.Task
}
func (rec addCallbackReceiver) GetCallbackChannel(ctx context.Context) chan lighttaskscheduler.Task {
return rec.taskChannel
}
// modeCallback 仅回调模式
func modeCallback() *lighttaskscheduler.TaskScheduler {
container := memeorycontainer.MakeQueueContainer(10000, 100*time.Millisecond)
taskChannel := make(chan lighttaskscheduler.Task, 10000)
actuator, err := actuator.MakeFucntionActuator(add, nil)
actuator.SetCallbackChannel(taskChannel)
if err != nil {
log.Fatal("make fucntionActuato error: ", err)
}
receiver := addCallbackReceiver{
taskChannel: taskChannel,
}
sch, err := lighttaskscheduler.MakeScheduler(
container, actuator, nil,
lighttaskscheduler.Config{
TaskLimit: 2,
TaskTimeout: 5 * time.Second, // 5s 超时
EnableFinshedTaskList: true, // 开启已完成任务返回功能
MaxFailedAttempts: 3, // 失败重试次数
SchedulingPollInterval: 0,
DisableStatePoll: true,
EnableStateCallback: true,
CallbackReceiver: receiver,
},
)
if err != nil {
log.Fatal("make scheduler error: ", err)
}
return sch
}
// modeCallback 轮询 + 回调模式
func modePollingCallback() *lighttaskscheduler.TaskScheduler {
container := memeorycontainer.MakeQueueContainer(10000, 100*time.Millisecond)
taskChannel := make(chan lighttaskscheduler.Task, 10000)
actuator, err := actuator.MakeFucntionActuator(add, nil)
actuator.SetCallbackChannel(taskChannel)
if err != nil {
log.Fatal("make fucntionActuato error: ", err)
}
receiver := addCallbackReceiver{
taskChannel: taskChannel,
}
sch, err := lighttaskscheduler.MakeScheduler(
container, actuator, nil,
lighttaskscheduler.Config{
TaskLimit: 2,
TaskTimeout: 5 * time.Second, // 5s 超时
EnableFinshedTaskList: true, // 开启已完成任务返回功能
MaxFailedAttempts: 3, // 失败重试次数
SchedulingPollInterval: 0,
DisableStatePoll: false,
StatePollInterval: 0,
EnableStateCallback: true,
CallbackReceiver: receiver,
},
)
if err != nil {
log.Fatal("make scheduler error: ", err)
}
return sch
}
func main() {
// 模式1, 默认模式,状态轮询模式
// sch := modePolling()
// 模式2, 回调模式
// sch := modeCallback()
// 模式3,轮询 + 回调模式
sch := modePollingCallback()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 10; i++ {
sch.AddTask(context.Background(),
lighttaskscheduler.Task{
TaskId: strconv.Itoa(i),
TaskItem: AddTask{
A: r.Int31() % 1000,
B: r.Int31() % 1000,
},
})
}
for task := range sch.FinshedTasks() {
if task.TaskStatus == lighttaskscheduler.TASK_STATUS_FAILED {
log.Printf("failed task %s, reason: %s, timecost: %dms, attempt times: %d\n",
task.TaskId, task.FailedReason, task.TaskEnbTime.Sub(task.TaskStartTime).Milliseconds(), task.TaskAttemptsTime)
} else if task.TaskStatus == lighttaskscheduler.TASK_STATUS_SUCCESS {
log.Printf("success task %s, timecost: %dms, attempt times: %d\n", task.TaskId,
task.TaskEnbTime.Sub(task.TaskStartTime).Milliseconds(), task.TaskAttemptsTime)
}
}
}