Skip to content

Commit be6f80b

Browse files
committed
Make activity_definitions consistent
1 parent 8bb7e84 commit be6f80b

11 files changed

Lines changed: 298 additions & 351 deletions

File tree

crates/common-wasm/src/activity_definition.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
//! Contains types for activity definitions, used by the code generated by the macros for defining
22
//! activities, or directly by users targeting activities in other languages.
33
4-
use crate::data_converters::{TemporalDeserializable, TemporalSerializable};
4+
use crate::{
5+
data_converters::{TemporalDeserializable, TemporalSerializable},
6+
protos::temporal::api::common::v1::Payload,
7+
};
8+
use std::time::Duration;
59

610
/// Implement on a marker struct to define an activity.
711
///
@@ -18,3 +22,46 @@ pub trait ActivityDefinition {
1822
where
1923
Self: Sized;
2024
}
25+
26+
/// Returned as errors from activity functions.
27+
#[derive(Debug)]
28+
pub enum ActivityError {
29+
/// This error can be returned from activities to allow the explicit configuration of certain
30+
/// error properties. It's also the default error type that arbitrary errors will be converted
31+
/// into.
32+
Retryable {
33+
/// The underlying error
34+
source: Box<dyn std::error::Error + Send + Sync + 'static>,
35+
/// If specified, the next retry (if there is one) will occur after this delay
36+
explicit_delay: Option<Duration>,
37+
},
38+
/// Return this error to indicate your activity is cancelling
39+
Cancelled {
40+
/// Some data to save as the cancellation reason
41+
details: Option<Payload>,
42+
},
43+
/// Return this error to indicate that the activity should not be retried.
44+
NonRetryable(Box<dyn std::error::Error + Send + Sync + 'static>),
45+
/// Return this error to indicate that the activity will be completed outside of this activity
46+
/// definition, by an external client.
47+
WillCompleteAsync,
48+
}
49+
50+
impl ActivityError {
51+
/// Construct a cancelled error without details
52+
pub fn cancelled() -> Self {
53+
Self::Cancelled { details: None }
54+
}
55+
}
56+
57+
impl<E> From<E> for ActivityError
58+
where
59+
E: Into<anyhow::Error>,
60+
{
61+
fn from(source: E) -> Self {
62+
Self::Retryable {
63+
source: source.into().into_boxed_dyn_error(),
64+
explicit_delay: None,
65+
}
66+
}
67+
}

crates/common-wasm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub mod protos;
1414
pub mod worker;
1515
mod workflow_definition;
1616

17-
pub use activity_definition::ActivityDefinition;
17+
pub use activity_definition::{ActivityDefinition, ActivityError};
1818
pub use priority::Priority;
1919
pub use worker::WorkerDeploymentVersion;
2020
pub use workflow_definition::{

crates/common/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ pub mod protos;
1616
pub mod telemetry;
1717
pub mod worker;
1818
pub use temporalio_common_wasm::{
19-
ActivityDefinition, HasWorkflowDefinition, Priority, QueryDefinition, SignalDefinition,
20-
UntypedWorkflow, UpdateDefinition, WorkerDeploymentVersion, WorkflowDefinition,
21-
data_converters,
19+
ActivityDefinition, ActivityError, HasWorkflowDefinition, Priority, QueryDefinition,
20+
SignalDefinition, UntypedWorkflow, UpdateDefinition, WorkerDeploymentVersion,
21+
WorkflowDefinition, data_converters,
2222
};
2323

2424
macro_rules! dbg_panic {

0 commit comments

Comments
 (0)