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}) - Creates a new Worker instance.
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
-
The queue storage backend.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- isRunning → bool
-
Returns
trueif 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?
-
Callback triggered when a job completes successfully.
getter/setter pair
- onFail ↔ JobEventCallback?
-
Callback triggered when a job permanently fails.
getter/setter pair
- onProcess ↔ JobEventCallback?
-
Callback triggered when a job begins processing.
getter/setter pair
- onRetry ↔ JobEventCallback?
-
Callback triggered when a job is scheduled for retry.
getter/setter pair
- onTimeout ↔ JobEventCallback?
-
Callback triggered when a job times out.
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