@@ -23,19 +23,23 @@ use std::{
2323 mem,
2424 ops:: Add ,
2525 rc:: Rc ,
26- sync:: mpsc:: Sender ,
26+ sync:: { mpsc:: Sender , Arc } ,
2727 time:: { Duration , Instant } ,
2828} ;
29+ use temporal_sdk_core_api:: { errors:: WorkflowErrorType , worker:: WorkerConfig } ;
2930use temporal_sdk_core_protos:: {
3031 coresdk:: {
3132 workflow_activation:: {
3233 create_evict_activation, query_to_job, remove_from_cache:: EvictionReason ,
3334 workflow_activation_job, RemoveFromCache , WorkflowActivation ,
3435 } ,
35- workflow_commands:: QueryResult ,
36+ workflow_commands:: { FailWorkflowExecution , QueryResult } ,
3637 workflow_completion,
3738 } ,
38- temporal:: api:: { enums:: v1:: WorkflowTaskFailedCause , failure:: v1:: Failure } ,
39+ temporal:: api:: {
40+ command:: v1:: command:: Attributes as CmdAttribs , enums:: v1:: WorkflowTaskFailedCause ,
41+ failure:: v1:: Failure ,
42+ } ,
3943 TaskToken ,
4044} ;
4145use tokio:: sync:: oneshot;
@@ -92,6 +96,7 @@ pub(super) struct ManagedRun {
9296 /// We store the paginator used for our own run's history fetching
9397 paginator : Option < HistoryPaginator > ,
9498 completion_waiting_on_page_fetch : Option < RunActivationCompletion > ,
99+ config : Arc < WorkerConfig > ,
95100}
96101impl ManagedRun {
97102 pub ( super ) fn new (
@@ -100,6 +105,7 @@ impl ManagedRun {
100105 local_activity_request_sink : Rc < dyn LocalActivityRequestSink > ,
101106 ) -> ( Self , RunUpdateAct ) {
102107 let metrics = basics. metrics . clone ( ) ;
108+ let config = basics. worker_config . clone ( ) ;
103109 let wfm = WorkflowManager :: new ( basics) ;
104110 let mut me = Self {
105111 wfm,
@@ -114,6 +120,7 @@ impl ManagedRun {
114120 metrics,
115121 paginator : None ,
116122 completion_waiting_on_page_fetch : None ,
123+ config,
117124 } ;
118125 let rua = me. incoming_wft ( wft) ;
119126 ( me, rua)
@@ -534,7 +541,6 @@ impl ManagedRun {
534541 return None ;
535542 } ;
536543
537- self . metrics . wf_task_failed ( ) ;
538544 let message = format ! ( "Workflow activation completion failed: {:?}" , & failure) ;
539545 // We don't want to fail queries that could otherwise be retried
540546 let is_no_report_query_fail = self . pending_work_is_legacy_query ( )
@@ -570,12 +576,35 @@ impl ManagedRun {
570576 )
571577 }
572578 } else if should_report {
573- ActivationCompleteOutcome :: ReportWFTFail ( FailedActivationWFTReport :: Report (
574- tt, cause, failure,
575- ) )
579+ // Check if we should fail the workflow instead of the WFT because of user's preferences
580+ if matches ! ( cause, WorkflowTaskFailedCause :: NonDeterministicError )
581+ && self . config . should_fail_workflow (
582+ & self . wfm . machines . workflow_type ,
583+ & WorkflowErrorType :: Nondeterminism ,
584+ )
585+ {
586+ warn ! ( failure=?failure, "Failing workflow due to nondeterminism error" ) ;
587+ return self
588+ . successful_completion (
589+ vec ! [ WFCommand :: FailWorkflow ( FailWorkflowExecution {
590+ failure: failure. failure,
591+ } ) ] ,
592+ vec ! [ ] ,
593+ resp_chan,
594+ )
595+ . unwrap_or_else ( |e| {
596+ dbg_panic ! ( "Got next page request when auto-failing workflow: {e:?}" ) ;
597+ None
598+ } ) ;
599+ } else {
600+ ActivationCompleteOutcome :: ReportWFTFail ( FailedActivationWFTReport :: Report (
601+ tt, cause, failure,
602+ ) )
603+ }
576604 } else {
577605 ActivationCompleteOutcome :: WFTFailedDontReport
578606 } ;
607+ self . metrics . wf_task_failed ( ) ;
579608 self . reply_to_complete ( outcome, resp_chan) ;
580609 rur
581610 }
@@ -1039,6 +1068,25 @@ impl ManagedRun {
10391068 )
10401069 } ;
10411070
1071+ // Record metrics for any outgoing terminal commands
1072+ for cmd in commands. iter ( ) {
1073+ match cmd. attributes . as_ref ( ) {
1074+ Some ( CmdAttribs :: CompleteWorkflowExecutionCommandAttributes ( _) ) => {
1075+ self . metrics . wf_completed ( ) ;
1076+ }
1077+ Some ( CmdAttribs :: FailWorkflowExecutionCommandAttributes ( _) ) => {
1078+ self . metrics . wf_failed ( ) ;
1079+ }
1080+ Some ( CmdAttribs :: ContinueAsNewWorkflowExecutionCommandAttributes ( _) ) => {
1081+ self . metrics . wf_continued_as_new ( ) ;
1082+ }
1083+ Some ( CmdAttribs :: CancelWorkflowExecutionCommandAttributes ( _) ) => {
1084+ self . metrics . wf_canceled ( ) ;
1085+ }
1086+ _ => ( ) ,
1087+ }
1088+ }
1089+
10421090 ActivationCompleteOutcome :: ReportWFTSuccess ( ServerCommandsWithWorkflowInfo {
10431091 task_token : data. task_token ,
10441092 action : ActivationAction :: WftComplete {
0 commit comments