Skip to main content

TaskRegistry

Struct TaskRegistry 

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

Registry for task implementations.

Maps task IDs to factory functions that create task instances. This enables workflow serialization: only IDs and structure are serialized, and implementations are looked up from the registry at runtime.

Important: The registry is code, not data. It contains closures and cannot be serialized. Both serialization and deserialization sides must construct the same registry by calling the same registration functions. See module docs for the recommended pattern.

Implementations§

Source§

impl TaskRegistry

Source

pub fn new() -> TaskRegistry

Create a new empty registry.

Source

pub fn register<T, C>(&mut self, id: &str, codec: Arc<C>, task: T)
where T: CoreTask + 'static, <T as CoreTask>::Input: Send + 'static, <T as CoreTask>::Output: Send + 'static, <T as CoreTask>::Future: Send + 'static, C: Codec + DecodeValue<<T as CoreTask>::Input> + EncodeValue<<T as CoreTask>::Output> + 'static,

Register a task implementing CoreTask.

This method accepts any type implementing CoreTask, including:

  • Structs with explicit CoreTask implementation
  • Closures wrapped with fn_task

For convenience with raw closures, use register_fn.

§Example
use sayiir_core::prelude::*;
use std::pin::Pin;
use std::future::Future;

struct DoubleTask;
impl CoreTask for DoubleTask {
    type Input = u32;
    type Output = u32;
    type Future = Pin<Box<dyn Future<Output = Result<u32, BoxError>> + Send>>;
    fn run(&self, input: u32) -> Self::Future {
        Box::pin(async move { Ok(input * 2) })
    }
}

// Register a struct implementing CoreTask
registry.register("struct_task", codec.clone(), DoubleTask);

// Register a closure via fn_task wrapper
registry.register("closure_task", codec.clone(), fn_task(|i: u32| async move { Ok(i * 2) }));
Source

pub fn register_with_metadata<T, C>( &mut self, id: &str, codec: Arc<C>, task: T, metadata: TaskMetadata, )
where T: CoreTask + 'static, <T as CoreTask>::Input: Send + 'static, <T as CoreTask>::Output: Send + 'static, <T as CoreTask>::Future: Send + 'static, C: Codec + DecodeValue<<T as CoreTask>::Input> + EncodeValue<<T as CoreTask>::Output> + 'static,

Register a task implementing CoreTask with metadata.

Same as register, but allows attaching metadata for timeouts, retries, and display information.

Source

pub fn register_fn<I, O, F, Fut, C>(&mut self, id: &str, codec: Arc<C>, func: F)
where F: Fn(I) -> Fut + Send + Sync + 'static, I: Send + 'static, O: Send + 'static, Fut: Future<Output = Result<O, Box<dyn Error + Send + Sync>>> + Send + 'static, C: Codec + DecodeValue<I> + EncodeValue<O> + 'static,

Register a closure as a task (convenience method).

This is a convenience method for registering closures without needing to wrap them in fn_task.

§Example
registry.register_fn("double", codec.clone(), |input: u32| async move { Ok(input * 2) });
Source

pub fn register_fn_with_metadata<I, O, F, Fut, C>( &mut self, id: &str, codec: Arc<C>, func: F, metadata: TaskMetadata, )
where F: Fn(I) -> Fut + Send + Sync + 'static, I: Send + 'static, O: Send + 'static, Fut: Future<Output = Result<O, Box<dyn Error + Send + Sync>>> + Send + 'static, C: Codec + DecodeValue<I> + EncodeValue<O> + 'static,

Register a closure as a task with metadata.

Same as register_fn, but allows attaching metadata.

Source

pub fn register_join<O, F, Fut, C>(&mut self, id: &str, codec: Arc<C>, func: F)
where F: Fn(BranchOutputs<C>) -> Fut + Send + Sync + 'static, O: Send + 'static, Fut: Future<Output = Result<O, Box<dyn Error + Send + Sync>>> + Send + 'static, C: Codec + EncodeValue<O> + DecodeValue<NamedBranchResults> + Send + Sync + 'static,

Register a join task using a closure.

Source

pub fn register_join_with_metadata<O, F, Fut, C>( &mut self, id: &str, codec: Arc<C>, func: F, metadata: TaskMetadata, )
where F: Fn(BranchOutputs<C>) -> Fut + Send + Sync + 'static, O: Send + 'static, Fut: Future<Output = Result<O, Box<dyn Error + Send + Sync>>> + Send + 'static, C: Codec + EncodeValue<O> + DecodeValue<NamedBranchResults> + Send + Sync + 'static,

Register a join task using a closure with metadata.

Source

pub fn get( &self, id: &str, ) -> Option<Box<dyn CoreTask<Output = Bytes, Future = BytesFuture, Input = Bytes> + Send + Sync>>

Get a task by ID, creating a new instance.

Returns None if the task ID is not registered.

Source

pub fn get_metadata(&self, id: &str) -> Option<&TaskMetadata>

Get the metadata for a task by ID.

Returns None if the task ID is not registered.

Source

pub fn get_with_metadata( &self, id: &str, ) -> Option<(Box<dyn CoreTask<Output = Bytes, Future = BytesFuture, Input = Bytes> + Send + Sync>, &TaskMetadata)>

Get both the task and its metadata by ID.

Returns None if the task ID is not registered.

Source

pub fn set_metadata(&mut self, id: &str, metadata: TaskMetadata) -> bool

Set or update the metadata for a registered task.

Returns true if the task was found and metadata updated, false otherwise.

Source

pub fn contains(&self, id: &str) -> bool

Check if a task ID is registered.

Source

pub fn len(&self) -> usize

Get the number of registered tasks.

Source

pub fn is_empty(&self) -> bool

Check if the registry is empty.

Source

pub fn task_ids(&self) -> impl Iterator<Item = &str>

Get an iterator over registered task IDs.

Source

pub fn with_codec<C>(codec: Arc<C>) -> RegistryBuilder<C>
where C: Codec,

Create a builder with a codec for ergonomic task registration.

§Example
let registry = TaskRegistry::with_codec(codec)
    .register_fn("double", |i: u32| async move { Ok(i * 2) })
    .register_fn("add_ten", |i: u32| async move { Ok(i + 10) })
    .build();

Trait Implementations§

Source§

impl Default for TaskRegistry

Source§

fn default() -> TaskRegistry

Returns the “default value” for a type. Read more
Source§

impl RegistryBehavior for TaskRegistry

Source§

fn maybe_register<I, O, F, Fut, C>( &mut self, id: &str, codec: Arc<C>, func: &Arc<F>, )
where F: Fn(I) -> Fut + Send + Sync + 'static, I: Send + 'static, O: Send + 'static, Fut: Future<Output = Result<O, Box<dyn Error + Send + Sync>>> + Send + 'static, C: Codec + DecodeValue<I> + EncodeValue<O> + 'static,

Register a task (no-op for NoRegistry, actual registration for TaskRegistry).
Source§

fn maybe_register_join<O, F, Fut, C>( &mut self, id: &str, codec: Arc<C>, func: &Arc<F>, )
where F: Fn(BranchOutputs<C>) -> Fut + Send + Sync + 'static, O: Send + 'static, Fut: Future<Output = Result<O, Box<dyn Error + Send + Sync>>> + Send + 'static, C: Codec + EncodeValue<O> + DecodeValue<NamedBranchResults> + Send + Sync + 'static,

Register a join task (no-op for NoRegistry, actual registration for TaskRegistry).

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

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
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 = 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