temporalio_workflow/workflow_context/options/
nexus.rs1use std::{collections::HashMap, time::Duration};
2
3use crate::WorkflowCancellationToken;
4use temporalio_common_wasm::protos::{
5 coresdk::{
6 nexus::NexusOperationCancellationType as ProtoNexusOperationCancellationType,
7 workflow_commands::{ScheduleNexusOperation, WorkflowCommand, workflow_command},
8 },
9 temporal::api::common::v1::Payload,
10};
11
12#[derive(
14 Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize,
15)]
16#[non_exhaustive]
17pub enum NexusOperationCancellationType {
18 #[default]
20 WaitCancellationCompleted,
21 Abandon,
23 TryCancel,
25 WaitCancellationRequested,
27}
28
29impl From<NexusOperationCancellationType> for ProtoNexusOperationCancellationType {
30 fn from(value: NexusOperationCancellationType) -> Self {
31 match value {
32 NexusOperationCancellationType::WaitCancellationCompleted => {
33 Self::WaitCancellationCompleted
34 }
35 NexusOperationCancellationType::Abandon => Self::Abandon,
36 NexusOperationCancellationType::TryCancel => Self::TryCancel,
37 NexusOperationCancellationType::WaitCancellationRequested => {
38 Self::WaitCancellationRequested
39 }
40 }
41 }
42}
43
44impl From<ProtoNexusOperationCancellationType> for NexusOperationCancellationType {
45 fn from(value: ProtoNexusOperationCancellationType) -> Self {
46 match value {
47 ProtoNexusOperationCancellationType::WaitCancellationCompleted => {
48 Self::WaitCancellationCompleted
49 }
50 ProtoNexusOperationCancellationType::Abandon => Self::Abandon,
51 ProtoNexusOperationCancellationType::TryCancel => Self::TryCancel,
52 ProtoNexusOperationCancellationType::WaitCancellationRequested => {
53 Self::WaitCancellationRequested
54 }
55 }
56 }
57}
58
59#[derive(Debug, Clone, bon::Builder)]
61#[builder(on(String, into))]
62#[non_exhaustive]
63pub struct NexusOperationOptions {
64 pub endpoint: String,
66 pub service: String,
68 pub operation: String,
70 pub input: Option<Payload>,
75 pub schedule_to_close_timeout: Option<Duration>,
79 #[builder(default)]
86 pub nexus_header: HashMap<String, String>,
87 pub cancellation_type: Option<NexusOperationCancellationType>,
89 pub cancellation_token: Option<WorkflowCancellationToken>,
91 pub schedule_to_start_timeout: Option<Duration>,
97 pub start_to_close_timeout: Option<Duration>,
104}
105
106impl NexusOperationOptions {
107 pub(crate) fn into_command(self, seq: u32) -> WorkflowCommand {
108 workflow_command::Variant::ScheduleNexusOperation(ScheduleNexusOperation {
109 seq,
110 endpoint: self.endpoint,
111 service: self.service,
112 operation: self.operation,
113 input: self.input,
114 schedule_to_close_timeout: self
115 .schedule_to_close_timeout
116 .and_then(|duration| duration.try_into().ok()),
117 schedule_to_start_timeout: self
118 .schedule_to_start_timeout
119 .and_then(|duration| duration.try_into().ok()),
120 start_to_close_timeout: self
121 .start_to_close_timeout
122 .and_then(|duration| duration.try_into().ok()),
123 nexus_header: self.nexus_header,
124 cancellation_type: ProtoNexusOperationCancellationType::from(
125 self.cancellation_type
126 .unwrap_or(NexusOperationCancellationType::WaitCancellationCompleted),
127 )
128 .into(),
129 })
130 .into()
131 }
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 #[test]
139 fn cancellation_defaults_to_wait_for_completion() {
140 assert_eq!(
141 NexusOperationCancellationType::default(),
142 NexusOperationCancellationType::WaitCancellationCompleted
143 );
144 }
145}