oximedia-workflow 0.1.2

Comprehensive workflow orchestration engine for OxiMedia
Documentation
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
//! Parallel and sequential step execution within workflow stages.
//!
//! A [`WorkflowStage`] is a logical grouping of [`WorkflowStep`]s that either
//! run **in parallel** (all steps spawned concurrently via `std::thread::scope`)
//! or **sequentially** (each step blocks the next).
//!
//! # Example
//!
//! ```rust
//! use oximedia_workflow::parallel_steps::{
//!     ParallelSteps, StepType, WorkflowStage, WorkflowStep,
//! };
//!
//! let mut parallel = ParallelSteps::new("encode-all");
//! parallel.add_step(WorkflowStep::new("pass-1", StepType::Compute { value: 1 }));
//! parallel.add_step(WorkflowStep::new("pass-2", StepType::Compute { value: 2 }));
//! let results = parallel.execute_parallel().expect("all steps should succeed");
//! assert_eq!(results.len(), 2);
//! ```

use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

// ---------------------------------------------------------------------------
// Step types
// ---------------------------------------------------------------------------

/// The unit of work a [`WorkflowStep`] performs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StepType {
    /// Produce an integer value (useful for testing).
    Compute {
        /// The integer to return as output.
        value: i64,
    },
    /// Fail intentionally with a reason string.
    Fail {
        /// Human-readable failure reason.
        reason: String,
    },
    /// Sleep for a duration (simulates I/O-bound work).
    Wait {
        /// Duration to sleep, in milliseconds.
        duration_ms: u64,
    },
    /// Multiply `value` by `factor` and return the product.
    Transform {
        /// Input value.
        value: i64,
        /// Multiplication factor.
        factor: i64,
    },
}

// ---------------------------------------------------------------------------
// WorkflowStep
// ---------------------------------------------------------------------------

/// A single unit of work inside a [`WorkflowStage`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowStep {
    /// Human-readable step name.
    pub name: String,
    /// What this step does.
    pub step_type: StepType,
    /// Optional per-step timeout.  When `Some(ms)`, a `Wait` step that
    /// exceeds the timeout is marked as timed-out.
    pub timeout_ms: Option<u64>,
}

impl WorkflowStep {
    /// Create a step with no timeout.
    #[must_use]
    pub fn new(name: impl Into<String>, step_type: StepType) -> Self {
        Self {
            name: name.into(),
            step_type,
            timeout_ms: None,
        }
    }

    /// Set an optional timeout (milliseconds).
    #[must_use]
    pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
        self.timeout_ms = Some(timeout_ms);
        self
    }
}

// ---------------------------------------------------------------------------
// StepResult
// ---------------------------------------------------------------------------

/// The outcome of executing a single [`WorkflowStep`].
#[derive(Debug, Clone)]
pub struct StepResult {
    /// Name of the step that produced this result.
    pub name: String,
    /// Whether the step completed without error.
    pub success: bool,
    /// Integer output, if the step produced one.
    pub output: Option<i64>,
    /// Error message, if the step failed.
    pub error: Option<String>,
    /// Approximate wall-clock time the step took, in milliseconds.
    pub duration_ms: u64,
}

// ---------------------------------------------------------------------------
// ParallelStepError
// ---------------------------------------------------------------------------

/// Errors from parallel (or sequential) step execution.
#[derive(Debug, thiserror::Error)]
pub enum ParallelStepError {
    /// A single named step failed.
    #[error("Step '{name}' failed: {reason}")]
    StepFailed {
        /// Name of the failing step.
        name: String,
        /// Failure reason.
        reason: String,
    },
    /// Multiple steps failed (reported when `fail_fast = false`).
    #[error("{count} of {total} steps failed")]
    MultipleStepsFailed {
        /// Number of failed steps.
        count: usize,
        /// Total number of steps in the group.
        total: usize,
        /// Individual step results (both successes and failures).
        results: Vec<StepResult>,
    },
    /// A step exceeded its per-step timeout.
    #[error("Step timed out: {name}")]
    Timeout {
        /// Name of the step that timed out.
        name: String,
    },
}

// ---------------------------------------------------------------------------
// Step execution
// ---------------------------------------------------------------------------

/// Execute a single step and return a [`StepResult`].
///
/// This function is `Send + Sync`-safe and is designed to be called from
/// `std::thread::scope` worker threads.
pub fn execute_step(step: &WorkflowStep) -> StepResult {
    let start = Instant::now();

    let (success, output, error) = match &step.step_type {
        StepType::Compute { value } => (true, Some(*value), None),

        StepType::Fail { reason } => (false, None, Some(reason.clone())),

        StepType::Wait { duration_ms } => {
            let wait = Duration::from_millis(*duration_ms);
            // Honour per-step timeout if set.
            let timed_out = if let Some(limit_ms) = step.timeout_ms {
                *duration_ms > limit_ms
            } else {
                false
            };

            if timed_out {
                let elapsed = start.elapsed().as_millis() as u64;
                return StepResult {
                    name: step.name.clone(),
                    success: false,
                    output: None,
                    error: Some(format!("step '{}' timed out", step.name)),
                    duration_ms: elapsed,
                };
            }

            std::thread::sleep(wait);
            (true, None, None)
        }

        StepType::Transform { value, factor } => {
            let result = value.saturating_mul(*factor);
            (true, Some(result), None)
        }
    };

    let duration_ms = start.elapsed().as_millis() as u64;
    StepResult {
        name: step.name.clone(),
        success,
        output,
        error,
        duration_ms,
    }
}

// ---------------------------------------------------------------------------
// ParallelSteps
// ---------------------------------------------------------------------------

/// A named group of steps that execute concurrently.
pub struct ParallelSteps {
    /// Group name (for logging and error messages).
    pub name: String,
    /// The steps in this group.
    pub steps: Vec<WorkflowStep>,
    /// When `true` (the default): return an error as soon as the first
    /// failure is detected rather than waiting for all steps to finish.
    ///
    /// Note: because this implementation uses `std::thread::scope`, all
    /// spawned threads must complete before the scope returns regardless
    /// of `fail_fast`.  The flag controls only whether the *error type*
    /// returned is [`ParallelStepError::StepFailed`] (single, fail-fast)
    /// or [`ParallelStepError::MultipleStepsFailed`] (aggregated).
    pub fail_fast: bool,
}

impl ParallelSteps {
    /// Create a new group with `fail_fast = true` and no steps.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            steps: Vec::new(),
            fail_fast: true,
        }
    }

    /// Set the `fail_fast` flag.
    #[must_use]
    pub fn with_fail_fast(mut self, fail_fast: bool) -> Self {
        self.fail_fast = fail_fast;
        self
    }

    /// Append a step to the group.
    pub fn add_step(&mut self, step: WorkflowStep) -> &mut Self {
        self.steps.push(step);
        self
    }

    /// Execute all steps in parallel using `std::thread::scope`.
    ///
    /// All worker threads are joined before this function returns.
    /// Results are collected in the same order as [`Self::steps`].
    ///
    /// # Errors
    ///
    /// Returns [`ParallelStepError::StepFailed`] if `fail_fast = true` and
    /// one or more steps fail, or [`ParallelStepError::MultipleStepsFailed`]
    /// if `fail_fast = false` and multiple steps fail.
    pub fn execute_parallel(&self) -> Result<Vec<StepResult>, ParallelStepError> {
        if self.steps.is_empty() {
            return Ok(Vec::new());
        }

        // Collect results using thread::scope so all threads are joined before
        // we return.  We use a Mutex-protected Vec to gather results.
        let results: std::sync::Mutex<Vec<(usize, StepResult)>> =
            std::sync::Mutex::new(Vec::with_capacity(self.steps.len()));

        std::thread::scope(|scope| {
            for (idx, step) in self.steps.iter().enumerate() {
                let results_ref = &results;
                scope.spawn(move || {
                    let result = execute_step(step);
                    if let Ok(mut guard) = results_ref.lock() {
                        guard.push((idx, result));
                    }
                });
            }
        });

        // Reconstruct in original order.
        let mut collected = results.into_inner().unwrap_or_default();
        collected.sort_by_key(|(idx, _)| *idx);
        let ordered: Vec<StepResult> = collected.into_iter().map(|(_, r)| r).collect();

        // Check for failures.
        let failed: Vec<&StepResult> = ordered.iter().filter(|r| !r.success).collect();

        if failed.is_empty() {
            return Ok(ordered);
        }

        if self.fail_fast {
            // Return the first failure.
            let first = &failed[0];
            return Err(ParallelStepError::StepFailed {
                name: first.name.clone(),
                reason: first
                    .error
                    .clone()
                    .unwrap_or_else(|| "unknown error".to_string()),
            });
        }

        // Not fail-fast: return aggregated error.
        Err(ParallelStepError::MultipleStepsFailed {
            count: failed.len(),
            total: ordered.len(),
            results: ordered,
        })
    }
}

// ---------------------------------------------------------------------------
// WorkflowStage
// ---------------------------------------------------------------------------

/// A logical stage within a workflow, containing steps that run either
/// sequentially or in parallel.
pub enum WorkflowStage {
    /// Steps run one after another; fails on the first error.
    Sequential(Vec<WorkflowStep>),
    /// All steps start at the same time; results are joined.
    Parallel(Vec<WorkflowStep>),
}

impl WorkflowStage {
    /// Execute this stage and return all step results.
    ///
    /// # Errors
    ///
    /// Returns [`ParallelStepError`] if any step fails.
    pub fn execute(&self) -> Result<Vec<StepResult>, ParallelStepError> {
        match self {
            Self::Sequential(steps) => {
                let mut results = Vec::with_capacity(steps.len());
                for step in steps {
                    let r = execute_step(step);
                    if !r.success {
                        let reason = r
                            .error
                            .clone()
                            .unwrap_or_else(|| "unknown error".to_string());
                        let name = r.name.clone();
                        results.push(r);
                        return Err(ParallelStepError::StepFailed { name, reason });
                    }
                    results.push(r);
                }
                Ok(results)
            }
            Self::Parallel(steps) => {
                let mut group = ParallelSteps::new("stage");
                group.fail_fast = true;
                for step in steps {
                    group.steps.push(step.clone());
                }
                group.execute_parallel()
            }
        }
    }

    /// Return the number of steps in this stage.
    #[must_use]
    pub fn step_count(&self) -> usize {
        match self {
            Self::Sequential(steps) | Self::Parallel(steps) => steps.len(),
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // -----------------------------------------------------------------------
    // ParallelSteps — success paths
    // -----------------------------------------------------------------------

    #[test]
    fn parallel_all_success_returns_results() {
        let mut group = ParallelSteps::new("grp");
        group.add_step(WorkflowStep::new("a", StepType::Compute { value: 10 }));
        group.add_step(WorkflowStep::new("b", StepType::Compute { value: 20 }));
        group.add_step(WorkflowStep::new(
            "c",
            StepType::Transform {
                value: 3,
                factor: 7,
            },
        ));
        let results = group.execute_parallel().expect("all should succeed");
        assert_eq!(results.len(), 3);
        assert_eq!(results[0].output, Some(10));
        assert_eq!(results[1].output, Some(20));
        assert_eq!(results[2].output, Some(21));
    }

    #[test]
    fn parallel_empty_steps_returns_empty() {
        let group = ParallelSteps::new("empty");
        let results = group
            .execute_parallel()
            .expect("empty group should succeed");
        assert!(results.is_empty());
    }

    #[test]
    fn parallel_single_step_success() {
        let mut group = ParallelSteps::new("single");
        group.add_step(WorkflowStep::new("only", StepType::Compute { value: 42 }));
        let results = group.execute_parallel().expect("should succeed");
        assert_eq!(results[0].output, Some(42));
        assert!(results[0].success);
    }

    // -----------------------------------------------------------------------
    // ParallelSteps — failure paths
    // -----------------------------------------------------------------------

    #[test]
    fn parallel_one_fail_fast_gives_step_failed_error() {
        let mut group = ParallelSteps::new("fail-fast-grp");
        group.fail_fast = true;
        group.add_step(WorkflowStep::new("ok", StepType::Compute { value: 1 }));
        group.add_step(WorkflowStep::new(
            "bad",
            StepType::Fail {
                reason: "oops".to_string(),
            },
        ));
        let err = group.execute_parallel().expect_err("should fail");
        assert!(matches!(err, ParallelStepError::StepFailed { .. }));
    }

    #[test]
    fn parallel_one_fail_not_fast_gives_multiple_failed() {
        let mut group = ParallelSteps::new("no-ff");
        group.fail_fast = false;
        group.add_step(WorkflowStep::new("ok", StepType::Compute { value: 1 }));
        group.add_step(WorkflowStep::new(
            "bad",
            StepType::Fail {
                reason: "nope".to_string(),
            },
        ));
        let err = group.execute_parallel().expect_err("should fail");
        if let ParallelStepError::MultipleStepsFailed { count, total, .. } = err {
            assert_eq!(count, 1);
            assert_eq!(total, 2);
        } else {
            panic!("expected MultipleStepsFailed");
        }
    }

    #[test]
    fn parallel_all_fail_no_fast_aggregates() {
        let mut group = ParallelSteps::new("all-fail");
        group.fail_fast = false;
        group.add_step(WorkflowStep::new(
            "a",
            StepType::Fail {
                reason: "err-a".to_string(),
            },
        ));
        group.add_step(WorkflowStep::new(
            "b",
            StepType::Fail {
                reason: "err-b".to_string(),
            },
        ));
        let err = group.execute_parallel().expect_err("should fail");
        if let ParallelStepError::MultipleStepsFailed { count, total, .. } = err {
            assert_eq!(count, 2);
            assert_eq!(total, 2);
        } else {
            panic!("expected MultipleStepsFailed");
        }
    }

    // -----------------------------------------------------------------------
    // execute_step — direct tests
    // -----------------------------------------------------------------------

    #[test]
    fn execute_compute_step() {
        let step = WorkflowStep::new("s", StepType::Compute { value: -7 });
        let r = execute_step(&step);
        assert!(r.success);
        assert_eq!(r.output, Some(-7));
        assert!(r.error.is_none());
    }

    #[test]
    fn execute_fail_step() {
        let step = WorkflowStep::new(
            "s",
            StepType::Fail {
                reason: "bad".to_string(),
            },
        );
        let r = execute_step(&step);
        assert!(!r.success);
        assert_eq!(r.error.as_deref(), Some("bad"));
    }

    #[test]
    fn execute_transform_step() {
        let step = WorkflowStep::new(
            "s",
            StepType::Transform {
                value: 6,
                factor: 9,
            },
        );
        let r = execute_step(&step);
        assert!(r.success);
        assert_eq!(r.output, Some(54));
    }

    #[test]
    fn execute_wait_step_timeout() {
        // A Wait step whose duration exceeds the per-step timeout should fail.
        let step = WorkflowStep::new("s", StepType::Wait { duration_ms: 500 }).with_timeout(10);
        let r = execute_step(&step);
        assert!(!r.success, "should time out");
        assert!(r.error.is_some());
    }

    // -----------------------------------------------------------------------
    // WorkflowStage
    // -----------------------------------------------------------------------

    #[test]
    fn sequential_stage_executes_in_order() {
        let steps = vec![
            WorkflowStep::new("first", StepType::Compute { value: 1 }),
            WorkflowStep::new("second", StepType::Compute { value: 2 }),
            WorkflowStep::new("third", StepType::Compute { value: 3 }),
        ];
        let stage = WorkflowStage::Sequential(steps);
        let results = stage.execute().expect("should succeed");
        assert_eq!(results.len(), 3);
        assert_eq!(results[0].name, "first");
        assert_eq!(results[1].name, "second");
        assert_eq!(results[2].name, "third");
    }

    #[test]
    fn sequential_stage_stops_on_first_failure() {
        let steps = vec![
            WorkflowStep::new("ok", StepType::Compute { value: 1 }),
            WorkflowStep::new(
                "bad",
                StepType::Fail {
                    reason: "stop".to_string(),
                },
            ),
            WorkflowStep::new("never", StepType::Compute { value: 99 }),
        ];
        let stage = WorkflowStage::Sequential(steps);
        let err = stage.execute().expect_err("should fail");
        assert!(matches!(err, ParallelStepError::StepFailed { .. }));
    }

    #[test]
    fn parallel_stage_all_success() {
        let steps = vec![
            WorkflowStep::new("p1", StepType::Compute { value: 10 }),
            WorkflowStep::new(
                "p2",
                StepType::Transform {
                    value: 3,
                    factor: 4,
                },
            ),
        ];
        let stage = WorkflowStage::Parallel(steps);
        let results = stage.execute().expect("should succeed");
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn stage_step_count() {
        let seq = WorkflowStage::Sequential(vec![
            WorkflowStep::new("a", StepType::Compute { value: 0 }),
            WorkflowStep::new("b", StepType::Compute { value: 0 }),
        ]);
        assert_eq!(seq.step_count(), 2);

        let par =
            WorkflowStage::Parallel(vec![WorkflowStep::new("x", StepType::Compute { value: 0 })]);
        assert_eq!(par.step_count(), 1);
    }

    #[test]
    fn parallel_and_sequential_same_results_all_success() {
        let steps = vec![
            WorkflowStep::new("a", StepType::Compute { value: 5 }),
            WorkflowStep::new("b", StepType::Compute { value: 10 }),
        ];
        let seq_results = WorkflowStage::Sequential(steps.clone())
            .execute()
            .expect("seq ok");
        let par_results = WorkflowStage::Parallel(steps).execute().expect("par ok");

        let seq_outputs: Vec<Option<i64>> = seq_results.iter().map(|r| r.output).collect();
        let par_outputs: Vec<Option<i64>> = par_results.iter().map(|r| r.output).collect();
        assert_eq!(seq_outputs, par_outputs);
    }
}