Skip to main content

temporalio_workflow/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![warn(missing_docs)]
3
4//! Temporal workflow authoring APIs and runtime glue.
5
6extern crate self as temporalio_workflow;
7
8pub use temporalio_common_wasm as common;
9pub use temporalio_macros::{
10    init, query, run, signal, update, update_validator, workflow, workflow_methods,
11};
12
13#[doc(hidden)]
14pub mod __private {
15    pub use futures_util::{FutureExt, future::LocalBoxFuture, join, select_biased};
16
17    pub mod macros {
18        pub use crate::{
19            component::{
20                __wit_export, ExportedComponent, StaticWorkflowComponent, bindings,
21                instantiate_component_workflow,
22                instantiate_component_workflow_with_interceptor_constructors,
23            },
24            runtime::{
25                entry::WorkflowImplementation,
26                guest::WorkflowInstance,
27                host::WorkflowHost,
28                types::{
29                    UpdateDefinitionDescriptor, WorkflowDefinitionDescriptor, WorkflowFailure,
30                    WorkflowInit,
31                },
32            },
33        };
34    }
35
36    pub mod sdk {
37        pub use crate::runtime::{
38            entry::WorkflowImplementation,
39            guest::WorkflowInstance,
40            host::WorkflowHost,
41            instance::GuestWorkflowInstance,
42            is_sdk_wake,
43            types::{
44                ActivationJobResult, ActivationResult, MAIN_ROUTINE_ID, MainRoutineCompletion,
45                QueryResponse, RoutineCompletion, RoutineId, RoutineKind, RoutinePendingState,
46                RoutinePollResult, StartedRoutine, TaskFailure, TerminalOutcome,
47                UpdateRoutineCompletion, UpdateRoutineKind, WorkflowActivation, WorkflowFailure,
48                WorkflowInit,
49            },
50        };
51    }
52}
53
54mod cancellation;
55mod component;
56mod runtime;
57mod workflow_context;
58pub mod workflow_interceptors;
59pub mod workflows;
60
61pub use cancellation::{WorkflowCancellationError, WorkflowCancellationToken};
62pub use runtime::model::{TimerResult, WorkflowResult, WorkflowTermination};
63pub use temporalio_common_wasm::{
64    ActivityCloseTimeouts, Memo, MemoValue, MemoValues, RetryPolicy,
65    error::{
66        ActivityExecutionError, CancelExternalWorkflowError, ChildWorkflowExecutionError,
67        ChildWorkflowStartError, RetryState, TimeoutType, WorkflowSignalError,
68    },
69};
70pub use workflow_context::{
71    ActivityCancellationType, ActivityOptions, BaseWorkflowContext, CancellableFuture,
72    CancellableFutureWithReason, ChildWorkflowCancellationType, ChildWorkflowOptions,
73    ContinueAsNewOptions, ExternalWorkflowHandle, LocalActivityOptions, NamespacedWorkflowInfo,
74    ParentClosePolicy, SignalWorkflowOptions, StartChildWorkflowExecutionFailedCause,
75    StartChildWorkflowOutput, StartedChildWorkflow, SyncWorkflowContext, TimerOptions,
76    VersioningIntent, WaitConditionOptions, WorkflowContext, WorkflowContextFuture,
77    WorkflowContextKey, WorkflowContextView, WorkflowIdReusePolicy, WorkflowRandomStream,
78    WorkflowRandomValue,
79};
80#[cfg(feature = "experimental")]
81pub use workflow_context::{
82    ContinueAsNewVersioningBehavior, NexusOperationCancellationType, NexusOperationOptions,
83    PatchActivationCallback, PatchActivationInput, StartedNexusOperation,
84};
85#[doc(hidden)]
86pub use workflow_context::{
87    PatchActivationCallback as InternalPatchActivationCallback, PatchActivationCaller,
88};
89pub use workflows::{join, join_all, select};
90
91#[macro_export]
92#[doc(hidden)]
93macro_rules! __temporal_select {
94    ($($tokens:tt)*) => {
95        $crate::__private::select_biased! { $($tokens)* }
96    };
97}
98
99#[macro_export]
100#[doc(hidden)]
101macro_rules! __temporal_join {
102    ($($tokens:tt)*) => {
103        $crate::__private::join!($($tokens)*)
104    };
105}
106
107#[macro_export]
108#[doc(hidden)]
109macro_rules! __temporalio_export_workflow_component {
110    ($export_type:ident) => {
111        $crate::__private::macros::__wit_export!(
112            $export_type with_types_in $crate::__private::macros::bindings
113        );
114    };
115}
116
117#[macro_export]
118/// Export one or more workflow implementations as a component-model workflow module.
119///
120/// Component-side workflow interceptor constructors can be supplied with
121/// `interceptor_constructors = [constructor]`. Each constructor receives a read-only workflow
122/// context and is invoked for every workflow instance.
123macro_rules! export_workflow_module {
124    ([$($workflow:ty),+ $(,)?]) => {
125        ::temporalio_workflow::export_workflow_module!(
126            [$($workflow),+],
127            interceptor_constructors = [],
128        );
129    };
130    ([$($workflow:ty),+ $(,)?], interceptor_constructors = [$($constructor:expr),* $(,)?] $(,)?) => {
131        const _: () = {
132            struct __TemporalWorkflowModule;
133
134            fn __temporal_workflow_interceptor_constructors() -> ::std::vec::Vec<
135                ::temporalio_workflow::workflow_interceptors::WorkflowInterceptorConstructor,
136            > {
137                ::std::vec![
138                    $(
139                        ::temporalio_workflow::workflow_interceptors::WorkflowInterceptorConstructor::new(
140                            $constructor,
141                        )
142                    ),*
143                ]
144            }
145
146            impl $crate::__private::macros::StaticWorkflowComponent for __TemporalWorkflowModule {
147                fn list_workflows(
148                ) -> ::std::vec::Vec<$crate::__private::macros::WorkflowDefinitionDescriptor> {
149                    ::std::vec![$(<$workflow as $crate::__private::macros::WorkflowImplementation>::definition()),*]
150                }
151
152                fn instantiate_workflow(
153                    workflow_type: &str,
154                    init: $crate::__private::macros::WorkflowInit,
155                    host: ::std::rc::Rc<dyn $crate::__private::macros::WorkflowHost>,
156                ) -> ::std::result::Result<
157                    ::std::boxed::Box<dyn $crate::__private::macros::WorkflowInstance>,
158                    $crate::__private::macros::WorkflowFailure,
159                > {
160                    match workflow_type {
161                        $(
162                            name if name == <$workflow as $crate::__private::macros::WorkflowImplementation>::name() => {
163                                $crate::__private::macros::instantiate_component_workflow_with_interceptor_constructors::<$workflow>(
164                                    init,
165                                    host,
166                                    __temporal_workflow_interceptor_constructors(),
167                                )
168                            }
169                        )*
170                        _ => Err(::std::boxed::Box::new(
171                            ::temporalio_workflow::common::protos::temporal::api::failure::v1::Failure {
172                                message: ::std::format!(
173                                    "No workflow named '{}' exported by this component",
174                                    workflow_type
175                                ),
176                                ..::std::default::Default::default()
177                            },
178                        )),
179                    }
180                }
181            }
182
183            type __TemporalWorkflowComponentExport =
184                $crate::__private::macros::ExportedComponent<__TemporalWorkflowModule>;
185
186            ::temporalio_workflow::__temporalio_export_workflow_component!(
187                __TemporalWorkflowComponentExport
188            );
189        };
190    };
191}