Skip to main content

BackgroundJobServer

Struct BackgroundJobServer 

Source
pub struct BackgroundJobServer { /* private fields */ }
Expand description

Background job server that manages job processing

Implementations§

Source§

impl BackgroundJobServer

Source

pub fn new( config: ServerConfig, storage: Arc<dyn Storage>, worker_registry: Arc<WorkerRegistry>, ) -> Self

Create a new background job server

Source

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.

Source

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().

Source

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

Source

pub async fn start(&self) -> Result<()>

Start the background job server

Source

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.

Source

pub async fn is_running(&self) -> bool

Check if the server is running

Source

pub fn config(&self) -> &ServerConfig

Get server configuration

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more