Skip to main content

Task

Struct Task 

Source
pub struct Task<I, O> { /* private fields */ }
Expand description

A declared task: register it on a worker by itself, or add it to a Workflow.

Implementations§

Source§

impl<I, O> Task<I, O>

Source

pub fn name(&self) -> &str

The task’s name as declared.

Source

pub fn parent<PI, PO>(self, parent: &Task<PI, PO>) -> Self

Makes this task run after parent within a workflow. Call once per parent. The parent must be added to the same workflow.

parent is taken by reference, so parents must be attached before the parent task is moved into the workflow (or cloned first):

let fetch = TaskBuilder::<(), u32>::new("fetch").handler(|_ctx, _input| async { Ok(1) });
let report = TaskBuilder::<(), ()>::new("report")
    .handler(|_ctx, _input| async { Ok(()) })
    .parent(&fetch);
let workflow = Workflow::new("pipeline").task(&fetch).task(&report);
Source§

impl<I, O> Task<I, O>

Source

pub async fn run_no_wait( &self, client: &Client, input: &I, opts: RunOpts, ) -> Result<RunHandle<O>>
where I: Serialize,

Triggers a run of this task and hands back a handle to it, without waiting for it to finish.

What is triggered is the workflow named after the task, which is the right thing for a task registered on its own — a standalone task is a one-task workflow of the same name. A task declared inside a Workflow is not addressable by itself; trigger its workflow with Workflow::run_no_wait instead.

Nothing here checks that the engine is a version this SDK supports; a client that only triggers runs makes no other call. Call Client::check_engine_version once at startup if you want that checked.

§Retries

A TriggerWorkflow call the engine received but could not answer in time is sent again under the SDK’s retry policy, so a run can be started twice — the handle then names only the second one. This is the same exposure as the official Go SDK. A top-level run has no deduplication key to hand the engine (the engine’s own is for child runs, see RunOpts::child_key), so a task that must not happen twice needs Workflow::idempotency.

§Errors

Error::Json when input or a metadata value cannot be serialized; Error::IdempotencyCollision when the workflow declares idempotency and the computed key is already claimed by another run; Error::InvalidArgument when opts asks for RunOpts::sticky outside a task handler (there is no parent run to inherit a worker from); and Error::Grpc when the call fails for any other reason.

Source

pub async fn run(&self, client: &Client, input: &I, opts: RunOpts) -> Result<O>

Triggers a run of this task and waits for its output.

Equivalent to run_no_wait followed by RunHandle::result, and waits just as long: for ever, if the run never finishes. Wrap the call in tokio::time::timeout if that is not acceptable.

// The same declaration the worker registers; triggering only needs
// its name and its types.
let review = TaskBuilder::<Review, Verdict>::new("review")
    .handler(|_ctx, input: Review| async move {
        Ok(Verdict { approved: input.repo == "hatchet-rs" })
    });

let verdict = review
    .run(
        client,
        &Review { repo: "hatchet-rs".to_string() },
        RunOpts::default().metadata("customer", "acme"),
    )
    .await?;
println!("approved: {}", verdict.approved);
§Errors

Those of run_no_wait, plus those of RunHandle::result.

Source

pub async fn run_many( &self, client: &Client, inputs: impl IntoIterator<Item = (I, RunOpts)>, ) -> Result<Vec<RunHandle<O>>>
where I: Serialize,

Triggers a run of this task for every (input, opts) pair, in as few BulkTriggerWorkflow calls as the engine’s own limits allow (1000 requests or 3 MiB of encoded requests per call, whichever comes first), and hands back handles in the same order as inputs.

What is triggered is the workflow named after the task, exactly as in run_no_wait: right for a standalone task, and not possible for a task declared inside a Workflow, which is not addressable on its own — use Workflow::run_many for those.

Every request is built and serialized before the first call goes out, so a bad input aborts the whole batch rather than starting some runs and then failing on a later one. An empty inputs makes no call at all.

One RunOpts for the whole batch is a map over the inputs, since the pairs are what this takes:

let opts = RunOpts::default().metadata("batch", "nightly");
let handles = task
    .run_many(client, inputs.into_iter().map(|input| (input, opts.clone())))
    .await?;
§Retries

A BulkTriggerWorkflow call the engine received but could not answer in time is sent again under the SDK’s retry policy, so a chunk of up to 1000 runs can be started twice — the handles then name only the second set. This is the same exposure as the official Go SDK; tasks that must not happen twice need Workflow::idempotency.

§Errors

Error::Json when an input or a metadata value cannot be serialized (before anything is sent); Error::InvalidArgument when any pair’s opts asks for RunOpts::sticky (there is no parent run at this level to inherit a worker from), also checked before anything is sent; Error::Grpc or Error::IdempotencyCollision/Error::BulkIdempotencyCollision when the first chunk fails outright; and Error::PartialBulkTrigger when a later chunk fails (for any of those same reasons) after earlier ones already started runs — its run_ids are the runs that really were started, so they are not lost.

Trait Implementations§

Source§

impl<I, O> Clone for Task<I, O>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I, O> Debug for Task<I, O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I, O> Register for Task<I, O>

Auto Trait Implementations§

§

impl<I, O> !RefUnwindSafe for Task<I, O>

§

impl<I, O> !UnwindSafe for Task<I, O>

§

impl<I, O> Forget for Task<I, O>

§

impl<I, O> Freeze for Task<I, O>
where PhantomData<fn(I) -> O>: Freeze,

§

impl<I, O> Move for Task<I, O>

§

impl<I, O> Send for Task<I, O>
where PhantomData<fn(I) -> O>: Send,

§

impl<I, O> Sync for Task<I, O>
where PhantomData<fn(I) -> O>: Sync,

§

impl<I, O> Unpin for Task<I, O>
where PhantomData<fn(I) -> O>: Unpin,

§

impl<I, O> UnsafeUnpin for Task<I, O>
where PhantomData<fn(I) -> O>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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