Skip to content

Commit 65e9cde

Browse files
author
Francesco Cosentino
committed
updated readme
1 parent bcce5d7 commit 65e9cde

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Each `Task` represents a function scheduled by priority.
99

1010
- Task prioritization: You can register tasks with a priority level influencing the execution order.
1111
- 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.
1214
- Rate limiting: You can rate limit the tasks schedule by setting a maximum number of jobs per second.
1315
- Cancellation: Tasks can be canceled before or while they are running.
1416

@@ -102,44 +104,46 @@ package main
102104

103105
import (
104106
"fmt"
105-
"log"
106107
"time"
107108

109+
"github.com/google/uuid"
108110
worker "github.com/hyp3rd/go-worker"
109111
"github.com/hyp3rd/go-worker/middleware"
110-
"github.com/google/uuid"
111112
)
112113

113-
func main() {
114+
func main() {
114115
tm := worker.NewTaskManager(5, 10)
115-
// Example of using zap logger from uber
116-
logger := log.Default()
117116

118117
// apply middleware in the same order as you want to execute them
119118
tm = worker.RegisterMiddleware(tm,
120-
//middleware.YourMiddleware,
119+
// middleware.YourMiddleware,
121120
func(next worker.Service) worker.Service {
122-
return middleware.NewLoggerMiddleware(next, logger)
121+
return middleware.NewLoggerMiddleware(next, middleware.DefaultLogger())
123122
},
124123
)
125124

126125
task := worker.Task{
127126
ID: uuid.New(),
128127
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
136128
Fn: func() interface{} {
137129
return func(a int, b int) interface{} {
138130
return a + b
139131
}(2, 5)
140132
},
141133
}
142134

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+
143147
task3 := worker.Task{
144148
ID: uuid.New(),
145149
Priority: 90,
@@ -160,7 +164,7 @@ func main() {
160164
},
161165
}
162166

163-
tm.RegisterTask(task, task2, task3, task4)
167+
tm.RegisterTask(task, task1, task2, task3, task4)
164168
tm.Start(5)
165169

166170
tm.CancelTask(task3.ID)
@@ -175,7 +179,6 @@ func main() {
175179
fmt.Println(task)
176180
}
177181
}
178-
179182
```
180183

181184
## Conclusion

0 commit comments

Comments
 (0)