Documentation
¶
Overview ¶
Package resque is a pure-Go (CGO=0) reimplementation of the queue and job model of the Ruby Resque background-job library, backed by Redis through the github.com/redis/go-redis/v9 client.
It writes and reads exactly the keys and JSON payloads that a real Resque (and a Ruby MRI worker) uses, so a job enqueued here can be reserved by a Ruby Resque worker and vice-versa:
- a job is the JSON object {"class":"<Name>","args":[...]} RPUSH'd to the list resque:queue:<name>, and the queue name is SADD'd to resque:queues;
- a worker registers under resque:workers, records the job it is running under resque:worker:<id>, tracks resque:stat:processed / resque:stat:failed, and writes the Resque failure hash to the resque:failed list.
The actual body of a job — Resque's Job#perform — is Ruby, so it is an injected seam (WithPerform); likewise the class→queue mapping that Ruby derives from a job class (WithQueueResolver) and the wall clock (WithClock). Everything else — the wire format, key layout and worker bookkeeping — is deterministic Go with no goroutines and no sleeps.
It is the Resque backend for go-embedded-ruby, a sibling of go-ruby-redis and go-ruby-set, but is a standalone, reusable module.
Index ¶
- Variables
- type Clock
- type Job
- type JobError
- type Option
- type PerformFunc
- type QueueResolver
- type Resque
- func (r *Resque) Dequeue(class string, args ...any) (int64, error)
- func (r *Resque) DestroyFrom(queue, class string, args ...any) (int64, error)
- func (r *Resque) Enqueue(class string, args ...any) error
- func (r *Resque) EnqueueTo(queue, class string, args ...any) error
- func (r *Resque) FailedCount() (int64, error)
- func (r *Resque) FailedStat() (int64, error)
- func (r *Resque) NewWorker(cfg WorkerConfig) *Worker
- func (r *Resque) Peek(queue string, start, count int64) ([]*Job, error)
- func (r *Resque) Pop(queue string) (*Job, error)
- func (r *Resque) Processed() (int64, error)
- func (r *Resque) Queues() ([]string, error)
- func (r *Resque) Reserve(queues ...string) (*Job, error)
- func (r *Resque) Size(queue string) (int64, error)
- func (r *Resque) Stat(name string) (int64, error)
- func (r *Resque) Workers() ([]string, error)
- type Worker
- type WorkerConfig
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoQueueResolver is returned by [Resque.Enqueue] / [Resque.Dequeue] // when no [WithQueueResolver] seam has been configured, mirroring the fact // that Ruby derives the queue from the job class. ErrNoQueueResolver = errors.New("resque: no queue resolver configured") // ErrNoPerform is returned by [Job.Perform] when no [WithPerform] seam has // been configured (the job body is Ruby). ErrNoPerform = errors.New("resque: no perform seam configured") )
Functions ¶
This section is empty.
Types ¶
type Clock ¶
Clock returns the current time; it is injectable so worker and failure bookkeeping is deterministic in tests.
type Job ¶
type Job struct {
// Queue is the queue the job was taken from.
Queue string
// Class is the Ruby job class name.
Class string
// Args are the decoded arguments (JSON numbers stay as json.Number).
Args []any
// contains filtered or unexported fields
}
Job is a reserved unit of work: the decoded {"class":...,"args":[...]} payload plus the queue it came from.
func (*Job) Fail ¶
Fail records a job failure in the resque:failed list using the Resque failure hash format. The worker may be nil (its id is recorded as the empty string).
func (*Job) Perform ¶
Perform runs the job body via the configured WithPerform seam. It returns ErrNoPerform if no seam is set, otherwise the error the body raised.
type JobError ¶
type JobError struct {
// Exception is the Ruby exception class name (e.g. "ArgumentError").
Exception string
// Message is the exception message (exception.to_s).
Message string
// Backtrace is the Ruby backtrace, innermost frame first.
Backtrace []string
}
JobError describes a Ruby exception raised by a job body. A PerformFunc may return one so that Job.Fail records the true Ruby exception class, message and backtrace; a plain error is recorded with the [defaultException] class.
type Option ¶
type Option func(*Resque)
Option configures a Resque in New.
func WithContext ¶
WithContext sets the context passed to every Redis command.
func WithNamespace ¶
WithNamespace overrides the Redis key prefix (default "resque").
func WithPerform ¶
func WithPerform(f PerformFunc) Option
WithPerform injects the job-body seam (Resque's Job#perform).
func WithQueueResolver ¶
func WithQueueResolver(q QueueResolver) Option
WithQueueResolver injects the class→queue mapping used by Resque.Enqueue.
type PerformFunc ¶
PerformFunc is the injectable seam for a job body (Resque's Job#perform). It receives the decoded class name and arguments and returns the error the Ruby body raised, if any.
type QueueResolver ¶
QueueResolver maps a job class to its queue, mirroring the Ruby idiom where a job class responds to `queue`. It is consulted by Resque.Enqueue and Resque.Dequeue.
type Resque ¶
type Resque struct {
// contains filtered or unexported fields
}
Resque is a handle onto a Redis-backed Resque instance. It is safe to share a single value; it holds no mutable state of its own.
func New ¶
New wraps a go-redis client in a Resque handle. The client is owned by the caller (including Close).
func (*Resque) Dequeue ¶
Dequeue removes matching jobs from the queue derived from class, mirroring Resque.dequeue(klass, *args). It returns the number of jobs removed.
func (*Resque) DestroyFrom ¶
DestroyFrom removes matching jobs from the named queue, mirroring Resque::Job.destroy(queue, klass, *args). With no args every job of the class is removed; with args only jobs whose payload equals the encoded class+args are removed. It returns the number of jobs removed.
func (*Resque) Enqueue ¶
Enqueue pushes a job onto the queue derived from class via the configured WithQueueResolver, mirroring Resque.enqueue(klass, *args).
func (*Resque) EnqueueTo ¶
EnqueueTo pushes a job onto the named queue, mirroring Resque.enqueue_to(queue, klass, *args). It SADDs the queue to resque:queues and RPUSHes the {"class":...,"args":[...]} payload to resque:queue:<queue>.
func (*Resque) FailedCount ¶
FailedCount returns the length of the resque:failed list (Resque::Failure.count).
func (*Resque) FailedStat ¶
FailedStat returns the failed-jobs counter (Resque.info[:failed]).
func (*Resque) NewWorker ¶
func (r *Resque) NewWorker(cfg WorkerConfig) *Worker
NewWorker builds a worker bound to this handle.
func (*Resque) Peek ¶
Peek returns up to count jobs starting at index start without removing them (Resque.peek).
func (*Resque) Pop ¶
Pop removes and returns the next job on a queue, or nil if it is empty (Resque.pop).
func (*Resque) Processed ¶
Processed returns the number of jobs processed (Resque.info[:processed]).
func (*Resque) Queues ¶
Queues returns the registered queue names, sorted for determinism (Resque.queues).
func (*Resque) Reserve ¶
Reserve LPOPs the next job from the first non-empty queue, decodes it, and returns it ready to perform. A nil job with a nil error means every queue was empty (Resque::Job.reserve over a worker's queue list).
type Worker ¶
type Worker struct {
Hostname string
PID int
Queues []string
// contains filtered or unexported fields
}
Worker models a Resque worker: it reserves jobs from an ordered list of queues, performs them and keeps the Resque registry and stats up to date. It runs synchronously — no goroutines, no sleeps — so its behaviour is fully deterministic.
func (*Worker) DoneWorking ¶
DoneWorking clears the busy marker and bumps processed counters (Resque::Worker#done_working + #processed!).
func (*Worker) Register ¶
Register adds the worker to resque:workers and records its start time (Resque::Worker#register_worker + #started!).
func (*Worker) Unregister ¶
Unregister removes the worker and its per-worker bookkeeping (Resque::Worker#unregister_worker).
func (*Worker) Work ¶
Work registers the worker, drains its queues one job at a time until they are empty (Resque's interval-zero work loop), then unregisters. It returns the number of jobs processed. Failed jobs still count as processed, matching Resque, where done_working runs in an ensure block.
type WorkerConfig ¶
WorkerConfig identifies a worker. Hostname and PID are seams (Ruby reads them from the process) so worker ids are deterministic in tests.
