workqueue

package
v0.4.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package workqueue provides a task queue with single-consumer semantics. Each task is processed by exactly one worker.

Index

Constants

View Source
const (
	// PluginName identifies this plugin.
	PluginName = "workqueue"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(context.Context, *Task) error

Handler processes tasks from the work queue.

type Shutdownable

type Shutdownable interface {
	Shutdown(ctx context.Context) error
}

Shutdownable is implemented by WorkQueue implementations that need graceful shutdown.

type Task

type Task struct {
	ID      string // Unique identifier
	Queue   string // Queue name
	Data    any    // Payload
	Attempt int    // Processing attempt (1-based)
	// contains filtered or unexported fields
}

Task wraps task data with metadata.

func NewTask

func NewTask(id, queue string, data any) *Task

NewTask creates a task with default no-op ack/nack functions.

func NewTaskWithCallbacks

func NewTaskWithCallbacks(id, queue string, data any, attempt int, ack, nack func()) *Task

NewTaskWithCallbacks creates a task with custom ack/nack callbacks.

func (*Task) Ack

func (t *Task) Ack()

Ack acknowledges successful processing of the task.

func (*Task) Nack

func (t *Task) Nack()

Nack indicates the task failed to process and should be redelivered.

type WorkQueue

type WorkQueue interface {
	// Subscribe registers a handler that competes with other handlers.
	// Only one handler will process each task.
	Subscribe(queue string, handler Handler)

	// Enqueue adds a task to the queue for single-consumer processing.
	Enqueue(queue string, data any)

	// Wait blocks until locally-initiated operations complete. For in-memory
	// implementations, this means all handlers have finished. For distributed
	// implementations, this means tasks have been sent to the remote system.
	Wait(ctx context.Context) error
}

WorkQueue provides task queue with single-consumer semantics. Each enqueued task is processed by exactly one worker.

func FromContext

func FromContext(ctx context.Context) WorkQueue

FromContext retrieves the work queue from a context.

type WorkQueuePlugin

type WorkQueuePlugin struct {
	WorkQueue
}

WorkQueuePlugin provides access to a work queue for plugins and components.

func Plugin

func Plugin(wq WorkQueue) *WorkQueuePlugin

Plugin registers a workqueue with a Prefab server for use by other plugins. The queue can be retrieved from the request context using FromContext.

func (*WorkQueuePlugin) Name

func (p *WorkQueuePlugin) Name() string

From prefab.Plugin.

func (*WorkQueuePlugin) ServerOptions

func (p *WorkQueuePlugin) ServerOptions() []prefab.ServerOption

From prefab.OptionProvider.

func (*WorkQueuePlugin) Shutdown

func (p *WorkQueuePlugin) Shutdown(ctx context.Context) error

From prefab.ShutdownPlugin.

Directories

Path Synopsis
Package memqueue provides an in-memory implementation of workqueue.WorkQueue.
Package memqueue provides an in-memory implementation of workqueue.WorkQueue.

Jump to

Keyboard shortcuts

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