pub struct WorkerLoop { /* private fields */ }Expand description
DB-backed queue worker.
Runs a reaper→claim→spawn cycle until signalled to stop. Panic isolation (D-11) ensures a panicking job handler never kills the loop. Graceful shutdown (D-10) drains in-flight jobs and resets claimed-but-unstarted rows.
Implementations§
Source§impl WorkerLoop
impl WorkerLoop
Sourcepub fn new(config: WorkerConfig) -> Self
pub fn new(config: WorkerConfig) -> Self
Create a new worker loop.
A random UUID is generated as the worker ID. The worker reads the DB
connection from Queue::connection() — no connection argument needed.
Sourcepub fn with_tenant_scope(self, provider: Arc<dyn TenantScopeProvider>) -> Self
pub fn with_tenant_scope(self, provider: Arc<dyn TenantScopeProvider>) -> Self
Inject a tenant scope provider for tenant-aware job execution.
When set, jobs with a tenant_id are executed inside a tenant context
scope. Jobs without a tenant_id or workers without a provider run in
the default (system) scope.
Sourcepub fn register<J>(&mut self)where
J: Job + DeserializeOwned + 'static,
pub fn register<J>(&mut self)where
J: Job + DeserializeOwned + 'static,
Sourcepub fn from_registry(config: WorkerConfig) -> Self
pub fn from_registry(config: WorkerConfig) -> Self
Build a WorkerLoop and apply all job types registered via [Queue::register].
This is the entry point used by the framework’s server boot path to create the auto-started worker when at least one job type has been registered.
Sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Signal the worker loop to shut down gracefully.
Sets the same AtomicBool that the SIGTERM/Ctrl-C handler sets, so programmatic and signal-based shutdown share one path.
Sourcepub async fn run(&self) -> Result<(), Error>
pub async fn run(&self) -> Result<(), Error>
Run the worker loop until shutdown.
Loop behaviour:
- Check shutdown flag — if set, drain in-flight jobs and requeue.
- For each queue: run reaper, then attempt claim.
- If a job is claimed, spawn it and loop immediately (no idle sleep).
- If no jobs were found across all queues, sleep
config.sleep_duration.
Auto Trait Implementations§
impl !RefUnwindSafe for WorkerLoop
impl !UnwindSafe for WorkerLoop
impl Freeze for WorkerLoop
impl Send for WorkerLoop
impl Sync for WorkerLoop
impl Unpin for WorkerLoop
impl UnsafeUnpin for WorkerLoop
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more