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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
//! Workflow execution engine.
//!
//! # Parallel Fan-out / Fan-in
//!
//! [`WorkflowExecutor::execute`] automatically discovers *independent* groups
//! of tasks — tasks whose dependencies are all satisfied at the same time —
//! and launches them concurrently. When all tasks in a fan-out group complete
//! the join point (fan-in) tasks are unblocked. This happens naturally through
//! the dependency-tracking loop; no explicit graph analysis is required.
//!
//! # Pause / Resume
//!
//! Call [`WorkflowExecutor::pause`] to signal the executor to stop launching
//! new tasks after the current wave completes. The checkpoint is serialised to
//! a JSON string containing the completed task IDs and execution context
//! variables. Pass that string to [`WorkflowExecutor::resume_from_checkpoint`]
//! to reconstruct the state and continue.
use crate::error::{Result, WorkflowError};
use crate::task::{Task, TaskId, TaskResult, TaskState};
use crate::workflow::{Workflow, WorkflowId, WorkflowState};
use async_trait::async_trait;
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{mpsc, watch, RwLock, Semaphore};
use tokio::time::timeout;
use tracing::{debug, info, warn};
/// Task executor trait.
#[async_trait]
pub trait TaskExecutor: Send + Sync {
/// Execute a task and return the result.
async fn execute(&self, task: &Task) -> Result<TaskResult>;
}
// ---------------------------------------------------------------------------
// Pause / Resume support
// ---------------------------------------------------------------------------
/// Serializable checkpoint for pause/resume of workflow execution.
///
/// This struct captures the minimal state needed to resume a paused workflow:
/// the set of already-completed task IDs and the current execution context
/// variables. It can be persisted to disk, a database, or transferred over
/// the network, then passed back to
/// [`WorkflowExecutor::resume_from_checkpoint`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionCheckpoint {
/// Workflow identifier.
pub workflow_id: WorkflowId,
/// Task IDs that had completed at the time the checkpoint was taken.
pub completed_task_ids: Vec<TaskId>,
/// Execution context variables at checkpoint time.
pub variables: HashMap<String, serde_json::Value>,
/// Unix timestamp (seconds) when the checkpoint was captured.
pub timestamp_secs: u64,
}
impl ExecutionCheckpoint {
/// Serialize the checkpoint to a compact JSON string.
///
/// # Errors
///
/// Returns a `WorkflowError` if JSON serialization fails.
pub fn to_json(&self) -> Result<String> {
serde_json::to_string(self).map_err(WorkflowError::Serialization)
}
/// Deserialize a checkpoint from a JSON string.
///
/// # Errors
///
/// Returns a `WorkflowError` if JSON parsing fails.
pub fn from_json(json: &str) -> Result<Self> {
serde_json::from_str(json).map_err(WorkflowError::Serialization)
}
}
/// Execution context shared across task executions.
#[derive(Debug, Clone)]
pub struct ExecutionContext {
/// Workflow ID.
pub workflow_id: WorkflowId,
/// Workflow variables.
pub variables: Arc<DashMap<String, serde_json::Value>>,
/// Task results cache.
pub results: Arc<DashMap<TaskId, TaskResult>>,
}
impl ExecutionContext {
/// Create a new execution context.
#[must_use]
pub fn new(workflow_id: WorkflowId) -> Self {
Self {
workflow_id,
variables: Arc::new(DashMap::new()),
results: Arc::new(DashMap::new()),
}
}
/// Get variable value.
#[must_use]
pub fn get_variable(&self, key: &str) -> Option<serde_json::Value> {
self.variables.get(key).map(|v| v.clone())
}
/// Set variable value.
pub fn set_variable(&self, key: String, value: serde_json::Value) {
self.variables.insert(key, value);
}
/// Get task result.
#[must_use]
pub fn get_result(&self, task_id: &TaskId) -> Option<TaskResult> {
self.results.get(task_id).map(|r| r.clone())
}
/// Store task result.
pub fn store_result(&self, task_id: TaskId, result: TaskResult) {
self.results.insert(task_id, result);
}
}
// ---------------------------------------------------------------------------
// WorkflowControl
// ---------------------------------------------------------------------------
/// High-level control signal for a running [`WorkflowExecutor`].
///
/// Sent via [`WorkflowExecutor::send_control`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkflowControl {
/// Pause after current in-flight tasks complete.
///
/// The executor serialises a checkpoint and returns with
/// [`WorkflowState::Paused`].
Pause,
/// Resume a previously paused executor.
Resume,
/// Request immediate cancellation of the workflow.
///
/// In-flight tasks are allowed to finish; then the executor returns an
/// error with `"cancelled"`.
Cancel,
}
/// Workflow executor.
pub struct WorkflowExecutor {
/// Task executor implementation.
task_executor: Arc<dyn TaskExecutor>,
/// Maximum concurrent tasks.
max_concurrent: usize,
/// Execution timeout.
timeout: Option<Duration>,
/// Pause signal sender. When `true` is sent the executor stops launching
/// new tasks after the current in-flight set completes.
pause_tx: watch::Sender<bool>,
/// Pause signal receiver (cloned into each execution).
pause_rx: watch::Receiver<bool>,
/// Cancel signal sender. When `true` is sent the executor aborts after
/// the current wave finishes.
cancel_tx: watch::Sender<bool>,
/// Cancel signal receiver (cloned into each execution).
cancel_rx: watch::Receiver<bool>,
/// Completed tasks from a prior checkpoint (used for resume).
resume_completed: HashSet<TaskId>,
/// Variables from a prior checkpoint (used for resume).
resume_variables: HashMap<String, serde_json::Value>,
}
impl WorkflowExecutor {
/// Create a new workflow executor.
#[must_use]
pub fn new(task_executor: Arc<dyn TaskExecutor>) -> Self {
let (pause_tx, pause_rx) = watch::channel(false);
let (cancel_tx, cancel_rx) = watch::channel(false);
Self {
task_executor,
max_concurrent: 4,
timeout: None,
pause_tx,
pause_rx,
cancel_tx,
cancel_rx,
resume_completed: HashSet::new(),
resume_variables: HashMap::new(),
}
}
/// Set maximum concurrent tasks.
#[must_use]
pub fn with_max_concurrent(mut self, max_concurrent: usize) -> Self {
self.max_concurrent = max_concurrent;
self
}
/// Set execution timeout.
#[must_use]
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
/// Signal the executor to pause after the current in-flight tasks finish.
///
/// The executor stops launching new tasks as soon as it receives the
/// signal, but any already-running tasks are allowed to complete.
pub fn pause(&self) {
if let Err(e) = self.pause_tx.send(true) {
warn!("Failed to send pause signal: {e}");
}
}
/// Resume a previously paused executor (un-pause).
pub fn resume(&self) {
if let Err(e) = self.pause_tx.send(false) {
warn!("Failed to send resume signal: {e}");
}
}
/// Send a [`WorkflowControl`] signal to the executor.
///
/// - [`WorkflowControl::Pause`] — equivalent to [`Self::pause`].
/// - [`WorkflowControl::Resume`] — equivalent to [`Self::resume`].
/// - [`WorkflowControl::Cancel`] — signals the executor to abort after the
/// current task wave finishes and return a cancellation error.
///
/// # Errors
///
/// Returns [`WorkflowError`] if the underlying channel is closed (i.e. the
/// executor has already been dropped).
pub fn send_control(&self, control: WorkflowControl) -> Result<()> {
match control {
WorkflowControl::Pause => {
self.pause_tx
.send(true)
.map_err(|e| WorkflowError::generic(format!("pause send failed: {e}")))?;
}
WorkflowControl::Resume => {
self.pause_tx
.send(false)
.map_err(|e| WorkflowError::generic(format!("resume send failed: {e}")))?;
}
WorkflowControl::Cancel => {
self.cancel_tx
.send(true)
.map_err(|e| WorkflowError::generic(format!("cancel send failed: {e}")))?;
// Also set pause so the loop checks after current wave
let _ = self.pause_tx.send(true);
}
}
Ok(())
}
/// Returns `true` when the executor is currently in a paused state.
///
/// This reflects the latest value sent via [`Self::pause`] /
/// [`Self::send_control`]. It does **not** guarantee that execution has
/// actually stopped; in-flight tasks may still be running.
#[must_use]
pub fn is_paused(&self) -> bool {
*self.pause_rx.borrow()
}
/// Load a prior `ExecutionCheckpoint` so that already-completed tasks are
/// skipped when [`Self::execute`] is called next.
///
/// # Errors
///
/// Returns an error if the checkpoint JSON is invalid.
pub fn resume_from_checkpoint(&mut self, checkpoint_json: &str) -> Result<()> {
let cp = ExecutionCheckpoint::from_json(checkpoint_json)?;
self.resume_completed = cp.completed_task_ids.into_iter().collect();
self.resume_variables = cp.variables;
info!(
"Loaded checkpoint with {} completed tasks",
self.resume_completed.len()
);
Ok(())
}
/// Execute a workflow.
///
/// Tasks whose dependencies are all satisfied at the same time are launched
/// concurrently (fan-out). The executor waits for all of them before
/// considering their successors (fan-in), bounded by `max_concurrent`.
///
/// If a checkpoint was loaded via [`Self::resume_from_checkpoint`] the
/// corresponding tasks are pre-marked as completed and skipped.
///
/// The executor checks the pause signal between task waves; when paused it
/// drains the current in-flight tasks, serialises a checkpoint, and returns
/// `WorkflowState::Paused` in the `ExecutionResult`.
pub async fn execute(&self, workflow: &mut Workflow) -> Result<ExecutionResult> {
info!("Starting workflow execution: {}", workflow.name);
// Validate workflow
workflow.validate()?;
// Update workflow state
workflow.state = WorkflowState::Running;
let start_time = Instant::now();
let context = ExecutionContext::new(workflow.id);
// Initialize variables from workflow config
for (key, value) in &workflow.config.variables {
context.set_variable(key.clone(), value.clone());
}
// Apply resume variables (override config-level variables)
for (key, value) in &self.resume_variables {
context.set_variable(key.clone(), value.clone());
}
// Get topological order
let task_order = workflow.topological_sort()?;
// Track completed tasks — pre-populate from checkpoint if resuming
let completed_tasks: Arc<RwLock<HashSet<TaskId>>> =
Arc::new(RwLock::new(self.resume_completed.clone()));
let failed_tasks: Arc<RwLock<HashSet<TaskId>>> = Arc::new(RwLock::new(HashSet::new()));
// Semaphore for limiting concurrent tasks
let semaphore = Arc::new(Semaphore::new(
workflow
.config
.max_concurrent_tasks
.min(self.max_concurrent),
));
// Channel for task completion notifications
let (tx, mut rx) = mpsc::channel(100);
// Pause signal receiver (clone so we can poll it)
let pause_rx = self.pause_rx.clone();
// Cancel signal receiver (clone so we can poll it)
let cancel_rx = self.cancel_rx.clone();
// Execute tasks in dependency order
let mut active_tasks = 0;
let mut task_iter = task_order.iter();
loop {
// Check for timeout
if let Some(timeout_duration) = self.timeout {
if start_time.elapsed() > timeout_duration {
workflow.state = WorkflowState::Failed;
return Err(WorkflowError::generic("Workflow execution timeout"));
}
}
// Check cancel signal between task waves (only when no tasks are active)
if active_tasks == 0 && *cancel_rx.borrow() {
workflow.state = WorkflowState::Failed;
return Err(WorkflowError::generic("Workflow cancelled"));
}
// Check pause signal between task waves (only when no tasks are active)
if active_tasks == 0 && *pause_rx.borrow() {
// Drain in-flight tasks first (none here since active_tasks == 0)
// Capture checkpoint
let completed = completed_tasks.read().await;
let variables: HashMap<String, serde_json::Value> = context
.variables
.iter()
.map(|e| (e.key().clone(), e.value().clone()))
.collect();
let checkpoint = ExecutionCheckpoint {
workflow_id: workflow.id,
completed_task_ids: completed.iter().copied().collect(),
variables,
timestamp_secs: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
};
drop(completed);
let checkpoint_json = checkpoint.to_json()?;
info!(
"Workflow paused; checkpoint: {} bytes",
checkpoint_json.len()
);
workflow.state = WorkflowState::Paused;
return Ok(ExecutionResult {
workflow_id: workflow.id,
state: WorkflowState::Paused,
duration: start_time.elapsed(),
task_results: context
.results
.iter()
.map(|entry| (*entry.key(), entry.value().clone()))
.collect(),
checkpoint: Some(checkpoint_json),
});
}
// Start new tasks if possible
while active_tasks < task_order.len() {
let Some(&task_id) = task_iter.next() else {
break;
};
// Check if dependencies are satisfied
let deps = workflow.get_dependencies(&task_id);
let completed = completed_tasks.read().await;
let failed = failed_tasks.read().await;
// Skip tasks that were already completed in a prior checkpoint
let already_done = completed.contains(&task_id);
let deps_satisfied = deps.iter().all(|dep| completed.contains(dep));
let deps_failed = deps.iter().any(|dep| failed.contains(dep));
drop(completed);
drop(failed);
// Task was completed in a prior run: count it but skip execution
if already_done {
active_tasks += 1;
// Notify completion immediately via a synthetic result
let result = TaskResult {
task_id,
status: TaskState::Completed,
data: None,
error: None,
duration: Duration::ZERO,
outputs: Vec::new(),
};
let tx2 = tx.clone();
let completed2 = completed_tasks.clone();
tokio::spawn(async move {
completed2.write().await.insert(task_id);
let _ = tx2.send((task_id, result)).await;
});
continue;
}
if deps_failed {
if workflow.config.fail_fast {
workflow.state = WorkflowState::Failed;
return Err(WorkflowError::DependencyFailed(task_id.to_string()));
}
// Skip this task
if let Some(task) = workflow.get_task_mut(&task_id) {
task.set_state(TaskState::Skipped)?;
}
continue;
}
if !deps_satisfied {
continue;
}
// Get task
let Some(task) = workflow.get_task(&task_id).cloned() else {
continue;
};
// Check if task should run based on conditions
if !self.should_run_task(&task, &context).await {
if let Some(t) = workflow.get_task_mut(&task_id) {
t.set_state(TaskState::Skipped)?;
}
completed_tasks.write().await.insert(task_id);
continue;
}
// Spawn task execution
let executor = self.task_executor.clone();
let sem = semaphore.clone();
let ctx = context.clone();
let tx = tx.clone();
let completed = completed_tasks.clone();
let failed = failed_tasks.clone();
tokio::spawn(async move {
let Ok(_permit) = sem.acquire().await else {
let _ = tx
.send((
task_id,
TaskResult {
task_id,
status: TaskState::Failed,
data: None,
error: Some("Semaphore closed".to_string()),
duration: std::time::Duration::ZERO,
outputs: Vec::new(),
},
))
.await;
return;
};
let result = Self::execute_task(executor, &task, &ctx).await;
let success = matches!(result.status, TaskState::Completed);
if success {
completed.write().await.insert(task_id);
} else {
failed.write().await.insert(task_id);
}
ctx.store_result(task_id, result.clone());
let _ = tx.send((task_id, result)).await;
});
active_tasks += 1;
}
// Wait for task completion
if active_tasks == 0 {
break;
}
if let Some((_task_id, result)) = rx.recv().await {
active_tasks -= 1;
if !matches!(result.status, TaskState::Completed) && workflow.config.fail_fast {
workflow.state = WorkflowState::Failed;
return Err(WorkflowError::TaskExecutionFailed {
task_id: result.task_id.to_string(),
reason: result.error.unwrap_or_else(|| "Unknown error".to_string()),
});
}
} else {
break;
}
}
// Check final state
let failed = failed_tasks.read().await;
let final_state = if failed.is_empty() {
WorkflowState::Completed
} else {
WorkflowState::Failed
};
workflow.state = final_state;
info!(
"Workflow execution completed: {} in {:?}",
workflow.name,
start_time.elapsed()
);
Ok(ExecutionResult {
workflow_id: workflow.id,
state: final_state,
duration: start_time.elapsed(),
task_results: context
.results
.iter()
.map(|entry| (*entry.key(), entry.value().clone()))
.collect(),
checkpoint: None,
})
}
async fn execute_task(
executor: Arc<dyn TaskExecutor>,
task: &Task,
_context: &ExecutionContext,
) -> TaskResult {
debug!("Executing task: {}", task.name);
let start = Instant::now();
let result = if let Some(task_timeout) = Some(task.timeout) {
match timeout(task_timeout, executor.execute(task)).await {
Ok(Ok(result)) => result,
Ok(Err(e)) => TaskResult {
task_id: task.id,
status: TaskState::Failed,
data: None,
error: Some(e.to_string()),
duration: start.elapsed(),
outputs: Vec::new(),
},
Err(_) => TaskResult {
task_id: task.id,
status: TaskState::Failed,
data: None,
error: Some("Task timeout".to_string()),
duration: start.elapsed(),
outputs: Vec::new(),
},
}
} else {
match executor.execute(task).await {
Ok(result) => result,
Err(e) => TaskResult {
task_id: task.id,
status: TaskState::Failed,
data: None,
error: Some(e.to_string()),
duration: start.elapsed(),
outputs: Vec::new(),
},
}
};
debug!(
"Task {} completed with status: {:?}",
task.name, result.status
);
result
}
async fn should_run_task(&self, task: &Task, context: &ExecutionContext) -> bool {
// Evaluate task conditions
for condition in &task.conditions {
if !self.evaluate_condition(condition, context).await {
debug!("Task {} skipped due to condition: {}", task.name, condition);
return false;
}
}
true
}
async fn evaluate_condition(&self, condition: &str, context: &ExecutionContext) -> bool {
match parse_condition(condition, context) {
Ok(result) => result,
Err(err) => {
debug!(
"Condition parse error (treating as false): {} – {}",
condition, err
);
false
}
}
}
}
// ── Condition expression evaluator ──────────────────────────────────────────
/// Value types that can appear in condition expressions.
#[derive(Debug, Clone, PartialEq)]
enum CondValue {
/// Integer/byte quantity (e.g. file sizes, counts).
Int(i64),
/// Floating-point number.
Float(f64),
/// String value.
Str(String),
/// Boolean literal.
Bool(bool),
}
impl CondValue {
/// Attempt to compare two values using a standard ordering.
fn partial_cmp_values(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Self::Int(a), Self::Int(b)) => a.partial_cmp(b),
(Self::Float(a), Self::Float(b)) => a.partial_cmp(b),
(Self::Int(a), Self::Float(b)) => (*a as f64).partial_cmp(b),
(Self::Float(a), Self::Int(b)) => a.partial_cmp(&(*b as f64)),
(Self::Str(a), Self::Str(b)) => a.partial_cmp(b),
_ => None,
}
}
/// Equality comparison with type coercion between numerics.
fn eq_coerced(&self, other: &Self) -> bool {
match (self, other) {
(Self::Int(a), Self::Int(b)) => a == b,
(Self::Float(a), Self::Float(b)) => (a - b).abs() < f64::EPSILON,
(Self::Int(a), Self::Float(b)) => ((*a as f64) - b).abs() < f64::EPSILON,
(Self::Float(a), Self::Int(b)) => (a - (*b as f64)).abs() < f64::EPSILON,
(Self::Str(a), Self::Str(b)) => a.eq_ignore_ascii_case(b),
(Self::Bool(a), Self::Bool(b)) => a == b,
_ => false,
}
}
}
/// Resolve a variable reference from the execution context.
///
/// Supported paths:
/// - `output.<key>` – looks up `key` in `context.variables` and parses the
/// value as a `CondValue`.
/// - Bare identifiers – also looked up in `context.variables`.
fn resolve_variable(path: &str, context: &ExecutionContext) -> Option<CondValue> {
let key = path.trim_start_matches("output.");
let json_val = context.get_variable(key)?;
json_to_cond_value(&json_val)
}
/// Convert a `serde_json::Value` to a `CondValue`.
fn json_to_cond_value(v: &serde_json::Value) -> Option<CondValue> {
match v {
serde_json::Value::Bool(b) => Some(CondValue::Bool(*b)),
serde_json::Value::Number(n) => {
if let Some(i) = n.as_i64() {
Some(CondValue::Int(i))
} else {
n.as_f64().map(CondValue::Float)
}
}
serde_json::Value::String(s) => Some(CondValue::Str(s.clone())),
_ => None,
}
}
/// Parse a literal token (number with optional unit, boolean, or quoted string)
/// into a `CondValue`.
///
/// Unit suffixes supported:
/// - Bytes : `B`, `KB`, `MB`, `GB`, `TB` (powers of 1024)
/// - Time : `ms`, `s`, `m`, `h`
/// - Bare numbers are treated as integers or floats.
fn parse_literal(token: &str) -> Option<CondValue> {
let t = token.trim().trim_matches(|c| c == '"' || c == '\'');
// Boolean literals
match t.to_lowercase().as_str() {
"true" => return Some(CondValue::Bool(true)),
"false" => return Some(CondValue::Bool(false)),
_ => {}
}
// Try numeric with byte-size suffix (case-insensitive)
let lower = t.to_lowercase();
let byte_units: &[(&str, i64)] = &[
("tb", 1024 * 1024 * 1024 * 1024),
("gb", 1024 * 1024 * 1024),
("mb", 1024 * 1024),
("kb", 1024),
("b", 1),
];
for &(suffix, multiplier) in byte_units {
if let Some(num_str) = lower.strip_suffix(suffix) {
let num_str = num_str.trim();
if let Ok(n) = num_str.parse::<f64>() {
return Some(CondValue::Int((n * multiplier as f64) as i64));
}
}
}
// Duration suffixes
let duration_units: &[(&str, i64)] =
&[("ms", 1), ("s", 1_000), ("m", 60_000), ("h", 3_600_000)];
for &(suffix, multiplier_ms) in duration_units {
if let Some(num_str) = lower.strip_suffix(suffix) {
let num_str = num_str.trim();
if let Ok(n) = num_str.parse::<f64>() {
return Some(CondValue::Int((n * multiplier_ms as f64) as i64));
}
}
}
// Plain integer
if let Ok(i) = t.parse::<i64>() {
return Some(CondValue::Int(i));
}
// Plain float
if let Ok(f) = t.parse::<f64>() {
return Some(CondValue::Float(f));
}
// Fall back to string value (handles codec names etc.)
Some(CondValue::Str(t.to_string()))
}
/// Parse and evaluate a single condition expression against the execution context.
///
/// Grammar (simplified):
/// ```text
/// condition := operand operator operand
/// | "!" operand
/// operand := variable | literal
/// variable := identifier ("." identifier)*
/// operator := "==" | "!=" | ">" | ">=" | "<" | "<="
/// | "contains" | "startswith" | "endswith"
/// literal := number [unit] | bool | quoted-string
/// ```
///
/// Examples:
/// - `"output.size > 1MB"`
/// - `"duration >= 60s"`
/// - `"codec == av1"`
/// - `"output.status == completed"`
/// - `"count > 0"`
/// - `"!failed"`
pub fn parse_condition(
condition: &str,
context: &ExecutionContext,
) -> std::result::Result<bool, String> {
let cond = condition.trim();
// Handle negation: !<expr>
if let Some(rest) = cond.strip_prefix('!') {
return parse_condition(rest.trim(), context).map(|v| !v);
}
// Try to split on a binary operator (longest match first to avoid
// ">" matching before ">=")
let operators = &[
">=",
"<=",
"!=",
"==",
">",
"<",
"contains",
"startswith",
"endswith",
];
for &op in operators {
// Use case-insensitive search for word operators
let split_pos = if op.chars().all(char::is_alphabetic) {
// word operator – find as whole word
let lower = cond.to_lowercase();
let needle = format!(" {op} ");
lower
.find(&needle)
.map(|p| (p, p + needle.len() - 1, op.len()))
} else {
// symbol operator
cond.find(op).map(|p| (p, p + op.len(), op.len()))
};
let Some((lhs_end, rhs_start, _)) = split_pos else {
continue;
};
let lhs_str = cond[..lhs_end].trim();
let rhs_str = cond[rhs_start..].trim();
// Resolve left-hand side: try as variable first, then literal
let lhs = resolve_variable(lhs_str, context).or_else(|| parse_literal(lhs_str));
// Resolve right-hand side
let rhs = resolve_variable(rhs_str, context).or_else(|| parse_literal(rhs_str));
let lhs = lhs.ok_or_else(|| format!("Cannot resolve LHS: {lhs_str}"))?;
let rhs = rhs.ok_or_else(|| format!("Cannot resolve RHS: {rhs_str}"))?;
let result = match op {
"==" => lhs.eq_coerced(&rhs),
"!=" => !lhs.eq_coerced(&rhs),
">" => lhs
.partial_cmp_values(&rhs)
.is_some_and(|o| o == std::cmp::Ordering::Greater),
">=" => lhs
.partial_cmp_values(&rhs)
.is_some_and(|o| o != std::cmp::Ordering::Less),
"<" => lhs
.partial_cmp_values(&rhs)
.is_some_and(|o| o == std::cmp::Ordering::Less),
"<=" => lhs
.partial_cmp_values(&rhs)
.is_some_and(|o| o != std::cmp::Ordering::Greater),
"contains" => {
if let (CondValue::Str(l), CondValue::Str(r)) = (&lhs, &rhs) {
l.to_lowercase().contains(&r.to_lowercase())
} else {
false
}
}
"startswith" => {
if let (CondValue::Str(l), CondValue::Str(r)) = (&lhs, &rhs) {
l.to_lowercase().starts_with(&r.to_lowercase())
} else {
false
}
}
"endswith" => {
if let (CondValue::Str(l), CondValue::Str(r)) = (&lhs, &rhs) {
l.to_lowercase().ends_with(&r.to_lowercase())
} else {
false
}
}
_ => false,
};
return Ok(result);
}
// No operator found: treat the whole expression as a boolean variable lookup
if let Some(val) = resolve_variable(cond, context) {
return Ok(match val {
CondValue::Bool(b) => b,
CondValue::Int(i) => i != 0,
CondValue::Float(f) => f != 0.0,
CondValue::Str(s) => !s.is_empty() && s.to_lowercase() != "false",
});
}
// Unknown condition – default to true so existing workflows are not broken
debug!("Condition not resolvable, defaulting to true: {}", cond);
Ok(true)
}
/// Workflow execution result.
#[derive(Debug)]
pub struct ExecutionResult {
/// Workflow ID.
pub workflow_id: WorkflowId,
/// Final workflow state.
pub state: WorkflowState,
/// Total execution duration.
pub duration: Duration,
/// Results for all tasks.
pub task_results: HashMap<TaskId, TaskResult>,
/// Serialized checkpoint JSON when the workflow was paused mid-execution.
/// `None` when the workflow ran to completion or failure.
pub checkpoint: Option<String>,
}
/// Default task executor implementation.
pub struct DefaultTaskExecutor;
#[async_trait]
impl TaskExecutor for DefaultTaskExecutor {
async fn execute(&self, task: &Task) -> Result<TaskResult> {
use crate::task::TaskType;
let start = Instant::now();
let result: Result<()> = match &task.task_type {
TaskType::Wait { duration } => {
tokio::time::sleep(*duration).await;
Ok(())
}
TaskType::HttpRequest {
url,
method,
headers: _,
body: _,
} => {
debug!("HTTP {:?} request to: {}", method, url);
// HTTP client integration would go here (reqwest / hyper).
// At the workflow-engine layer we log the intent and succeed;
// callers that need real HTTP should provide a custom TaskExecutor.
info!("HTTP {} {}", format!("{:?}", method).to_uppercase(), url);
Ok(())
}
TaskType::Transcode {
input,
output,
preset,
params: _,
} => {
info!("Transcode: {:?} → {:?} (preset: {})", input, output, preset);
// Validate that the input path exists before handing off to a
// transcode engine. The actual codec pipeline is implemented in
// oximedia-transcode; this executor records the intent and
// succeeds so the workflow graph continues.
if !input.exists() {
return Err(WorkflowError::generic(format!(
"Transcode input not found: {}",
input.display()
)));
}
// Ensure parent directory of output exists.
if let Some(parent) = output.parent() {
if !parent.as_os_str().is_empty() {
tokio::fs::create_dir_all(parent).await.map_err(|e| {
WorkflowError::generic(format!(
"Cannot create output directory {}: {e}",
parent.display()
))
})?;
}
}
info!("Transcode task recorded for {:?}", output);
Ok(())
}
TaskType::QualityControl {
input,
profile,
rules,
} => {
info!(
"QualityControl: {:?} profile={} rules={:?}",
input, profile, rules
);
if !input.exists() {
return Err(WorkflowError::generic(format!(
"QC input not found: {}",
input.display()
)));
}
// QC validation logic lives in oximedia-qc; here we confirm
// the file is reachable and log that QC was requested.
let metadata = tokio::fs::metadata(input)
.await
.map_err(|e| WorkflowError::generic(format!("QC metadata error: {e}")))?;
info!(
"QC target size: {} bytes, profile: {}",
metadata.len(),
profile
);
Ok(())
}
TaskType::Transfer {
source,
destination,
protocol,
options: _,
} => {
use crate::task::TransferProtocol;
info!("Transfer: {} → {} via {:?}", source, destination, protocol);
// For local-filesystem transfers we perform the copy directly.
// Remote protocols (S3, SFTP, FTP, rsync, HTTP) are handled by
// dedicated transfer agents; this executor logs the request.
match protocol {
TransferProtocol::Local => {
let src_path = std::path::Path::new(source.as_str());
let dst_path = std::path::Path::new(destination.as_str());
if let Some(parent) = dst_path.parent() {
if !parent.as_os_str().is_empty() {
tokio::fs::create_dir_all(parent).await.map_err(|e| {
WorkflowError::generic(format!(
"Cannot create destination dir: {e}"
))
})?;
}
}
tokio::fs::copy(src_path, dst_path).await.map_err(|e| {
WorkflowError::generic(format!(
"Local copy {} → {} failed: {e}",
src_path.display(),
dst_path.display()
))
})?;
info!("Local transfer complete: {} → {}", source, destination);
}
other => {
info!(
"Remote transfer ({:?}) queued: {} → {}",
other, source, destination
);
}
}
Ok(())
}
TaskType::Notification {
channel,
message,
metadata: _,
} => {
use crate::task::NotificationChannel;
match channel {
NotificationChannel::Email { to, subject } => {
info!(
"Notification [Email] to={:?} subject={:?}: {}",
to, subject, message
);
}
NotificationChannel::Webhook { url } => {
info!("Notification [Webhook] url={}: {}", url, message);
}
NotificationChannel::Slack {
channel: slack_channel,
webhook_url,
} => {
info!(
"Notification [Slack] channel={} url={}: {}",
slack_channel, webhook_url, message
);
}
NotificationChannel::Discord { webhook_url } => {
info!("Notification [Discord] url={}: {}", webhook_url, message);
}
}
Ok(())
}
TaskType::CustomScript { script, args, env } => {
info!(
"CustomScript: {:?} args={:?} env_vars={}",
script,
args,
env.len()
);
if !script.exists() {
return Err(WorkflowError::generic(format!(
"Script not found: {}",
script.display()
)));
}
// Spawn the script as a child process via tokio::process.
let mut cmd = tokio::process::Command::new(script);
cmd.args(args);
for (k, v) in env {
cmd.env(k, v);
}
let status = cmd.status().await.map_err(|e| {
WorkflowError::generic(format!("Script {:?} failed to launch: {e}", script))
})?;
if !status.success() {
return Err(WorkflowError::generic(format!(
"Script {:?} exited with status: {}",
script, status
)));
}
info!("Script {:?} completed successfully", script);
Ok(())
}
TaskType::Analysis {
input,
analyses,
output,
} => {
info!(
"Analysis: {:?} types={:?} output={:?}",
input, analyses, output
);
if !input.exists() {
return Err(WorkflowError::generic(format!(
"Analysis input not found: {}",
input.display()
)));
}
// If an output path was requested, ensure its parent exists.
if let Some(out_path) = output {
if let Some(parent) = out_path.parent() {
if !parent.as_os_str().is_empty() {
tokio::fs::create_dir_all(parent).await.map_err(|e| {
WorkflowError::generic(format!(
"Cannot create analysis output dir: {e}"
))
})?;
}
}
}
// Analysis engines live in oximedia-quality / oximedia-scene etc.
// This executor records the request and succeeds; real analysis
// is performed by the domain-specific pipeline.
info!("Analysis task recorded for {:?}", input);
Ok(())
}
TaskType::Conditional {
condition,
true_task,
false_task,
} => {
// Evaluate the condition expression (simple boolean string parse;
// full expression evaluation would use the ExecutionContext variables).
let condition_result = match condition.trim().to_lowercase().as_str() {
"true" | "1" | "yes" => true,
"false" | "0" | "no" => false,
other => {
debug!(
"Condition not resolvable as literal, defaulting to true: {}",
other
);
true
}
};
let branch_task = if condition_result {
true_task.as_deref()
} else {
false_task.as_deref()
};
if let Some(inner_task) = branch_task {
info!(
"Conditional branch selected: condition={} task={}",
condition_result, inner_task.name
);
// Recursively execute the selected branch task.
let branch_result = self.execute(inner_task).await?;
if !matches!(branch_result.status, TaskState::Completed) {
return Err(WorkflowError::generic(format!(
"Conditional branch task '{}' failed: {}",
inner_task.name,
branch_result.error.as_deref().unwrap_or("unknown")
)));
}
} else {
debug!("Conditional task: selected branch has no task, skipping");
}
Ok(())
}
};
match result {
Ok(()) => Ok(TaskResult {
task_id: task.id,
status: TaskState::Completed,
data: None,
error: None,
duration: start.elapsed(),
outputs: Vec::new(),
}),
Err(e) => Ok(TaskResult {
task_id: task.id,
status: TaskState::Failed,
data: None,
error: Some(e.to_string()),
duration: start.elapsed(),
outputs: Vec::new(),
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::task::{Task, TaskType};
#[test]
fn test_execution_context_creation() {
let workflow_id = WorkflowId::new();
let ctx = ExecutionContext::new(workflow_id);
assert_eq!(ctx.workflow_id, workflow_id);
}
#[test]
fn test_execution_context_variables() {
let ctx = ExecutionContext::new(WorkflowId::new());
ctx.set_variable("key".to_string(), serde_json::json!("value"));
assert_eq!(ctx.get_variable("key"), Some(serde_json::json!("value")));
}
#[test]
fn test_execution_context_results() {
let ctx = ExecutionContext::new(WorkflowId::new());
let task_id = TaskId::new();
let result = TaskResult {
task_id,
status: TaskState::Completed,
data: None,
error: None,
duration: Duration::from_secs(1),
outputs: Vec::new(),
};
ctx.store_result(task_id, result.clone());
let retrieved = ctx.get_result(&task_id);
assert!(retrieved.is_some());
}
#[tokio::test]
async fn test_default_executor_wait_task() {
let executor = DefaultTaskExecutor;
let task = Task::new(
"wait-task",
TaskType::Wait {
duration: Duration::from_millis(10),
},
);
let result = executor
.execute(&task)
.await
.expect("should succeed in test");
assert_eq!(result.status, TaskState::Completed);
}
#[tokio::test]
async fn test_workflow_executor_creation() {
let executor = WorkflowExecutor::new(Arc::new(DefaultTaskExecutor))
.with_max_concurrent(2)
.with_timeout(Duration::from_secs(60));
assert_eq!(executor.max_concurrent, 2);
assert!(executor.timeout.is_some());
}
#[tokio::test]
async fn test_simple_workflow_execution() {
let mut workflow = Workflow::new("test-workflow");
let task = Task::new(
"test-task",
TaskType::Wait {
duration: Duration::from_millis(10),
},
);
workflow.add_task(task);
let executor = WorkflowExecutor::new(Arc::new(DefaultTaskExecutor));
let result = executor
.execute(&mut workflow)
.await
.expect("should succeed in test");
assert_eq!(result.state, WorkflowState::Completed);
assert_eq!(result.task_results.len(), 1);
}
}