Skip to main content

Worker

Struct Worker 

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

Runs registered Flow Step and RPC handlers through outbound task streams.

A Worker owns an internal Tokio runtime and is one-shot. Self::start negotiates the Server protocol, synchronizes Attribute indexes, then blocks while serving. Call Self::stop from another thread to request graceful shutdown. The Worker runs at most the configured number of handlers concurrently and needs only an outbound connection to Dex. See crate::PullWorkerOptions.

§Examples

use dex_sdk::{BlobCache, BlobCacheConfig, Registry, Worker, WorkerOptions};
use std::sync::Arc;

let cache = Arc::new(BlobCache::open(BlobCacheConfig::new(
    "/tmp/dex-blobs", 64 * 1024 * 1024, 0,
)?)?);
let worker = Worker::try_new(Registry::new(), cache, WorkerOptions::new())?;
worker.start()?;

Implementations§

Source§

impl Worker

Source

pub fn new( registry: Registry, blob_cache: Arc<BlobCache>, options: WorkerOptions, ) -> Self

Creates a Worker or panics when options or runtime initialization are invalid.

§Panics

Panics for invalid pull options, zero index-sync timeout, invalid Dex endpoint, or Tokio runtime creation failure. Prefer Self::try_new for recoverable startup.

Source

pub fn try_new( registry: Registry, blob_cache: Arc<BlobCache>, options: WorkerOptions, ) -> SdkResult<Self>

Creates a one-shot pull Worker with a lazy outbound connection.

§Errors

Returns SdkError::FlowDefinition for invalid Worker options, or Service for runtime and endpoint initialization failures. The Dex connection is lazy.

Source

pub fn start(&self) -> SdkResult<()>

Negotiates protocol, synchronizes Attribute indexes, and serves until stopped or failed.

Stream errors are logged and retried. After Self::stop, this method drains active handlers for the configured timeout, cancels any remaining handlers, and waits for them to return. Call this method only once, on a dedicated thread.

§Errors

Returns FlowDefinition if called more than once, or Service for index synchronization, timeout, and server failures. Returns a service error exposing SdkError::pull_registration_error after draining when permanent-rejection shutdown is enabled.

Source

pub async fn wait_pull_registered(&self) -> SdkResult<()>

Waits until every Flow type queue and the app Worker ID queue has registered once.

Safe before or after Self::start and for concurrent callers. Start the Worker on a dedicated thread. Drop this future to cancel the wait, or use tokio::time::timeout. Completion remains latched after shutdown; use Self::pull_registration_state for probes.

§Errors

Returns a service error exposing SdkError::pull_registration_error for a permanent rejection without another registered stream before readiness. Returns the startup error if startup fails, or InvalidArgument for shutdown before readiness.

tokio::time::timeout(std::time::Duration::from_secs(10), worker.wait_pull_registered()).await??;
assert!(worker.pull_registration_state().is_ready);
Source

pub fn pull_reserved_rpc_declines(&self) -> u64

Returns cumulative Step declines caused by reserved RPC capacity for this Worker.

The counter counts decline events. Dex may decline the same Step dispatch repeatedly while retrying it, so read the counter as a rate. Full-capacity rejections and unknown names are excluded. The count never resets and remains available after stop. Concurrent reads are safe. Export the count to a monitoring counter to identify reserve pressure.

let declines: u64 = worker.pull_reserved_rpc_declines();
Source

pub fn pull_registration_state(&self) -> PullRegistrationState

Returns an independent snapshot of current pull queue registrations.

Readiness latches after every queue registers once. Only a permanent rejection with no registered stream makes it false. Successful registration clears that queue’s error. Transient disconnects and Stop do not clear readiness. See Self::wait_pull_registered for a startup and probe example.

Source

pub fn stop(&self)

Requests shutdown of a running Worker and returns immediately.

Calling stop before or after start is safe; Self::start observes the shutdown signal. The Worker first drains active handlers. After the drain timeout it cancels them, and Self::start waits for their return. Half-closed pull streams wait for server EOF until the remaining drain deadline. Stop is idempotent.

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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