pub struct BackgroundJobServer { /* private fields */ }Expand description
Background job server that manages job processing
Implementations§
Source§impl BackgroundJobServer
impl BackgroundJobServer
Sourcepub fn new(
config: ServerConfig,
storage: Arc<dyn Storage>,
worker_registry: Arc<WorkerRegistry>,
) -> Self
pub fn new( config: ServerConfig, storage: Arc<dyn Storage>, worker_registry: Arc<WorkerRegistry>, ) -> Self
Create a new background job server
Sourcepub fn with_middleware(self, middleware: Vec<Arc<dyn JobMiddleware>>) -> Self
pub fn with_middleware(self, middleware: Vec<Arc<dyn JobMiddleware>>) -> Self
Replace the middleware stack that wraps worker.execute in every
worker thread. Runs in registration order — the first entry is the
outermost layer.
The default stack is [TracingMiddleware]; calling this replaces
it entirely. Re-add TracingMiddleware yourself if you still
want structured spans around every execution.
Must be called before BackgroundJobServer::start — changes made
after a running server has spawned its worker threads won’t affect
already-started processors.
Sourcepub fn with_state_change_hook(self, hook: StateChangeHook) -> Self
pub fn with_state_change_hook(self, hook: StateChangeHook) -> Self
Install a state-change hook fired after every persisted job state
transition driven by the processor. See StateChangeHook for
semantics — the hook runs synchronously inside process_job, so
keep it non-blocking.
The hook is cloned into every per-worker JobProcessor when
BackgroundJobServer::start spawns workers, so callers must
install it before start().
Sourcepub fn with_retry_policy(
config: ServerConfig,
storage: Arc<dyn Storage>,
worker_registry: Arc<WorkerRegistry>,
retry_policy: RetryPolicy,
) -> Self
pub fn with_retry_policy( config: ServerConfig, storage: Arc<dyn Storage>, worker_registry: Arc<WorkerRegistry>, retry_policy: RetryPolicy, ) -> Self
Create a new background job server with custom retry policy
Sourcepub async fn stop(&self) -> Result<()>
pub async fn stop(&self) -> Result<()>
Stop the background job server.
Cancels the shutdown token so every worker drops out of its polling
loop after finishing its current job, then waits up to
config.shutdown_timeout for all tasks to join. Any task still
running past the timeout is aborted — those jobs will need
stale-processing recovery on next startup.
Sourcepub async fn is_running(&self) -> bool
pub async fn is_running(&self) -> bool
Check if the server is running
Sourcepub fn config(&self) -> &ServerConfig
pub fn config(&self) -> &ServerConfig
Get server configuration
Sourcepub async fn schedule_recurring(
&self,
id: impl Into<String>,
cron: impl Into<String>,
method: impl Into<String>,
payload: Value,
queue: impl Into<String>,
) -> Result<()>
pub async fn schedule_recurring( &self, id: impl Into<String>, cron: impl Into<String>, method: impl Into<String>, payload: Value, queue: impl Into<String>, ) -> Result<()>
Register (or update) a recurring job template.
id uniquely identifies this template — calling again with the same
id replaces the previous definition. cron is a 6-field
cron expression (second minute hour day month day-of-week) parsed by
the cron crate. The template is stored via
[Storage::upsert_recurring_job] and the running
RecurringJobPoller will materialize it into a normal [Job] the
next time next_run_at is in the past.
Sourcepub async fn remove_recurring(&self, id: &str) -> Result<bool>
pub async fn remove_recurring(&self, id: &str) -> Result<bool>
Remove a recurring job template by id. Returns true if a row was
deleted, false if no template with that id existed.