Worker class

The background worker that processes jobs from one or more queues.

The Worker continuously polls the QueueDriver for available jobs, executes them with timeout protection, handles retries with exponential backoff, enforces rate limits, and tracks metrics.

final worker = Worker(
  driver: memoryDriver,
  concurrency: 4,
  maxJobsPerSecond: 10,
);
worker.onComplete = (job) => print('Done: ${job.name}');
worker.onFail = (job) => print('Failed permanently: ${job.name}');
worker.start();

Constructors

Worker({required QueueDriver driver, List<String> queues = const ['default'], Duration pollInterval = const Duration(milliseconds: 500), int concurrency = 1, int maxJobsPerSecond = 0, QueueMetrics? metrics})

Properties

activeJobs int
Returns the number of jobs currently being processed.
no setter
concurrency int
Maximum number of jobs to process concurrently.
final
driver QueueDriver
final
hashCode int
The hash code for this object.
no setterinherited
isRunning bool
Returns true if the worker is currently running.
no setter
maxJobsPerSecond int
Maximum number of jobs to start per second across all queues.
final
metrics QueueMetrics
Metrics collector for this worker.
final
onComplete JobEventCallback?
getter/setter pair
onFail JobEventCallback?
getter/setter pair
onProcess JobEventCallback?
Lifecycle callbacks
getter/setter pair
onRetry JobEventCallback?
getter/setter pair
onTimeout JobEventCallback?
getter/setter pair
pollInterval Duration
How often the worker polls for new jobs.
final
queues List<String>
The queues this worker listens to. Defaults to ['default'].
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
start() Future<void>
Starts the worker loop. The returned future completes when stop is called.
stop() Future<void>
Gracefully stops the worker. Waits for in-flight jobs to finish.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited