tasks

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package tasks provides Asynq background task helpers.

Index

Constants

View Source
const (
	TaskTypeEmailNotification = "email:notification"
	TaskTypeSyncData          = "sync:data"
	TaskTypeCleanup           = "maintenance:cleanup"
)

Common task type constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps an Asynq client for enqueuing tasks.

func NewClient

func NewClient(redisAddr, redisPassword string, redisDB int) *Client

NewClient creates a new task client.

func (*Client) Close

func (c *Client) Close() error

Close closes the task client.

func (*Client) Enqueue

func (c *Client) Enqueue(taskType string, payload interface{}, opts ...asynq.Option) (*asynq.TaskInfo, error)

Enqueue enqueues a task with the given type and payload.

func (*Client) EnqueueAt

func (c *Client) EnqueueAt(taskType string, payload interface{}, processAt time.Time, opts ...asynq.Option) (*asynq.TaskInfo, error)

EnqueueAt enqueues a task to be processed at the specified time.

func (*Client) EnqueueIn

func (c *Client) EnqueueIn(taskType string, payload interface{}, delay time.Duration, opts ...asynq.Option) (*asynq.TaskInfo, error)

EnqueueIn enqueues a task to be processed after the specified delay.

type EmailNotificationPayload

type EmailNotificationPayload struct {
	To      string `json:"to"`
	Subject string `json:"subject"`
	Body    string `json:"body"`
}

EmailNotificationPayload is the payload for email notification tasks.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server wraps an Asynq server for processing tasks.

func NewServer

func NewServer(cfg *ServerConfig) *Server

NewServer creates a new task server.

func (*Server) Handle

func (s *Server) Handle(taskType string, handler asynq.Handler)

Handle registers a handler for the given task type.

func (*Server) HandleFunc

func (s *Server) HandleFunc(taskType string, handler func(context.Context, *asynq.Task) error)

HandleFunc registers a handler function for the given task type.

func (*Server) Run

func (s *Server) Run() error

Run starts the server and blocks until shutdown.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown gracefully shuts down the server.

type ServerConfig

type ServerConfig struct {
	RedisAddr     string
	RedisPassword string
	RedisDB       int
	Concurrency   int
	Queues        map[string]int // Queue name -> priority
}

ServerConfig holds configuration for the task server.

func DefaultServerConfig

func DefaultServerConfig(redisAddr, redisPassword string, redisDB int) *ServerConfig

DefaultServerConfig returns a default server configuration.

type SyncDataPayload

type SyncDataPayload struct {
	Source      string `json:"source"`
	Destination string `json:"destination"`
}

SyncDataPayload is the payload for data sync tasks.

type TaskHandler

type TaskHandler[T any] struct {
	// contains filtered or unexported fields
}

TaskHandler is an interface for task handlers with automatic JSON unmarshaling.

func NewTaskHandler

func NewTaskHandler[T any](handler func(context.Context, T) error) *TaskHandler[T]

NewTaskHandler creates a new typed task handler.

func (*TaskHandler[T]) ProcessTask

func (h *TaskHandler[T]) ProcessTask(ctx context.Context, task *asynq.Task) error

ProcessTask implements asynq.Handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL