queue

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlushFailedJobs

func FlushFailedJobs()

FlushFailedJobs clears all failed jobs.

func RecordFailedJob

func RecordFailedJob(job Job, err error)

RecordFailedJob stores a failed job.

func RetryFailedJob

func RetryFailedJob(id int64) error

RetryFailedJob retries a failed job by ID.

Types

type BatchBuilder

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

BatchBuilder configures a batch of jobs.

func (*BatchBuilder) Catch

func (bb *BatchBuilder) Catch(fn func(*JobBatch, error)) *BatchBuilder

Catch sets the callback for when a job fails.

func (*BatchBuilder) Dispatch

func (bb *BatchBuilder) Dispatch() (*JobBatch, error)

Dispatch dispatches all jobs in the batch.

func (*BatchBuilder) Then

func (bb *BatchBuilder) Then(fn func(*JobBatch)) *BatchBuilder

Then sets the callback for when all jobs complete.

type Bus

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

Bus dispatches jobs and manages batches.

func NewBus

func NewBus(manager *Manager) *Bus

NewBus creates a new job bus.

func (*Bus) Batch

func (b *Bus) Batch(jobs []Job) *BatchBuilder

Batch creates a batch of jobs and dispatches them.

func (*Bus) Chain

func (b *Bus) Chain(jobs []Job) *ChainBuilder

Chain dispatches jobs sequentially.

func (*Bus) Delay

func (b *Bus) Delay(job Job, delay time.Duration) error

Delay dispatches a job with a delay.

func (*Bus) Dispatch

func (b *Bus) Dispatch(job Job) error

Dispatch pushes a job to the queue.

type ChainBuilder

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

ChainBuilder configures sequential job execution.

func (*ChainBuilder) Catch

func (cb *ChainBuilder) Catch(fn func(error)) *ChainBuilder

Catch sets the callback for when a job fails.

func (*ChainBuilder) Dispatch

func (cb *ChainBuilder) Dispatch() error

Dispatch dispatches jobs sequentially.

func (*ChainBuilder) Then

func (cb *ChainBuilder) Then(fn func()) *ChainBuilder

Then sets the callback for when all jobs complete.

type ChainableJob

type ChainableJob interface {
	// Chain returns the middleware chain for this job type.
	Chain() []JobMiddleware
}

ChainableJob allows a job to define middleware via a static method.

type DatabaseDriver

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

DatabaseDriver is a queue driver backed by a SQL database.

func NewDatabaseDriver

func NewDatabaseDriver(db *orm.DB, defaultQueueName string) *DatabaseDriver

NewDatabaseDriver creates a new Database queue driver.

func (*DatabaseDriver) Pop

func (d *DatabaseDriver) Pop() (Job, error)

Pop retrieves and reserves a job from the jobs table using a transaction to prevent race conditions between concurrent workers.

func (*DatabaseDriver) Push

func (d *DatabaseDriver) Push(job Job) error

Push pushes a job into the jobs table.

type Driver

type Driver interface {
	Push(job Job) error
	Pop() (Job, error)
}

Driver interface abstracts queue interactions.

type FailedJob

type FailedJob struct {
	ID        int64
	Queue     string
	Payload   string
	Exception string
	FailedAt  time.Time
}

FailedJob represents a failed job record.

func GetFailedJobs

func GetFailedJobs() []FailedJob

GetFailedJobs returns all failed jobs.

type FailedJobStore

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

FailedJobStore manages failed jobs.

type Fake

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

Fake is a fake queue dispatcher for testing.

func NewFake

func NewFake() *Fake

NewFake creates a new fake queue dispatcher.

func (*Fake) AssertNotPushed

func (f *Fake) AssertNotPushed() bool

AssertNotPushed asserts that no jobs were pushed.

func (*Fake) AssertNotPushedJob

func (f *Fake) AssertNotPushedJob(jobType any) bool

AssertNotPushedJob asserts that a specific job type was NOT pushed.

func (*Fake) AssertPushed

func (f *Fake) AssertPushed() bool

AssertPushed asserts that at least one job was pushed.

func (*Fake) AssertPushedCount

func (f *Fake) AssertPushedCount(count int) bool

AssertPushedCount asserts the exact number of jobs pushed.

func (*Fake) AssertPushedJob

func (f *Fake) AssertPushedJob(jobType any) bool

AssertPushedJob asserts that a specific job type was pushed.

func (*Fake) Clear

func (f *Fake) Clear()

Clear resets all captured jobs.

func (*Fake) GetJobCount

func (f *Fake) GetJobCount() int

GetJobCount returns the number of jobs queued.

func (*Fake) GetJobs

func (f *Fake) GetJobs() []Job

GetJobs returns all captured jobs.

func (*Fake) GetLastJob

func (f *Fake) GetLastJob() Job

GetLastJob returns the last captured job, or nil if none.

func (*Fake) HasJob

func (f *Fake) HasJob(jobType any) bool

HasJob checks if a specific job type was queued.

func (*Fake) Push

func (f *Fake) Push(job Job) error

Push captures the job instead of dispatching it.

func (*Fake) Size

func (f *Fake) Size() int

Size returns the number of jobs in the fake queue.

func (*Fake) Sync

func (f *Fake) Sync() error

Sync runs all captured jobs synchronously (useful in tests).

func (*Fake) SyncJobs

func (f *Fake) SyncJobs(jobType any) error

SyncJobs runs jobs of a specific type synchronously.

type HasBackoff

type HasBackoff interface {
	Backoff() time.Duration
}

HasBackoff allows a job to specify its own backoff strategy.

type HasMaxAttempts

type HasMaxAttempts interface {
	MaxAttempts() int
}

HasMaxAttempts allows a job to specify a hard limit on total attempts.

type HasMaxRetries

type HasMaxRetries interface {
	MaxRetries() int
}

HasMaxRetries allows a job to specify its own max retry count.

type HasPriority

type HasPriority interface {
	Priority() int
}

HasPriority allows a job to specify its priority (0 = highest, 10 = lowest). Jobs with lower priority numbers are processed first.

type HasTimeout

type HasTimeout interface {
	Timeout() time.Duration
}

HasTimeout allows a job to specify its own timeout.

type InMemoryUniqueStore

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

InMemoryUniqueStore is an in-memory implementation of UniqueJobStore.

func NewInMemoryUniqueStore

func NewInMemoryUniqueStore() *InMemoryUniqueStore

func (*InMemoryUniqueStore) Acquire

func (s *InMemoryUniqueStore) Acquire(key string) bool

func (*InMemoryUniqueStore) Release

func (s *InMemoryUniqueStore) Release(key string)

type Job

type Job interface {
	// Handle executes the job logic.
	Handle() error
	// Failed handles job failure after all retries are exhausted.
	Failed(err error)
}

Job defines the interface for an executable queue job.

type JobBatch

type JobBatch struct {
	ID        int64
	Jobs      []Job
	Pending   int
	Completed int
	Failed    int
	Then      func(batch *JobBatch)            // callback when all jobs complete
	Catch     func(batch *JobBatch, err error) // callback on failure
}

JobBatch represents a batch of jobs.

type JobMiddleware

type JobMiddleware interface {
	Handle(job Job, next func(Job) error) error
}

JobMiddleware wraps job execution with custom logic.

func EnsureUnique

func EnsureUnique(store UniqueJobStore) JobMiddleware

EnsureUnique ensures only one instance of a job with the same UniqueID runs at a time.

func LoggingMiddleware

func LoggingMiddleware() JobMiddleware

LoggingMiddleware logs job execution.

func RateLimitByKeyMiddleware

func RateLimitByKeyMiddleware(keyFunc func(Job) string, maxJobs int, per time.Duration) JobMiddleware

RateLimitByKeyMiddleware limits jobs by a key extracted from the job. Useful for per-user or per-tenant rate limiting.

func RateLimitMiddleware

func RateLimitMiddleware(maxJobs int, per time.Duration) JobMiddleware

RateLimitMiddleware limits job execution to N jobs per time period. Uses a token bucket algorithm.

func RetryMiddleware

func RetryMiddleware(maxRetries int) JobMiddleware

RetryMiddleware adds retry logic with exponential backoff.

func TimeoutMiddleware

func TimeoutMiddleware(timeout time.Duration) JobMiddleware

TimeoutMiddleware adds a timeout to job execution.

func WithoutRetry

func WithoutRetry() JobMiddleware

WithoutRetry disables retry logic for a job.

type JobWithMiddleware

type JobWithMiddleware interface {
	Middleware() []JobMiddleware
}

JobWithMiddleware allows a job to declare its own middleware.

type Manager

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

Manager resolves queue drivers.

func NewManager

func NewManager(defaultQueue string) *Manager

NewManager initializes a new Queue Manager.

func (*Manager) AddDriver

func (m *Manager) AddDriver(name string, driver Driver)

AddDriver registers a new queue driver.

func (*Manager) Connection

func (m *Manager) Connection(name string) Driver

Connection gets a driver by name.

func (*Manager) Push

func (m *Manager) Push(job Job) error

Push pushes a job to the default connection.

type MemoryDriver

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

MemoryDriver is a goroutine-safe queue driver backed by a buffered channel.

func NewMemoryDriver

func NewMemoryDriver(capacity int) *MemoryDriver

NewMemoryDriver initializes a new MemoryDriver with a specified buffer capacity.

func (*MemoryDriver) Len

func (d *MemoryDriver) Len() int

Len returns the current number of jobs waiting in the queue.

func (*MemoryDriver) Pop

func (d *MemoryDriver) Pop() (Job, error)

Pop blocks until a job is available and returns it.

func (*MemoryDriver) Push

func (d *MemoryDriver) Push(job Job) error

Push adds a new job to the channel. Returns an error if the queue is full after 5 seconds.

func (*MemoryDriver) TryPop

func (d *MemoryDriver) TryPop() Job

TryPop attempts to read a job from the channel without blocking. Returns nil if the queue is empty.

type MiddlewareFunc

type MiddlewareFunc func(job Job, next func(Job) error) error

MiddlewareFunc is a function adapter for JobMiddleware.

func (MiddlewareFunc) Handle

func (m MiddlewareFunc) Handle(job Job, next func(Job) error) error

type RabbitMQDriver

type RabbitMQDriver struct {
	ConnectionString string
	QueueName        string
	Exchange         string
	// contains filtered or unexported fields
}

RabbitMQDriver implements the Driver interface for RabbitMQ

func NewRabbitMQDriver

func NewRabbitMQDriver(connectionString, queueName, exchange string) *RabbitMQDriver

NewRabbitMQDriver creates a new RabbitMQ driver

func (*RabbitMQDriver) Pop

func (d *RabbitMQDriver) Pop() (Job, error)

Pop pops a job from RabbitMQ

func (*RabbitMQDriver) Push

func (d *RabbitMQDriver) Push(job Job) error

Push pushes a job to RabbitMQ

type RedisDriver

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

RedisDriver is a queue driver backed by Redis.

func NewRedisDriver

func NewRedisDriver(client *redis.Client, defaultQueueName string) *RedisDriver

NewRedisDriver creates a new Redis queue driver.

func (*RedisDriver) Pop

func (d *RedisDriver) Pop() (Job, error)

Pop pops a job off the Redis queue list.

func (*RedisDriver) Push

func (d *RedisDriver) Push(job Job) error

Push pushes a job onto the Redis queue list.

type SQSDriver

type SQSDriver struct {
	QueueURL  string
	Region    string
	AccessKey string
	SecretKey string
	Endpoint  string // Optional custom endpoint (e.g., for LocalStack)
	// contains filtered or unexported fields
}

SQSDriver implements the Driver interface for Amazon SQS

func NewSQSDriver

func NewSQSDriver(queueURL, region, accessKey, secretKey string) *SQSDriver

NewSQSDriver creates a new SQS driver

func (*SQSDriver) Pop

func (d *SQSDriver) Pop() (Job, error)

Pop pops a job from SQS

func (*SQSDriver) Push

func (d *SQSDriver) Push(job Job) error

Push pushes a job to SQS

type ShouldBeUnique

type ShouldBeUnique interface {
	UniqueID() string
}

ShouldBeUnique interface for jobs that must be unique in the queue. If a job with the same UniqueID is already pending, it will not be dispatched.

type ShouldBeUniqueUntilProcessing

type ShouldBeUniqueUntilProcessing interface {
	ShouldBeUnique
	UniqueVia() string // optional: custom lock key prefix
}

ShouldBeUniqueUntilProcessing is like ShouldBeUnique but releases the lock once the job begins processing (not after completion).

type SyncDriver

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

SyncDriver is an in-memory channel-based queue driver for local development.

func NewSyncDriver

func NewSyncDriver(bufferSize int) *SyncDriver

NewSyncDriver creates a new SyncDriver with the given buffer size.

func (*SyncDriver) Channel

func (d *SyncDriver) Channel() <-chan Job

Channel returns the underlying Go channel.

func (*SyncDriver) Pop

func (d *SyncDriver) Pop() (Job, error)

Pop retrieves the next job from the channel (blocking). In a real channel based worker, the worker just ranges over the channel.

func (*SyncDriver) Push

func (d *SyncDriver) Push(job Job) error

Push adds a job to the channel.

type UniqueJobStore

type UniqueJobStore interface {
	Acquire(key string) bool
	Release(key string)
}

UniqueJobStore is an interface for storing job uniqueness locks.

type Worker

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

Worker processes jobs from a queue connection.

func NewWorker

func NewWorker(manager *Manager) *Worker

NewWorker initializes a new Worker.

func (*Worker) Work

func (w *Worker) Work(ctx context.Context, connectionName string)

Work starts a worker daemon for the given connection. It runs until the context is cancelled.

Jump to

Keyboard shortcuts

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