@@ -9,6 +9,8 @@ Each `Task` represents a function scheduled by priority.
9
9
10
10
- Task prioritization: You can register tasks with a priority level influencing the execution order.
11
11
- Concurrent execution: Tasks are executed concurrently by a pool of workers.
12
+ - Middleware: You can apply middleware to the ` TaskManager ` to add additional functionality.
13
+ - Results: You can access the results of the tasks via the ` Results ` channel.
12
14
- Rate limiting: You can rate limit the tasks schedule by setting a maximum number of jobs per second.
13
15
- Cancellation: Tasks can be canceled before or while they are running.
14
16
@@ -102,44 +104,46 @@ package main
102
104
103
105
import (
104
106
" fmt"
105
- " log"
106
107
" time"
107
108
109
+ " github.com/google/uuid"
108
110
worker " github.com/hyp3rd/go-worker"
109
111
" github.com/hyp3rd/go-worker/middleware"
110
- " github.com/google/uuid"
111
112
)
112
113
113
- func main () {
114
+ func main () {
114
115
tm := worker.NewTaskManager (5 , 10 )
115
- // Example of using zap logger from uber
116
- logger := log.Default ()
117
116
118
117
// apply middleware in the same order as you want to execute them
119
118
tm = worker.RegisterMiddleware (tm,
120
- // middleware.YourMiddleware,
119
+ // middleware.YourMiddleware,
121
120
func (next worker.Service ) worker.Service {
122
- return middleware.NewLoggerMiddleware (next, logger )
121
+ return middleware.NewLoggerMiddleware (next, middleware. DefaultLogger () )
123
122
},
124
123
)
125
124
126
125
task := worker.Task {
127
126
ID: uuid.New (),
128
127
Priority: 1 ,
129
- Fn: func () interface {} { return " Hello, World from Task 1!" },
130
- }
131
-
132
- task2 := worker.Task {
133
- ID: uuid.New (),
134
- Priority: 5 ,
135
- // You can pass in parameters to the function and return a value using a closure
136
128
Fn: func () interface {} {
137
129
return func (a int , b int ) interface {} {
138
130
return a + b
139
131
}(2 , 5 )
140
132
},
141
133
}
142
134
135
+ // Invalid task, it doesn't have a function
136
+ task1 := worker.Task {
137
+ ID: uuid.New (),
138
+ Priority: 1 ,
139
+ }
140
+
141
+ task2 := worker.Task {
142
+ ID: uuid.New (),
143
+ Priority: 5 ,
144
+ Fn: func () interface {} { return " Hello, World from Task 2!" },
145
+ }
146
+
143
147
task3 := worker.Task {
144
148
ID: uuid.New (),
145
149
Priority: 90 ,
@@ -160,7 +164,7 @@ func main() {
160
164
},
161
165
}
162
166
163
- tm.RegisterTask (task, task2, task3, task4)
167
+ tm.RegisterTask (task, task1, task2, task3, task4)
164
168
tm.Start (5 )
165
169
166
170
tm.CancelTask (task3.ID )
@@ -175,7 +179,6 @@ func main() {
175
179
fmt.Println (task)
176
180
}
177
181
}
178
-
179
182
```
180
183
181
184
## Conclusion
0 commit comments