Skip to content

antonmashko/taskq

Repository files navigation

TaskQ

Go Report GoDoc Build Status codecov

Simple and powerful goroutine manager.


Installing

go get github.com/antonmashko/taskq

Purpose

Usually, you need TaskQ for managing your goroutines. This will allow you to control service resource consumption and graceful shutdown.

Example

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

Persistence and Queues

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 Events

Task support two type of events:

  1. Done - completion of the task. https://pkg.go.dev/github.com/antonmashko/taskq#TaskDone
  2. OnError - error handling event. https://pkg.go.dev/github.com/antonmashko/taskq#TaskOnError For invoking event implement interface on your task (example).

Graceful shutdown

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.

Benchmark results

Benchmarks