Simple and powerful goroutine manager.
go get github.com/antonmashko/taskq
Usually, you need TaskQ
for managing your goroutines. This will allow you to control service resource consumption and graceful shutdown.
package main
import (
"context"
"fmt"
"log"
"github.com/antonmashko/taskq"
)
type Task struct{}
func (Task) Do(ctx context.Context) error {
fmt.Println("hello world")
return nil
}
func main() {
// Initializing new TaskQ instance with a limit of 10 max active goroutines
// Use `limit=0` for not limiting goroutines number.
tq := taskq.New(10)
// Starting reading and executing tasks from queue
err := tq.Start()
if err != nil {
log.Fatal(err)
}
// Enqueue new task for execution.
// TaskQ will run Do method of Task when it will have an available worker (goroutine)
_, err = tq.Enqueue(context.Background(), Task{})
if err != nil {
log.Fatal(err)
}
// Gracefully shutting down
err = tq.Close()
if err != nil {
log.Fatal(err)
}
}
More examples here
By default TaskQ stores all tasks in memory using ConcurrencyQueue. For creating custom queue you need to implement interface Queue and pass it as argument on creating NewWithQueue. See example of how to adapt redis queue into TaskQ
Task support two type of events:
- Done - completion of the task. https://pkg.go.dev/github.com/antonmashko/taskq#TaskDone
- OnError - error handling event. https://pkg.go.dev/github.com/antonmashko/taskq#TaskOnError For invoking event implement interface on your task (example).
Shutdown and Close gracefully shuts down the TaskQ without interrupting any active tasks. If TaskQ need to finish all tasks in queue, use context ContextWithWait as Shutdown
method argument.