Skip to content

Commit 2d9e3cd

Browse files
authored
WAIT_CANCELLATION_COMPLETED should be default, populate operation_token on cancellation failures (#947)
1 parent 419f372 commit 2d9e3cd

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

core/src/worker/workflow/machines/nexus_operation_state_machine.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fsm! {
5353
ScheduledEventRecorded
5454
--(NexusOperationTimedOut(NexusOperationTimedOutEventAttributes), on_timed_out)--> TimedOut;
5555
ScheduledEventRecorded
56-
--(NexusOperationStarted(NexusOperationStartedEventAttributes), on_started)--> Started;
56+
--(NexusOperationStarted(NexusOperationStartedEventAttributes), shared on_started)--> Started;
5757

5858
Started --(Cancel, shared on_issue_cancel)--> Started;
5959
Started --(Cancel, shared on_issue_cancel)--> Cancelled;
@@ -113,6 +113,7 @@ pub(super) struct SharedState {
113113
cancelled_before_sent: bool,
114114
cancel_sent: bool,
115115
cancel_type: NexusOperationCancellationType,
116+
operation_token: Option<String>,
116117
}
117118

118119
impl NexusOperationMachine {
@@ -128,6 +129,7 @@ impl NexusOperationMachine {
128129
cancelled_before_sent: false,
129130
cancel_sent: false,
130131
cancel_type: attribs.cancellation_type(),
132+
operation_token: None,
131133
},
132134
);
133135
NewMachineWithCommand {
@@ -246,8 +248,10 @@ impl ScheduledEventRecorded {
246248

247249
pub(super) fn on_started(
248250
self,
251+
ss: &mut SharedState,
249252
sa: NexusOperationStartedEventAttributes,
250253
) -> NexusOperationMachineTransition<Started> {
254+
ss.operation_token = Some(sa.operation_token.clone());
251255
NexusOperationMachineTransition::commands([NexusOperationCommand::Start {
252256
operation_token: sa.operation_token,
253257
}])
@@ -496,6 +500,7 @@ impl WFMachinesAdapter for NexusOperationMachine {
496500
status: Some(resolve_nexus_operation_start::Status::CancelledBeforeStart(
497501
self.cancelled_failure(
498502
"Nexus Operation cancelled before scheduled".to_owned(),
503+
&self.shared_state.operation_token,
499504
),
500505
)),
501506
}
@@ -506,6 +511,7 @@ impl WFMachinesAdapter for NexusOperationMachine {
506511
status: Some(nexus_operation_result::Status::Cancelled(
507512
self.cancelled_failure(
508513
"Nexus Operation cancelled before scheduled".to_owned(),
514+
&self.shared_state.operation_token,
509515
),
510516
)),
511517
}),
@@ -584,6 +590,7 @@ impl WFMachinesAdapter for NexusOperationMachine {
584590
status: Some(nexus_operation_result::Status::Cancelled(
585591
self.cancelled_failure(
586592
"Nexus operation cancelled after starting".to_owned(),
593+
&self.shared_state.operation_token,
587594
),
588595
)),
589596
}),
@@ -610,7 +617,7 @@ impl TryFrom<CommandType> for NexusOperationMachineEvents {
610617
}
611618

612619
impl NexusOperationMachine {
613-
fn cancelled_failure(&self, message: String) -> Failure {
620+
fn cancelled_failure(&self, message: String, operation_token: &Option<String>) -> Failure {
614621
Failure {
615622
message,
616623
cause: Some(Box::new(Failure {
@@ -624,7 +631,7 @@ impl NexusOperationMachine {
624631
service: self.shared_state.service.clone(),
625632
operation: self.shared_state.operation.clone(),
626633
operation_id: "".to_string(),
627-
operation_token: "".to_string(),
634+
operation_token: operation_token.clone().unwrap_or_default(),
628635
},
629636
)),
630637
..Default::default()

sdk-core-protos/protos/local/temporal/sdk/core/nexus/nexus.proto

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ enum NexusTaskCancelReason {
7272

7373
// Controls at which point to report back to lang when a nexus operation is cancelled
7474
enum NexusOperationCancellationType {
75+
// Wait for operation cancellation completion. Default.
76+
WAIT_CANCELLATION_COMPLETED = 0;
7577
// Do not request cancellation of the nexus operation if already scheduled
76-
ABANDON = 0;
78+
ABANDON = 1;
7779

7880
// Initiate a cancellation request for the Nexus operation and immediately report cancellation
7981
// to the caller. Note that it doesn't guarantee that cancellation is delivered to the operation if calling workflow exits before the delivery is done.
8082
// If you want to ensure that cancellation is delivered to the operation, use WAIT_CANCELLATION_REQUESTED.
81-
TRY_CANCEL = 1;
83+
TRY_CANCEL = 2;
8284
// Request cancellation of the operation and wait for confirmation that the request was received.
83-
WAIT_CANCELLATION_REQUESTED = 2;
84-
// Wait for operation cancellation completion. Default.
85-
WAIT_CANCELLATION_COMPLETED = 3;
85+
WAIT_CANCELLATION_REQUESTED = 3;
8686
}

0 commit comments

Comments
 (0)