Skip to main content

Worker

Struct Worker 

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

Configured Rust worker with typed activity handlers.

Implementations§

Source§

impl Worker

Source

pub fn builder(config: WorkerConfig) -> WorkerBuilder

Starts a new builder for the supplied config.

Source

pub fn activity_types(&self) -> &[String]

Returns the activity types this worker registers with the engine.

Source

pub fn available_handlers(&self) -> &BTreeSet<String>

Returns the handler-name set used for registration validation.

Source

pub async fn run(self) -> Result<(), WorkerError>

Connects to the configured endpoint, registers activities, and serves indefinitely.

Registration completes only when the server’s RegisterAck — the guaranteed first response frame — arrives; the worker serves nothing before it. Session establishment goes through the bounded-backoff reconnect machinery configured in WorkerConfig::reconnect, and retryable mid-run transport drops — including clean server-side stream closes — re-establish through the same machinery: the worker re-registers its activity types, re-reports every unacknowledged activity result (cleared only by the server’s per-result ResultAck frames), and resumes serving. A server-announced drain reconnects after the schedule’s initial backoff without consuming drop budget. Deterministic PermissionDenied / Unauthenticated denials surface after exactly one attempt. Without a shutdown signal the run ends only on a non-retryable error or drop-budget exhaustion; see crate::config::ReconnectConfig for the budget-reset semantics.

§Errors

Returns WorkerError for connection, registration, dispatch, heartbeat, or report failures.

Source

pub async fn run_until<Shutdown>( self, shutdown: Shutdown, ) -> Result<(), WorkerError>
where Shutdown: Future<Output = ()> + Send,

Connects to the configured endpoint, registers activities, and serves until shutdown fires.

Establishment and mid-run reconnect behaviour match Worker::run. On shutdown, no new tasks are pulled, in-flight activity contexts are marked cancelled, and all in-flight activities are drained before this returns; shutdown signalled during a reconnect or backoff wins promptly without waiting out the backoff delay.

§Errors

Returns WorkerError for connection, registration, dispatch, heartbeat, or report failures.

Source

pub async fn run_with_connector_until<S, F, Fut, Shutdown>( self, connect: F, shutdown: Shutdown, ) -> Result<(), WorkerError>
where S: WorkerSession, F: FnMut() -> Fut, Fut: Future<Output = Result<S, WorkerError>>, Shutdown: Future<Output = ()> + Send,

Runs the reconnect-aware serve loop over an injected session factory.

Session establishment goes through reconnect_with_backoff: transient failures retry up to the configured reconnect.max_attempts with bounded exponential backoff, while PermissionDenied / Unauthenticated denials surface after exactly one attempt. When an established session drops retryably mid-run — a retryable transport failure or an unannounced clean server-side stream close, both count — the worker drains in-flight activities into the unacked tracker, backs off, reconnects through the same machinery (re-registering its activity types), re-reports every still-unacknowledged result (the shutdown signal can interrupt that replay; tracked results survive), and resumes serving. Server ResultAck frames clear tracker entries mid-session, so the steady-state replay backlog is empty.

Mid-run drops share one cumulative budget of reconnect.max_attempts, matching the Python and TypeScript workers, and the budget resets to zero once a session proves healthy: it served at least one task, or it stayed connected longer than reconnect.max_backoff (measured monotonically from successful registration to the moment the stream ended or dropped — post-drop draining of in-flight activities never extends it). A server-announced drain is unbudgeted: the worker finishes in-flight work and redials after reconnect.initial_backoff; the drain classification latches for the session, so even an abrupt end after the drain frame stays drain-class. See crate::config::ReconnectConfig. The run therefore ends only on shutdown, a non-retryable error, or budget exhaustion — never merely because the server closed or drained the stream. At most one session is alive at a time, and a shutdown signalled during a reconnect or backoff wins promptly (returning Ok when the pending drop was a drain or clean close, and the pending error when it was a failure).

§Errors

Returns WorkerError when establishment attempts are exhausted or denied, when a non-retryable error occurs mid-run, when the mid-run drop budget is exhausted (WorkerError::CleanCloseExhausted when the exhausting drops were clean closes), or when shutdown interrupts an unrecovered error drop.

Source

pub async fn run_with_session<S>(self, session: S) -> Result<S, WorkerError>
where S: WorkerSession,

Test seam that handshakes, registers, and serves an injected session until its stream ends.

§Errors

Returns WorkerError for registration, dispatch, heartbeat, or report failures.

Source

pub async fn run_with_session_until<S, Shutdown>( self, session: S, shutdown: Shutdown, ) -> Result<S, WorkerError>
where S: WorkerSession, Shutdown: Future<Output = ()> + Send,

Test seam that handshakes, registers, and serves an injected session until shutdown fires.

§Errors

Returns WorkerError for registration, dispatch, heartbeat, or report failures.

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> 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<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