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>
impl<I, O> Task<I, O>
Sourcepub fn parent<PI, PO>(self, parent: &Task<PI, PO>) -> Self
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>
impl<I, O> Task<I, O>
Sourcepub async fn run_no_wait(
&self,
client: &Client,
input: &I,
opts: RunOpts,
) -> Result<RunHandle<O>>where
I: Serialize,
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.
Sourcepub async fn run(&self, client: &Client, input: &I, opts: RunOpts) -> Result<O>where
I: Serialize,
O: DeserializeOwned,
pub async fn run(&self, client: &Client, input: &I, opts: RunOpts) -> Result<O>where
I: Serialize,
O: DeserializeOwned,
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.
Sourcepub async fn run_many(
&self,
client: &Client,
inputs: impl IntoIterator<Item = (I, RunOpts)>,
) -> Result<Vec<RunHandle<O>>>where
I: Serialize,
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§
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>
impl<I, O> Move for Task<I, O>
impl<I, O> Send for Task<I, O>
impl<I, O> Sync for Task<I, O>
impl<I, O> Unpin for Task<I, O>
impl<I, O> UnsafeUnpin for Task<I, O>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request