Documentation
¶
Overview ¶
Package worker provides job processing runtime for Spooled queues.
Index ¶
- type Event
- type EventHandler
- type EventType
- type JobClaimedData
- type JobCompletedData
- type JobContext
- type JobFailedData
- type JobHandler
- type JobLeaseLostData
- type JobProgressDatadeprecated
- type JobStartedData
- type JobTimeoutData
- type Options
- type State
- type Worker
- type WorkerErrorData
- type WorkerStartedData
- type WorkerStoppedData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EventHandler ¶
type EventHandler func(event Event)
EventHandler is a callback for worker events.
type EventType ¶
type EventType string
Event types for worker events.
const ( EventWorkerStarted EventType = "worker:started" EventWorkerStopped EventType = "worker:stopped" EventWorkerError EventType = "worker:error" EventJobClaimed EventType = "job:claimed" EventJobStarted EventType = "job:started" EventJobCompleted EventType = "job:completed" EventJobFailed EventType = "job:failed" // EventJobTimeout is emitted when a job runs longer than its timeout_seconds. // The handler's context is canceled and the job is failed with "Job timeout // exceeded"; EventJobFailed follows once the API accepts the failure. EventJobTimeout EventType = "job:timeout" // EventJobProgress is never emitted: the API has no job progress route. // // Deprecated: job progress is not supported. EventJobProgress EventType = "job:progress" EventJobHeartbeat EventType = "job:heartbeat" EventJobLeaseLost EventType = "job:lease_lost" EventWorkerHeartbeat EventType = "worker:heartbeat" )
type JobClaimedData ¶
JobClaimedData is emitted when a job is claimed.
type JobCompletedData ¶
type JobCompletedData struct {
JobID string
QueueName string
Result map[string]any
Duration time.Duration
}
JobCompletedData is emitted when a job completes.
type JobContext ¶
type JobContext struct {
// Context is the Go context with cancellation
Context context.Context
// JobID is the unique job identifier
JobID string
// QueueName is the queue this job belongs to
QueueName string
// Payload is the job payload when it is a JSON object, and nil for any other
// JSON value (array, string, number, boolean, null). Use RawPayload to read
// those.
Payload map[string]any
// RawPayload is the decoded job payload whatever its JSON type: a
// map[string]any for an object, a []any for an array, a string, a float64, a
// bool, or nil. Integers too large for a float64 (above 2^53) arrive as
// json.Number so IDs are not rounded; every other number is a float64.
RawPayload any
// RetryCount is the current retry attempt number
RetryCount int
// MaxRetries is the maximum number of retries
MaxRetries int
// Progress is not supported: the Spooled API has no job progress route. It
// returns an *resources.UnsupportedError (it matches resources.ErrUnsupported
// with errors.Is) and makes no request. Returning that error from a handler
// fails the job, so remove the call. Return progress in the job result or
// report it with Log.
//
// Deprecated: the API cannot store job progress.
Progress func(percent float64, message string) error
// Log logs a message at the specified level
Log func(level string, message string, meta map[string]any)
// contains filtered or unexported fields
}
JobContext provides context and utilities for job handlers.
type JobFailedData ¶
type JobFailedData struct {
JobID string
QueueName string
Error error
Duration time.Duration
WillRetry bool
}
JobFailedData is emitted when a job fails.
type JobHandler ¶
type JobHandler func(ctx *JobContext) (map[string]any, error)
JobHandler is a function that processes a job. Return an error to fail the job, or nil/result to complete it.
type JobLeaseLostData ¶ added in v1.0.20
type JobLeaseLostData struct {
JobID string
QueueName string
LeaseID string
Operation string
Error error
}
JobLeaseLostData is emitted when the backend rejects an operation because this execution no longer owns the job lease.
type JobProgressData
deprecated
type JobStartedData ¶
JobStartedData is emitted when job processing starts.
type JobTimeoutData ¶ added in v1.5.0
type JobTimeoutData struct {
JobID string
QueueName string
TimeoutSeconds int
Duration time.Duration
}
JobTimeoutData is emitted when a job exceeds its timeout_seconds.
type Options ¶
type Options struct {
// QueueName is the name of the queue to process
QueueName string
// WorkerID is an optional stable worker identity (1-128 chars from
// [A-Za-z0-9._-]). Keep it the same across restarts and registration
// upserts a single worker row, so a restarting process reclaims its
// identity. Leave it empty and the server mints a fresh UUID on every
// Start, which leaves the previous row occupying the plan worker cap
// until the stale-worker reaper clears it (roughly two minutes).
WorkerID string
// Hostname is the worker hostname (default: auto-detected)
Hostname string
// WorkerType is an identifier for this worker type
WorkerType string
// Concurrency is the maximum concurrent jobs (1-100, default: 5)
Concurrency int
// PollInterval is the polling interval (default: 1s)
PollInterval time.Duration
// LeaseDuration is the job lease duration in seconds (5-3600, default: 30)
LeaseDuration int
// HeartbeatFraction is the heartbeat interval as a fraction of lease duration (default: 0.5)
HeartbeatFraction float64
// ShutdownTimeout is how long Stop (and cancelling the context given to
// Start) lets running handlers finish before they are cancelled and their
// jobs failed with "Worker shutdown timeout" (default: 30s). Handlers that
// finish inside the window are completed or failed normally.
ShutdownTimeout time.Duration
// Version is the worker version string
Version string
// Metadata is additional worker metadata
Metadata map[string]string
// Debug enables debug logging
Debug bool
// Logger is a custom logger function
Logger func(msg string, args ...any)
}
Options configures a worker.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns options with sensible defaults.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker processes jobs from a Spooled queue using REST polling.
func NewWorker ¶
func NewWorker(jobs *resources.JobsResource, workers *resources.WorkersResource, opts Options) *Worker
NewWorker creates a new REST polling worker.
func (*Worker) ActiveJobCount ¶
ActiveJobCount returns the number of jobs currently being processed.
func (*Worker) OnEvent ¶
func (w *Worker) OnEvent(handler EventHandler)
OnEvent registers an event handler.
func (*Worker) Process ¶
func (w *Worker) Process(handler JobHandler)
Process registers a job handler.
func (*Worker) Start ¶
Start registers the worker and starts claiming jobs. It returns once the worker is running.
Cancelling ctx shuts the worker down exactly like Stop: it stops claiming, lets running handlers finish for up to Options.ShutdownTimeout, acknowledges them, and deregisters. Handlers are not canceled while they drain, so wait for that to finish with Stop (which blocks until it has):
cancel() // e.g. from a SIGTERM handler w.Stop() // returns after the drain
If registration fails, Start returns the error and may be called again.
func (*Worker) Stop ¶
Stop gracefully stops the worker and blocks until it has finished.
It stops claiming new jobs, then waits up to Options.ShutdownTimeout for running handlers to finish. A handler that returns in that window has its result sent (complete, or fail if it returned an error) before the worker deregisters, whatever the reason Stop was called. Only handlers still running when the timeout expires are canceled (their JobContext.Context is done) and their jobs failed with "Worker shutdown timeout". The worker is deregistered last. Stop is safe to call more than once and from several goroutines.
type WorkerErrorData ¶
type WorkerErrorData struct {
Error error
}
WorkerErrorData is emitted on worker errors.
type WorkerStartedData ¶
WorkerStartedData is emitted when the worker starts.
type WorkerStoppedData ¶
WorkerStoppedData is emitted when the worker stops.