oximedia-workflow 0.2.0

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
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
//! Workflow definition and management.

use crate::error::{Result, WorkflowError};
use crate::task::{Task, TaskId};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use uuid::Uuid;

/// Unique workflow identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct WorkflowId(Uuid);

impl WorkflowId {
    /// Create a new random workflow ID.
    #[must_use]
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }

    /// Get the underlying UUID.
    #[must_use]
    pub const fn as_uuid(&self) -> &Uuid {
        &self.0
    }
}

impl Default for WorkflowId {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for WorkflowId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<Uuid> for WorkflowId {
    fn from(uuid: Uuid) -> Self {
        Self(uuid)
    }
}

/// Workflow state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WorkflowState {
    /// Workflow is created but not started.
    Created,
    /// Workflow is scheduled to run.
    Scheduled,
    /// Workflow is running.
    Running,
    /// Workflow is paused.
    Paused,
    /// Workflow completed successfully.
    Completed,
    /// Workflow failed.
    Failed,
    /// Workflow was cancelled.
    Cancelled,
}

impl WorkflowState {
    /// Check if workflow is in a terminal state.
    #[must_use]
    pub const fn is_terminal(&self) -> bool {
        matches!(self, Self::Completed | Self::Failed | Self::Cancelled)
    }

    /// Check if workflow is active.
    #[must_use]
    pub const fn is_active(&self) -> bool {
        matches!(self, Self::Running)
    }
}

/// Edge between tasks in the workflow DAG.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Edge {
    /// Source task ID.
    pub from: TaskId,
    /// Target task ID.
    pub to: TaskId,
    /// Edge condition (optional).
    pub condition: Option<String>,
}

impl Edge {
    /// Create a new edge.
    #[must_use]
    pub const fn new(from: TaskId, to: TaskId) -> Self {
        Self {
            from,
            to,
            condition: None,
        }
    }

    /// Create an edge with a condition.
    #[must_use]
    pub fn with_condition(from: TaskId, to: TaskId, condition: String) -> Self {
        Self {
            from,
            to,
            condition: Some(condition),
        }
    }
}

/// Workflow configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowConfig {
    /// Maximum concurrent tasks.
    #[serde(default = "default_max_concurrent")]
    pub max_concurrent_tasks: usize,
    /// Global timeout for workflow.
    #[serde(default)]
    pub global_timeout: Option<std::time::Duration>,
    /// Whether to stop on first error.
    #[serde(default)]
    pub fail_fast: bool,
    /// Whether to continue on task failure.
    #[serde(default)]
    pub continue_on_error: bool,
    /// Variables available to all tasks.
    #[serde(default)]
    pub variables: HashMap<String, serde_json::Value>,
}

fn default_max_concurrent() -> usize {
    4
}

impl Default for WorkflowConfig {
    fn default() -> Self {
        Self {
            max_concurrent_tasks: default_max_concurrent(),
            global_timeout: None,
            fail_fast: false,
            continue_on_error: false,
            variables: HashMap::new(),
        }
    }
}

/// Workflow definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workflow {
    /// Unique workflow identifier.
    pub id: WorkflowId,
    /// Workflow name.
    pub name: String,
    /// Workflow description.
    #[serde(default)]
    pub description: String,
    /// Tasks in the workflow.
    pub tasks: HashMap<TaskId, Task>,
    /// Edges defining task dependencies.
    pub edges: Vec<Edge>,
    /// Workflow configuration.
    #[serde(default)]
    pub config: WorkflowConfig,
    /// Current state.
    #[serde(default = "default_workflow_state")]
    pub state: WorkflowState,
    /// Workflow metadata.
    #[serde(default)]
    pub metadata: HashMap<String, String>,
}

fn default_workflow_state() -> WorkflowState {
    WorkflowState::Created
}

impl Workflow {
    /// Create a new workflow.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            id: WorkflowId::new(),
            name: name.into(),
            description: String::new(),
            tasks: HashMap::new(),
            edges: Vec::new(),
            config: WorkflowConfig::default(),
            state: WorkflowState::Created,
            metadata: HashMap::new(),
        }
    }

    /// Add a task to the workflow.
    pub fn add_task(&mut self, task: Task) -> TaskId {
        let task_id = task.id;
        self.tasks.insert(task_id, task);
        task_id
    }

    /// Add an edge between tasks.
    pub fn add_edge(&mut self, from: TaskId, to: TaskId) -> Result<()> {
        // Validate that both tasks exist
        if !self.tasks.contains_key(&from) {
            return Err(WorkflowError::TaskNotFound(from.to_string()));
        }
        if !self.tasks.contains_key(&to) {
            return Err(WorkflowError::TaskNotFound(to.to_string()));
        }

        self.edges.push(Edge::new(from, to));
        Ok(())
    }

    /// Add a conditional edge.
    pub fn add_conditional_edge(
        &mut self,
        from: TaskId,
        to: TaskId,
        condition: String,
    ) -> Result<()> {
        if !self.tasks.contains_key(&from) {
            return Err(WorkflowError::TaskNotFound(from.to_string()));
        }
        if !self.tasks.contains_key(&to) {
            return Err(WorkflowError::TaskNotFound(to.to_string()));
        }

        self.edges.push(Edge::with_condition(from, to, condition));
        Ok(())
    }

    /// Get task by ID.
    #[must_use]
    pub fn get_task(&self, task_id: &TaskId) -> Option<&Task> {
        self.tasks.get(task_id)
    }

    /// Get mutable task by ID.
    pub fn get_task_mut(&mut self, task_id: &TaskId) -> Option<&mut Task> {
        self.tasks.get_mut(task_id)
    }

    /// Get all tasks.
    #[must_use]
    pub fn tasks(&self) -> impl Iterator<Item = &Task> {
        self.tasks.values()
    }

    /// Get dependencies for a task.
    #[must_use]
    pub fn get_dependencies(&self, task_id: &TaskId) -> Vec<TaskId> {
        self.edges
            .iter()
            .filter(|e| &e.to == task_id)
            .map(|e| e.from)
            .collect()
    }

    /// Get dependents for a task.
    #[must_use]
    pub fn get_dependents(&self, task_id: &TaskId) -> Vec<TaskId> {
        self.edges
            .iter()
            .filter(|e| &e.from == task_id)
            .map(|e| e.to)
            .collect()
    }

    /// Get root tasks (tasks with no dependencies).
    #[must_use]
    pub fn get_root_tasks(&self) -> Vec<TaskId> {
        let has_incoming: HashSet<_> = self.edges.iter().map(|e| e.to).collect();
        self.tasks
            .keys()
            .filter(|id| !has_incoming.contains(id))
            .copied()
            .collect()
    }

    /// Get leaf tasks (tasks with no dependents).
    #[must_use]
    pub fn get_leaf_tasks(&self) -> Vec<TaskId> {
        let has_outgoing: HashSet<_> = self.edges.iter().map(|e| e.from).collect();
        self.tasks
            .keys()
            .filter(|id| !has_outgoing.contains(id))
            .copied()
            .collect()
    }

    /// Validate workflow structure.
    pub fn validate(&self) -> Result<()> {
        // Check for cycles
        if self.has_cycle() {
            return Err(WorkflowError::CycleDetected);
        }

        // Validate all edge references
        for edge in &self.edges {
            if !self.tasks.contains_key(&edge.from) {
                return Err(WorkflowError::TaskNotFound(edge.from.to_string()));
            }
            if !self.tasks.contains_key(&edge.to) {
                return Err(WorkflowError::TaskNotFound(edge.to.to_string()));
            }
        }

        Ok(())
    }

    /// Check if workflow has a cycle.
    #[must_use]
    pub fn has_cycle(&self) -> bool {
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();

        for task_id in self.tasks.keys() {
            if self.has_cycle_util(*task_id, &mut visited, &mut rec_stack) {
                return true;
            }
        }

        false
    }

    fn has_cycle_util(
        &self,
        task_id: TaskId,
        visited: &mut HashSet<TaskId>,
        rec_stack: &mut HashSet<TaskId>,
    ) -> bool {
        if rec_stack.contains(&task_id) {
            return true;
        }

        if visited.contains(&task_id) {
            return false;
        }

        visited.insert(task_id);
        rec_stack.insert(task_id);

        for dependent in self.get_dependents(&task_id) {
            if self.has_cycle_util(dependent, visited, rec_stack) {
                return true;
            }
        }

        rec_stack.remove(&task_id);
        false
    }

    /// Get topological order of tasks.
    pub fn topological_sort(&self) -> Result<Vec<TaskId>> {
        if self.has_cycle() {
            return Err(WorkflowError::CycleDetected);
        }

        let mut result = Vec::new();
        let mut visited = HashSet::new();

        for task_id in self.tasks.keys() {
            self.topological_sort_util(*task_id, &mut visited, &mut result);
        }

        result.reverse();
        Ok(result)
    }

    fn topological_sort_util(
        &self,
        task_id: TaskId,
        visited: &mut HashSet<TaskId>,
        result: &mut Vec<TaskId>,
    ) {
        if visited.contains(&task_id) {
            return;
        }

        visited.insert(task_id);

        for dependent in self.get_dependents(&task_id) {
            self.topological_sort_util(dependent, visited, result);
        }

        result.push(task_id);
    }

    /// Set workflow description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Set workflow configuration.
    #[must_use]
    pub fn with_config(mut self, config: WorkflowConfig) -> Self {
        self.config = config;
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::task::TaskType;
    use std::time::Duration;

    fn create_test_task(name: &str) -> Task {
        Task::new(
            name,
            TaskType::Wait {
                duration: Duration::from_secs(1),
            },
        )
    }

    #[test]
    fn test_workflow_creation() {
        let workflow = Workflow::new("test-workflow");
        assert_eq!(workflow.name, "test-workflow");
        assert_eq!(workflow.state, WorkflowState::Created);
        assert!(workflow.tasks.is_empty());
    }

    #[test]
    fn test_add_task() {
        let mut workflow = Workflow::new("test");
        let task = create_test_task("task1");
        let task_id = workflow.add_task(task);
        assert_eq!(workflow.tasks.len(), 1);
        assert!(workflow.get_task(&task_id).is_some());
    }

    #[test]
    fn test_add_edge() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");
        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);

        assert!(workflow.add_edge(id1, id2).is_ok());
        assert_eq!(workflow.edges.len(), 1);
    }

    #[test]
    fn test_add_edge_invalid_task() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let id1 = workflow.add_task(task1);
        let invalid_id = TaskId::new();

        assert!(workflow.add_edge(id1, invalid_id).is_err());
    }

    #[test]
    fn test_get_dependencies() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");
        let task3 = create_test_task("task3");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);
        let id3 = workflow.add_task(task3);

        workflow.add_edge(id1, id3).expect("should succeed in test");
        workflow.add_edge(id2, id3).expect("should succeed in test");

        let deps = workflow.get_dependencies(&id3);
        assert_eq!(deps.len(), 2);
        assert!(deps.contains(&id1));
        assert!(deps.contains(&id2));
    }

    #[test]
    fn test_get_root_tasks() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");
        let task3 = create_test_task("task3");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);
        let id3 = workflow.add_task(task3);

        workflow.add_edge(id1, id3).expect("should succeed in test");
        workflow.add_edge(id2, id3).expect("should succeed in test");

        let roots = workflow.get_root_tasks();
        assert_eq!(roots.len(), 2);
        assert!(roots.contains(&id1));
        assert!(roots.contains(&id2));
    }

    #[test]
    fn test_get_leaf_tasks() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");
        let task3 = create_test_task("task3");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);
        let id3 = workflow.add_task(task3);

        workflow.add_edge(id1, id2).expect("should succeed in test");
        workflow.add_edge(id1, id3).expect("should succeed in test");

        let leaves = workflow.get_leaf_tasks();
        assert_eq!(leaves.len(), 2);
        assert!(leaves.contains(&id2));
        assert!(leaves.contains(&id3));
    }

    #[test]
    fn test_has_cycle_no_cycle() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");
        let task3 = create_test_task("task3");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);
        let id3 = workflow.add_task(task3);

        workflow.add_edge(id1, id2).expect("should succeed in test");
        workflow.add_edge(id2, id3).expect("should succeed in test");

        assert!(!workflow.has_cycle());
    }

    #[test]
    fn test_has_cycle_with_cycle() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);

        workflow.add_edge(id1, id2).expect("should succeed in test");
        workflow.add_edge(id2, id1).expect("should succeed in test");

        assert!(workflow.has_cycle());
    }

    #[test]
    fn test_validate_success() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);
        workflow.add_edge(id1, id2).expect("should succeed in test");

        assert!(workflow.validate().is_ok());
    }

    #[test]
    fn test_validate_cycle() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);
        workflow.add_edge(id1, id2).expect("should succeed in test");
        workflow.add_edge(id2, id1).expect("should succeed in test");

        assert!(workflow.validate().is_err());
    }

    #[test]
    fn test_topological_sort() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");
        let task3 = create_test_task("task3");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);
        let id3 = workflow.add_task(task3);

        workflow.add_edge(id1, id2).expect("should succeed in test");
        workflow.add_edge(id2, id3).expect("should succeed in test");

        let sorted = workflow.topological_sort().expect("should succeed in test");
        assert_eq!(sorted.len(), 3);

        let pos1 = sorted
            .iter()
            .position(|&x| x == id1)
            .expect("should succeed in test");
        let pos2 = sorted
            .iter()
            .position(|&x| x == id2)
            .expect("should succeed in test");
        let pos3 = sorted
            .iter()
            .position(|&x| x == id3)
            .expect("should succeed in test");

        assert!(pos1 < pos2);
        assert!(pos2 < pos3);
    }

    #[test]
    fn test_workflow_state_terminal() {
        assert!(WorkflowState::Completed.is_terminal());
        assert!(WorkflowState::Failed.is_terminal());
        assert!(!WorkflowState::Running.is_terminal());
    }

    #[test]
    fn test_conditional_edge() {
        let mut workflow = Workflow::new("test");
        let task1 = create_test_task("task1");
        let task2 = create_test_task("task2");

        let id1 = workflow.add_task(task1);
        let id2 = workflow.add_task(task2);

        assert!(workflow
            .add_conditional_edge(id1, id2, "result == true".to_string())
            .is_ok());
        assert_eq!(workflow.edges.len(), 1);
        assert!(workflow.edges[0].condition.is_some());
    }
}