Skip to main content

Worker

Struct Worker 

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

A job-processing loop built on a SeppClient.

Configure it fluently — queues and lease duration via new, then with_* tuning and one handle call per job type — and start it with run. run consumes the worker and only returns after a ShutdownHandle is triggered and in-flight jobs have drained.

Each reserved job runs on its own task, bounded by with_max_in_flight. A job whose job_type has no registered handler is nacked for retry with an attempt-based backoff (min(2^attempt, 60) seconds), so a worker that does have the handler — e.g. one running the next deploy — can pick it up instead of this worker burning through the job’s attempts.

Implementations§

Source§

impl Worker

Source

pub fn new( client: SeppClient, queues: impl IntoIterator<Item = impl Into<String>>, lease_duration: Duration, ) -> Result<Self, WorkerBuilderError>

Creates a worker that reserves from queues with the given lease duration.

Sensible defaults are applied: up to 16 jobs in flight, a 1s backoff after a failed reserve, no lease auto-extension, and a generated worker_id derived from the hostname and PID. Register at least one handler with handle before run.

Source

pub fn with_wait_timeout(self, wait: Duration) -> Self

Sets the long-poll wait timeout for each reserve. See ReserveOptions::with_wait_timeout.

§Panics

Panics if wait is zero. The server would answer every reserve immediately, turning the poll loop into back-to-back RPCs.

Source

pub fn with_max_jobs(self, max: u32) -> Self

Caps how many jobs a single reserve may return. The worker already limits this to its free in-flight capacity, so set this only to request fewer.

§Panics

Panics if max is 0. The server requires max_jobs >= 1 and would reject every reserve, hanging the worker.

Source

pub fn shutdown_handle(&self) -> ShutdownHandle

Returns a ShutdownHandle for stopping the worker. Obtain it before calling run, which consumes self.

Source

pub fn with_auto_extend(self) -> Self

Enables automatic lease extension while a handler runs, using a heartbeat interval of one third of the lease duration. Use with caution: if the handler hangs indefinitely, the lease will be extended forever.

With this on, long-running handlers keep their lease alive without calling JobCtx::extend themselves. If the server reassigns the lease anyway, the handler task is aborted to avoid double processing.

Source

pub fn with_auto_extend_interval(self, interval: Duration) -> Self

Like with_auto_extend but with an explicit heartbeat interval (floored at 1ms). The interval should be comfortably shorter than the lease duration.

Source

pub fn with_max_in_flight(self, max_in_flight: usize) -> Self

Sets the maximum number of jobs processed concurrently (default 16). Values below 1 are treated as 1.

Source

pub fn with_reserve_error_backoff(self, backoff: Duration) -> Self

Sets how long to wait after a failed reserve before retrying (default 1s), preventing a hot loop when the server is unreachable.

Source

pub fn with_worker_id( self, worker_id: impl Into<String>, ) -> Result<Self, WorkerBuilderError>

Overrides the auto-generated worker id. Must be non-empty.

Source

pub fn handle<F, Fut>( self, job_type: &str, h: F, ) -> Result<Self, WorkerBuilderError>
where F: Fn(Option<Payload>, Arc<JobCtx>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<(), HandlerError>> + Send + 'static,

Registers the handler for a job_type.

The handler receives the job’s optional Payload and an Arc<JobCtx>, and returns Ok(()) to ack or a HandlerError to nack. Returns WorkerBuilderError::DuplicateHandler if a handler is already registered for this type.

Source

pub fn with_catch_all_handler<F, Fut>(self, h: F) -> Self
where F: Fn(Option<Payload>, Arc<JobCtx>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<(), HandlerError>> + Send + 'static,

Registers a catch-all handler for job types without a specific handler.

Source

pub fn replace_handler<F, Fut>(self, job_type: &str, h: F) -> Self
where F: Fn(Option<Payload>, Arc<JobCtx>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<(), HandlerError>> + Send + 'static,

Registers a handler, overwriting any existing one for the same job_type instead of erroring.

Source

pub fn remove_handler(self, job_type: &str) -> Self

Unregisters the handler for a job_type, if any. Jobs of an unhandled type are nacked for retry with an attempt-based backoff.

Source

pub async fn run(self)

Runs the reserve → process → ack/nack loop until shutdown.

Consumes the worker and does not return until a ShutdownHandle is triggered and all in-flight jobs have finished draining. Shutdown cancels a still-pending reserve promptly, but a reserve that has already completed with jobs at the shutdown boundary still has those jobs processed as part of the drain — they are leased to this worker either way. Reserve errors are logged and retried after with_reserve_error_backoff; they do not stop the loop. Take a shutdown_handle beforehand to be able to stop it.

The drain wait is unbounded: a handler that never returns blocks the return of run indefinitely (and with with_auto_extend its lease is kept alive the whole time). If a handler’s work can hang, bound it yourself, e.g. with tokio::time::timeout.

Auto Trait Implementations§

§

impl !Freeze for Worker

§

impl !RefUnwindSafe for Worker

§

impl !UnwindSafe for Worker

§

impl Send for Worker

§

impl Sync for Worker

§

impl Unpin for Worker

§

impl UnsafeUnpin for Worker

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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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