-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathexecute_function_graph.rs
More file actions
1118 lines (979 loc) · 38.7 KB
/
Copy pathexecute_function_graph.rs
File metadata and controls
1118 lines (979 loc) · 38.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! ExecuteFunctionGraph orchestration - the main durable function executor
//!
//! ⚠️ DETERMINISTIC CODE ONLY in this file!
//! - No I/O except through activities
//! - No random numbers, current time, or other non-deterministic sources
//! - Same input must always produce the same scheduling decisions
use std::collections::HashMap;
use std::time::Duration;
use duroxide::OrchestrationContext;
use crate::activities;
use crate::types::{
evaluate_condition, substitute_all, substitute_all_raw, FunctionGraph, FunctionInput,
FunctionNode, SystemVars,
};
/// Orchestration name for ExecuteFunctionGraph
pub const NAME: &str = "pg_durable::orchestration::execute-function-graph";
/// Orchestration name for ExecuteSubtree (used for parallel JOIN/RACE)
pub const SUBTREE_NAME: &str = "pg_durable::orchestration::execute-subtree";
/// Execution context containing vars and metadata
#[derive(Clone)]
struct ExecutionContext {
vars: HashMap<String, String>,
label: Option<String>,
}
/// Envelope returned by `execute_subtree` containing the SQL result and the updated
/// named-results map so the parent orchestration can merge any new entries after join/race.
#[derive(serde::Serialize, serde::Deserialize)]
struct SubtreeEnvelope {
result: String,
results: HashMap<String, String>,
}
/// Execute a complete function graph
pub async fn execute(ctx: OrchestrationContext, input_json: String) -> Result<String, String> {
let input: FunctionInput = serde_json::from_str(&input_json)
.map_err(|e| format!("Invalid orchestration input: {e}"))?;
let label_info = input
.label
.as_ref()
.map(|l| format!(" ({l})"))
.unwrap_or_default();
ctx.trace_info(format!(
"Starting ExecuteFunctionGraph for instance: {}{}",
input.instance_id, label_info
));
if !input.vars.is_empty() {
// Sort keys for deterministic logging
let mut keys: Vec<_> = input.vars.keys().collect();
keys.sort();
ctx.trace_info(format!("Workflow vars: {keys:?}"));
}
let graph_json = match ctx
.schedule_activity(
activities::load_function_graph::NAME,
input.instance_id.clone(),
)
.await
{
Ok(json) => json,
Err(e) => {
// load_function_graph failed (e.g., superuser blocked).
// Mark the instance as failed before propagating.
let status_input = serde_json::json!({
"instance_id": input.instance_id,
"status": "failed"
});
let _ = ctx
.schedule_activity(
activities::update_instance_status::NAME,
status_input.to_string(),
)
.await;
return Err(e);
}
};
let graph: FunctionGraph = serde_json::from_str(&graph_json)
.map_err(|e| format!("Failed to parse function graph: {e}"))?;
ctx.trace_info(format!(
"Executing function with {} nodes, root: {}",
graph.nodes.len(),
graph.root_node_id
));
// Mark the instance as running now that we have loaded the graph and are
// about to execute. This call is idempotent: on continue_as_new the
// instance is already 'running', so re-issuing the update is harmless.
let running_input = serde_json::json!({
"instance_id": input.instance_id,
"status": "running"
});
let _ = ctx
.schedule_activity(
activities::update_instance_status::NAME,
running_input.to_string(),
)
.await;
let mut results: HashMap<String, String> = HashMap::new();
// Create execution context with vars
let exec_ctx = ExecutionContext {
vars: input.vars.clone(),
label: input.label.clone(),
};
let function_result =
execute_function_node_with_vars(&ctx, &graph, &graph.root_node_id, &mut results, &exec_ctx)
.await;
match &function_result {
Ok(result) => {
ctx.trace_info(format!("Function completed with result: {result}"));
let status_input = serde_json::json!({
"instance_id": input.instance_id,
"status": "completed"
});
let _ = ctx
.schedule_activity(
activities::update_instance_status::NAME,
status_input.to_string(),
)
.await;
}
Err(err) => {
ctx.trace_info(format!("Function failed with error: {err}"));
let status_input = serde_json::json!({
"instance_id": input.instance_id,
"status": "failed"
});
let _ = ctx
.schedule_activity(
activities::update_instance_status::NAME,
status_input.to_string(),
)
.await;
}
}
function_result
}
/// Execute a subtree of a function graph (used for parallel JOIN/RACE)
pub async fn execute_subtree(
ctx: OrchestrationContext,
input_json: String,
) -> Result<String, String> {
let input: serde_json::Value = serde_json::from_str(&input_json)
.map_err(|e| format!("Failed to parse ExecuteSubtree input: {e}"))?;
let graph_json = input["graph"]
.as_str()
.ok_or("Missing graph in ExecuteSubtree input")?;
let node_id = input["node_id"]
.as_str()
.ok_or("Missing node_id in ExecuteSubtree input")?;
let results_json = input["results"]
.as_str()
.ok_or("Missing results in ExecuteSubtree input")?;
let graph: FunctionGraph = serde_json::from_str(graph_json)
.map_err(|e| format!("Failed to parse graph in ExecuteSubtree: {e}"))?;
let mut results: HashMap<String, String> = serde_json::from_str(results_json)
.map_err(|e| format!("Failed to parse results in ExecuteSubtree: {e}"))?;
let vars: HashMap<String, String> = if let Some(vars_json) = input["vars"].as_str() {
serde_json::from_str(vars_json)
.map_err(|e| format!("Failed to parse vars in ExecuteSubtree: {e}"))?
} else {
HashMap::new()
};
let label: Option<String> = input["label"].as_str().map(|s| s.to_string());
ctx.trace_info(format!("ExecuteSubtree: executing node {node_id}"));
let exec_ctx = ExecutionContext { vars, label };
let result =
execute_function_node_with_vars(&ctx, &graph, node_id, &mut results, &exec_ctx).await?;
ctx.trace_info(format!("ExecuteSubtree: node {node_id} completed"));
// Return an envelope with both the result and the updated results map so the parent
// orchestration can merge any named results produced inside this subtree.
let envelope = SubtreeEnvelope { result, results };
serde_json::to_string(&envelope)
.map_err(|e| format!("Failed to serialize subtree envelope: {e}"))
}
/// Recursively execute function nodes with vars support
async fn execute_function_node_with_vars(
ctx: &OrchestrationContext,
graph: &FunctionGraph,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
) -> Result<String, String> {
let node = graph
.nodes
.get(node_id)
.ok_or_else(|| format!("Node not found: {node_id}"))?;
ctx.trace_info(format!(
"Executing node {} (type: {})",
node_id, node.node_type
));
// Mark node as running
let running_input = serde_json::json!({
"node_id": node_id,
"status": "running"
});
let _ = ctx
.schedule_activity(
activities::update_node_status::NAME,
running_input.to_string(),
)
.await;
let execute_result = execute_node_inner(ctx, graph, node_id, node, results, exec_ctx).await;
// Update node with final status and result
match &execute_result {
Ok(result) => {
let completed_input = serde_json::json!({
"node_id": node_id,
"status": "completed",
"result": result
});
let _ = ctx
.schedule_activity(
activities::update_node_status::NAME,
completed_input.to_string(),
)
.await;
}
Err(err) => {
let failed_input = serde_json::json!({
"node_id": node_id,
"status": "failed",
"result": err
});
let _ = ctx
.schedule_activity(
activities::update_node_status::NAME,
failed_input.to_string(),
)
.await;
}
}
execute_result
}
/// Inner function that actually executes the node logic
async fn execute_node_inner(
ctx: &OrchestrationContext,
graph: &FunctionGraph,
node_id: &str,
node: &FunctionNode,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
) -> Result<String, String> {
// Build system vars
let sys_vars = SystemVars {
instance_id: graph.instance_id.clone(),
label: exec_ctx.label.clone(),
};
match node.node_type.to_lowercase().as_str() {
"sql" => execute_sql_node(ctx, node, node_id, results, exec_ctx, &sys_vars).await,
"then" => execute_then_node(ctx, graph, node, node_id, results, exec_ctx).await,
"sleep" => execute_sleep_node(ctx, node, node_id).await,
"wait_schedule" => execute_wait_schedule_node(ctx, node, node_id).await,
"loop" => execute_loop_node(ctx, graph, node, node_id, results, exec_ctx).await,
"if" => execute_if_node(ctx, graph, node, node_id, results, exec_ctx).await,
"join" => execute_join_node(ctx, graph, node, node_id, results, exec_ctx).await,
"race" => execute_race_node(ctx, graph, node, node_id, results, exec_ctx).await,
"http" => execute_http_node(ctx, node, node_id, results, exec_ctx, &sys_vars).await,
"signal" => execute_signal_node(ctx, node, node_id, results).await,
"break" => execute_break_node(ctx, node, node_id).await,
other => Err(format!("Unknown node type: {other}")),
}
}
// ============================================================================
// Node Type Handlers
// ============================================================================
async fn execute_sql_node(
ctx: &OrchestrationContext,
node: &FunctionNode,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
sys_vars: &SystemVars,
) -> Result<String, String> {
let query = node
.query
.as_ref()
.ok_or_else(|| format!("SQL node {node_id} has no query"))?;
let final_query = substitute_all(query, results, &exec_ctx.vars, sys_vars)?;
ctx.trace_info(format!("Executing SQL: {final_query}"));
let input = serde_json::json!({
"query": final_query,
"submitted_by": node.submitted_by,
"database": node.database,
});
let result = ctx
.schedule_activity(activities::execute_sql::NAME, input.to_string())
.await?;
if let Some(name) = &node.result_name {
ctx.trace_info(format!("Storing result as ${name}"));
results.insert(name.clone(), result.clone());
}
Ok(result)
}
fn store_named_result(
ctx: &OrchestrationContext,
node: &FunctionNode,
result: &str,
results: &mut HashMap<String, String>,
node_label: &str,
) {
if let Some(name) = &node.result_name {
ctx.trace_info(format!("Storing {node_label} result as ${name}"));
results.insert(name.clone(), result.to_string());
}
}
async fn execute_then_node(
ctx: &OrchestrationContext,
graph: &FunctionGraph,
node: &FunctionNode,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
) -> Result<String, String> {
let left_id = node
.left_node
.as_ref()
.ok_or_else(|| format!("THEN node {node_id} has no left_node"))?;
let right_id = node
.right_node
.as_ref()
.ok_or_else(|| format!("THEN node {node_id} has no right_node"))?;
let left_result = Box::pin(execute_function_node_with_vars(
ctx, graph, left_id, results, exec_ctx,
))
.await?;
// Propagate break signals immediately
if is_break_signal(&left_result) {
return Ok(left_result);
}
let right_result = Box::pin(execute_function_node_with_vars(
ctx, graph, right_id, results, exec_ctx,
))
.await?;
store_named_result(ctx, node, &right_result, results, "THEN");
Ok(right_result)
}
async fn execute_sleep_node(
ctx: &OrchestrationContext,
node: &FunctionNode,
node_id: &str,
) -> Result<String, String> {
let seconds_str = node
.query
.as_ref()
.ok_or_else(|| format!("SLEEP node {node_id} has no duration"))?;
let seconds: u64 = seconds_str
.parse()
.map_err(|_| format!("Invalid sleep duration: {seconds_str}"))?;
ctx.trace_info(format!("Sleeping for {seconds} seconds"));
ctx.schedule_timer(Duration::from_secs(seconds)).await;
Ok(format!(r#"{{"slept": true, "seconds": {seconds}}}"#))
}
async fn execute_wait_schedule_node(
ctx: &OrchestrationContext,
node: &FunctionNode,
node_id: &str,
) -> Result<String, String> {
let config_str = node
.query
.as_ref()
.ok_or_else(|| format!("WAIT_SCHEDULE node {node_id} has no config"))?;
// Parse pre-computed config from DSL time
let config: serde_json::Value = serde_json::from_str(config_str)
.map_err(|e| format!("Invalid WAIT_SCHEDULE config: {e}"))?;
let wait_seconds = config["wait_seconds"]
.as_u64()
.ok_or_else(|| "WAIT_SCHEDULE missing wait_seconds".to_string())?;
let cron_expr = config["cron_expr"].as_str().unwrap_or("?");
ctx.trace_info(format!(
"Waiting {wait_seconds} seconds until schedule: {cron_expr}"
));
ctx.schedule_timer(Duration::from_secs(wait_seconds)).await;
Ok(r#"{"scheduled": true}"#.to_string())
}
/// Sentinel key used to signal a break from within a loop
const BREAK_SENTINEL: &str = "__break__";
/// Minimum wall-clock duration that every loop iteration must take before
/// `continue_as_new` is called. If the body (plus any while-condition
/// evaluation) completes faster than this, a compensating timer makes up the
/// deficit so an empty-bodied loop can't busy-spin via continue_as_new.
const LOOP_MIN_ITER_DURATION: Duration = Duration::from_secs(1);
/// Check if a result contains a break signal
fn is_break_signal(result: &str) -> bool {
serde_json::from_str::<serde_json::Value>(result)
.map(|v| {
v.get(BREAK_SENTINEL)
.and_then(|b| b.as_bool())
.unwrap_or(false)
})
.unwrap_or(false)
}
/// Extract the break value from a break signal
fn extract_break_value(result: &str) -> String {
serde_json::from_str::<serde_json::Value>(result)
.ok()
.and_then(|v| v.get("value").cloned())
.map(|v| v.to_string())
.unwrap_or_else(|| "null".to_string())
}
async fn execute_loop_node(
ctx: &OrchestrationContext,
graph: &FunctionGraph,
node: &FunctionNode,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
) -> Result<String, String> {
let body_id = node
.left_node
.as_ref()
.ok_or_else(|| format!("LOOP node {node_id} has no body"))?;
// Capture the iteration start time so we can rate-limit `continue_as_new`
// below. `utc_now()` is duroxide's deterministic clock (recorded in
// history and replayed verbatim), so this remains replay-safe.
let iter_started = ctx.utc_now().await.ok();
ctx.trace_info("Executing loop iteration");
let body_result = Box::pin(execute_function_node_with_vars(
ctx, graph, body_id, results, exec_ctx,
))
.await?;
// Check for break signal from body
if is_break_signal(&body_result) {
let break_value = extract_break_value(&body_result);
ctx.trace_info(format!(
"Loop terminated by break with value: {break_value}"
));
store_named_result(ctx, node, &break_value, results, "LOOP");
return Ok(break_value);
}
// Check while-condition if present
if let Some(ref config_str) = node.query {
if let Ok(config) = serde_json::from_str::<serde_json::Value>(config_str) {
if let Some(condition_node_id) = config["condition_node"].as_str() {
ctx.trace_info("Evaluating loop condition");
let condition_result = Box::pin(execute_function_node_with_vars(
ctx,
graph,
condition_node_id,
results,
exec_ctx,
))
.await?;
// Parse condition result to check truthiness (uses evaluate_condition to extract boolean from SQL result)
let should_continue = evaluate_condition(&condition_result).unwrap_or(false);
ctx.trace_info(format!(
"Loop condition evaluated to: {condition_result} (continue={should_continue})"
));
if !should_continue {
ctx.trace_info("Loop condition false, exiting loop");
store_named_result(ctx, node, &body_result, results, "LOOP");
return Ok(body_result);
}
}
}
}
ctx.trace_info("Continuing as new for next loop iteration");
// Enforce a minimum per-iteration wall-clock duration to prevent
// busy-looping (e.g. `df.loop(df.sleep(0))`). Compute the elapsed time
// from the deterministic clock; if the iteration finished faster than
// LOOP_MIN_ITER_DURATION, schedule a timer for the deficit so the next
// continue_as_new is gated by at least that much real-clock time.
if let Some(started) = iter_started {
if let Ok(now) = ctx.utc_now().await {
let elapsed = now.duration_since(started).unwrap_or(Duration::ZERO);
if elapsed < LOOP_MIN_ITER_DURATION {
let deficit = LOOP_MIN_ITER_DURATION - elapsed;
ctx.trace_info(format!(
"Loop iteration took {elapsed:?} (< {LOOP_MIN_ITER_DURATION:?}); \
adding {deficit:?} rate-limit delay"
));
ctx.schedule_timer(deficit).await;
}
}
}
// Preserve vars in continue_as_new input
let new_input = FunctionInput {
instance_id: graph.instance_id.clone(),
label: exec_ctx.label.clone(),
vars: exec_ctx.vars.clone(),
};
// duroxide 0.1.1: continue_as_new returns an awaitable future - return it directly
return ctx
.continue_as_new(serde_json::to_string(&new_input).unwrap_or(graph.instance_id.clone()))
.await
.map(|_| body_result)
.map_err(|e| format!("continue_as_new failed: {e:?}"));
}
async fn execute_break_node(
ctx: &OrchestrationContext,
node: &FunctionNode,
node_id: &str,
) -> Result<String, String> {
let break_value = node
.query
.as_ref()
.and_then(|config_str| serde_json::from_str::<serde_json::Value>(config_str).ok())
.and_then(|config| config.get("break_value").cloned())
.and_then(|v| {
if v.is_null() {
None
} else {
v.as_str().map(|s| s.to_string())
}
});
ctx.trace_info(format!(
"BREAK node {node_id} executed with value: {break_value:?}"
));
// Return a special break signal that the loop will detect
let result = serde_json::json!({
BREAK_SENTINEL: true,
"value": break_value.map(|v| serde_json::from_str::<serde_json::Value>(&v).unwrap_or(serde_json::Value::String(v)))
});
Ok(result.to_string())
}
async fn execute_if_node(
ctx: &OrchestrationContext,
graph: &FunctionGraph,
node: &FunctionNode,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
) -> Result<String, String> {
let config_str = node
.query
.as_ref()
.ok_or_else(|| format!("IF node {node_id} has no config"))?;
let config: serde_json::Value =
serde_json::from_str(config_str).map_err(|e| format!("Invalid IF config: {e}"))?;
let then_id = node
.left_node
.as_ref()
.ok_or_else(|| format!("IF node {node_id} has no then branch"))?;
let else_id = node
.right_node
.as_ref()
.ok_or_else(|| format!("IF node {node_id} has no else branch"))?;
let is_true =
if config.get("condition_type").and_then(|ct| ct.as_str()) == Some("result_has_rows") {
// df.if_rows: check row_count from in-memory results — no activity needed
let result_name = config["result_name"]
.as_str()
.ok_or_else(|| "df.if_rows: missing result_name".to_string())?;
let result_json = results
.get(result_name)
.ok_or_else(|| format!("df.if_rows: result '{result_name}' not found"))?;
let parsed: serde_json::Value = serde_json::from_str(result_json)
.map_err(|e| format!("df.if_rows: invalid result JSON: {e}"))?;
let row_count = parsed
.get("row_count")
.and_then(|rc| rc.as_u64())
.ok_or_else(|| {
format!(
"df.if_rows: result '{result_name}' is not a SQL result (missing row_count)"
)
})?;
ctx.trace_info(format!("if_rows '{result_name}': {row_count} rows"));
row_count > 0
} else {
// df.if: execute condition node as SQL
let condition_node_id = config["condition_node"]
.as_str()
.ok_or_else(|| "IF node missing condition_node".to_string())?;
ctx.trace_info("Evaluating IF condition");
let condition_result = Box::pin(execute_function_node_with_vars(
ctx,
graph,
condition_node_id,
results,
exec_ctx,
))
.await?;
evaluate_condition(&condition_result)?
};
ctx.trace_info(format!("Condition evaluated to: {is_true}"));
if is_true {
let result = Box::pin(execute_function_node_with_vars(
ctx, graph, then_id, results, exec_ctx,
))
.await?;
store_named_result(ctx, node, &result, results, "IF");
Ok(result)
} else {
let result = Box::pin(execute_function_node_with_vars(
ctx, graph, else_id, results, exec_ctx,
))
.await?;
store_named_result(ctx, node, &result, results, "IF");
Ok(result)
}
}
/// Parse the JSON envelope returned by `execute_subtree`, extract the SQL result string,
/// and merge the branch's named results into `parent_results`.
fn parse_subtree_envelope(
raw: &str,
context: &str,
parent_results: &mut HashMap<String, String>,
) -> Result<String, String> {
let envelope: SubtreeEnvelope =
serde_json::from_str(raw).map_err(|e| format!("{context} envelope parse error: {e}"))?;
parent_results.extend(envelope.results);
Ok(envelope.result)
}
async fn execute_join_node(
ctx: &OrchestrationContext,
graph: &FunctionGraph,
node: &FunctionNode,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
) -> Result<String, String> {
let left_id = node
.left_node
.as_ref()
.ok_or_else(|| format!("JOIN node {node_id} has no left branch"))?;
let right_id = node
.right_node
.as_ref()
.ok_or_else(|| format!("JOIN node {node_id} has no right branch"))?;
ctx.trace_info("Executing JOIN branches in parallel");
let graph_json =
serde_json::to_string(&graph).map_err(|e| format!("Failed to serialize graph: {e}"))?;
let results_json =
serde_json::to_string(&results).map_err(|e| format!("Failed to serialize results: {e}"))?;
let vars_json = serde_json::to_string(&exec_ctx.vars)
.map_err(|e| format!("Failed to serialize vars: {e}"))?;
let left_input = serde_json::json!({
"graph": graph_json,
"node_id": left_id,
"results": results_json,
"vars": vars_json,
"label": exec_ctx.label
})
.to_string();
let right_input = serde_json::json!({
"graph": graph_json,
"node_id": right_id,
"results": results_json,
"vars": vars_json,
"label": exec_ctx.label
})
.to_string();
// Build list of branch inputs
let mut branch_inputs = vec![left_input, right_input];
// Check for extra nodes (join3)
if let Some(config_str) = &node.query {
if let Ok(config) = serde_json::from_str::<serde_json::Value>(config_str) {
if let Some(extra_nodes) = config["extra_nodes"].as_array() {
for extra_node_val in extra_nodes {
if let Some(extra_id) = extra_node_val.as_str() {
let extra_input = serde_json::json!({
"graph": graph_json,
"node_id": extra_id,
"results": results_json,
"vars": vars_json,
"label": exec_ctx.label
})
.to_string();
branch_inputs.push(extra_input);
}
}
}
}
}
// Schedule sub-orchestrations and collect DurableFutures
let mut durable_futures = Vec::new();
for input in branch_inputs {
let fut = ctx.schedule_sub_orchestration(SUBTREE_NAME, input);
durable_futures.push(fut);
}
// Use ctx.join() - Duroxide's proper join method for parallel execution
let results_vec = ctx.join(durable_futures).await;
// Process results - join now returns Vec<Result<String, String>> directly.
// Each Ok value is a JSON envelope {"result": "...", "results": {...}} produced by
// execute_subtree; unwrap it and merge the branch's named results into the parent map.
let mut join_results: Vec<serde_json::Value> = Vec::new();
for (i, result) in results_vec.into_iter().enumerate() {
match result {
Ok(r) => {
let context = format!("JOIN branch {}", i + 1);
let branch_result = parse_subtree_envelope(&r, &context, results)?;
// Propagate break signals from any branch immediately
if is_break_signal(&branch_result) {
ctx.trace_info(format!(
"JOIN branch {} returned a break signal, propagating",
i + 1
));
return Ok(branch_result);
}
let parsed = serde_json::from_str::<serde_json::Value>(&branch_result)
.map_err(|e| format!("JOIN branch {} result parse error: {}", i + 1, e))?;
join_results.push(parsed);
}
Err(e) => {
return Err(format!("JOIN branch {} failed: {}", i + 1, e));
}
}
}
ctx.trace_info(format!(
"JOIN completed with {} results",
join_results.len()
));
let result = serde_json::to_string(&join_results).unwrap_or_else(|_| "[]".to_string());
// Store result if named
if let Some(name) = &node.result_name {
ctx.trace_info(format!("Storing JOIN result as ${name}"));
results.insert(name.clone(), result.clone());
}
Ok(result)
}
/// Collect all node IDs in the subtree rooted at `root_id` by depth-first traversal.
///
/// Handles the full node structure:
/// - `left_node` / `right_node` for THEN, IF, JOIN, RACE, LOOP
/// - `condition_node` embedded in `query` JSON for IF and LOOP nodes
/// - `extra_nodes` array embedded in `query` JSON for JOIN nodes (join3, etc.)
fn collect_subtree_node_ids(graph: &FunctionGraph, root_id: &str) -> Vec<String> {
let mut ids: Vec<String> = Vec::new();
let mut stack = vec![root_id.to_string()];
while let Some(node_id) = stack.pop() {
// Guard against visiting the same node twice (not expected in valid graphs,
// but prevents any potential infinite loop).
if ids.contains(&node_id) {
continue;
}
let Some(node) = graph.nodes.get(&node_id) else {
continue;
};
ids.push(node_id.clone());
// Follow structural children
if let Some(left) = &node.left_node {
stack.push(left.clone());
}
if let Some(right) = &node.right_node {
stack.push(right.clone());
}
// Follow children embedded in the query config JSON
if let Some(config_str) = &node.query {
if let Ok(config) = serde_json::from_str::<serde_json::Value>(config_str) {
// IF and LOOP: condition_node is a plain node ID string
if let Some(cond_id) = config["condition_node"].as_str() {
stack.push(cond_id.to_string());
}
// JOIN (join3, etc.): extra_nodes is an array of node ID strings
if let Some(extras) = config["extra_nodes"].as_array() {
for extra in extras {
if let Some(id) = extra.as_str() {
stack.push(id.to_string());
}
}
}
}
}
}
ids
}
async fn execute_race_node(
ctx: &OrchestrationContext,
graph: &FunctionGraph,
node: &FunctionNode,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
) -> Result<String, String> {
let left_id = node
.left_node
.as_ref()
.ok_or_else(|| format!("RACE node {node_id} has no left branch"))?;
let right_id = node
.right_node
.as_ref()
.ok_or_else(|| format!("RACE node {node_id} has no right branch"))?;
ctx.trace_info("Executing RACE branches in parallel (first wins)");
let graph_json =
serde_json::to_string(&graph).map_err(|e| format!("Failed to serialize graph: {e}"))?;
let results_json =
serde_json::to_string(&results).map_err(|e| format!("Failed to serialize results: {e}"))?;
let vars_json = serde_json::to_string(&exec_ctx.vars)
.map_err(|e| format!("Failed to serialize vars: {e}"))?;
let left_input = serde_json::json!({
"graph": graph_json,
"node_id": left_id,
"results": results_json,
"vars": vars_json,
"label": exec_ctx.label
})
.to_string();
let right_input = serde_json::json!({
"graph": graph_json,
"node_id": right_id,
"results": results_json,
"vars": vars_json,
"label": exec_ctx.label
})
.to_string();
// Schedule sub-orchestrations
let left_fut = ctx.schedule_sub_orchestration(SUBTREE_NAME, left_input);
let right_fut = ctx.schedule_sub_orchestration(SUBTREE_NAME, right_input);
// Use ctx.select2() - first to complete wins
// select2 now returns Either2<Left, Right> instead of (winner_idx, DurableOutput)
let (raw, loser_root_id) = match ctx.select2(left_fut, right_fut).await {
duroxide::Either2::First(Ok(r)) => {
ctx.trace_info("RACE completed - left branch won");
(Ok(r), right_id.clone())
}
duroxide::Either2::First(Err(e)) => (Err(format!("RACE left branch failed: {e}")), right_id.clone()),
duroxide::Either2::Second(Ok(r)) => {
ctx.trace_info("RACE completed - right branch won");
(Ok(r), left_id.clone())
}
duroxide::Either2::Second(Err(e)) => (Err(format!("RACE right branch failed: {e}")), left_id.clone()),
};
// Cancel all non-terminal nodes in the losing branch so that df.instance_nodes
// does not show ghost running/pending work after the race has been decided.
let loser_node_ids = collect_subtree_node_ids(graph, &loser_root_id);
if !loser_node_ids.is_empty() {
ctx.trace_info(format!(
"Cancelling {} losing-branch node(s) (root: {})",
loser_node_ids.len(),
loser_root_id
));
let cancel_input = serde_json::json!({ "node_ids": loser_node_ids });
// Best-effort: a failure here does not affect the race result but will
// leave losing-branch nodes in a non-terminal state. Log so operators
// can observe the problem without failing the workflow.
match ctx
.schedule_activity(
activities::cancel_subtree_nodes::NAME,
cancel_input.to_string(),
)
.await
{
Ok(_) => {}
Err(e) => ctx.trace_info(format!(
"Warning: failed to cancel losing-branch nodes (root: {}): {e}",
loser_root_id
)),
}
}
let raw = raw?;
// Parse the subtree output envelope produced by execute_subtree and merge any named
// results from the winning branch into the parent results map.
let result = parse_subtree_envelope(&raw, "RACE branch", results)?;
// Propagate break signals from the winning branch immediately
if is_break_signal(&result) {
ctx.trace_info("RACE winning branch returned a break signal, propagating");
return Ok(result);
}
// Store result if named
if let Some(name) = &node.result_name {
ctx.trace_info(format!("Storing RACE result as ${name}"));
results.insert(name.clone(), result.clone());
}
Ok(result)
}
async fn execute_http_node(
ctx: &OrchestrationContext,
node: &FunctionNode,
node_id: &str,
results: &mut HashMap<String, String>,
exec_ctx: &ExecutionContext,
sys_vars: &SystemVars,
) -> Result<String, String> {
let config_str = node
.query
.as_ref()
.ok_or_else(|| format!("HTTP node {node_id} has no config"))?;
// Parse config to substitute variables in body and URL
let mut config: serde_json::Value =
serde_json::from_str(config_str).map_err(|e| format!("Invalid HTTP config: {e}"))?;
// Substitute variables in body if present
if let Some(body) = config.get("body").and_then(|b| b.as_str()) {
let substituted_body = substitute_all_raw(body, results, &exec_ctx.vars, sys_vars)?;
config["body"] = serde_json::Value::String(substituted_body);
}
// Substitute variables in URL if present
if let Some(url) = config.get("url").and_then(|u| u.as_str()) {
let substituted_url = substitute_all_raw(url, results, &exec_ctx.vars, sys_vars)?;
config["url"] = serde_json::Value::String(substituted_url);
}