oxigdal-workflow 0.1.4

DAG-based workflow engine for complex geospatial processing pipelines
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
//! Cross-workflow dependency scheduling.

use crate::error::{Result, WorkflowError};
use crate::scheduler::{ExecutionStatus, SchedulerConfig};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// Workflow dependency definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowDependency {
    /// Workflow ID that depends on others.
    pub workflow_id: String,
    /// List of workflow IDs this workflow depends on.
    pub dependencies: Vec<DependencyRule>,
    /// Dependency resolution strategy.
    pub strategy: DependencyStrategy,
    /// Description of the dependency.
    pub description: Option<String>,
}

/// Dependency rule for a single dependency.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyRule {
    /// Dependent workflow ID.
    pub workflow_id: String,
    /// Required execution status.
    pub required_status: ExecutionStatus,
    /// Optional time window in seconds (dependency must complete within this window).
    pub time_window_secs: Option<u64>,
    /// Optional execution version/tag requirement.
    pub version_requirement: Option<String>,
}

/// Dependency resolution strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DependencyStrategy {
    /// All dependencies must be satisfied.
    All,
    /// At least one dependency must be satisfied.
    Any,
    /// Exactly N dependencies must be satisfied.
    AtLeast {
        /// Minimum number of dependencies that must be satisfied.
        count: usize,
    },
    /// Custom voting strategy (majority).
    Majority,
}

/// Dependency graph for tracking workflow dependencies.
#[derive(Debug)]
pub struct DependencyGraph {
    /// Map of workflow ID to its dependencies.
    dependencies: HashMap<String, HashSet<String>>,
    /// Map of workflow ID to workflows that depend on it.
    dependents: HashMap<String, HashSet<String>>,
}

impl DependencyGraph {
    /// Create a new empty dependency graph.
    pub fn new() -> Self {
        Self {
            dependencies: HashMap::new(),
            dependents: HashMap::new(),
        }
    }

    /// Add a dependency edge.
    pub fn add_dependency(&mut self, workflow_id: String, dependency_id: String) {
        self.dependencies
            .entry(workflow_id.clone())
            .or_default()
            .insert(dependency_id.clone());

        self.dependents
            .entry(dependency_id)
            .or_default()
            .insert(workflow_id);
    }

    /// Remove a dependency edge.
    pub fn remove_dependency(&mut self, workflow_id: &str, dependency_id: &str) {
        if let Some(deps) = self.dependencies.get_mut(workflow_id) {
            deps.remove(dependency_id);
        }

        if let Some(dependents) = self.dependents.get_mut(dependency_id) {
            dependents.remove(workflow_id);
        }
    }

    /// Get all dependencies for a workflow.
    pub fn get_dependencies(&self, workflow_id: &str) -> Option<&HashSet<String>> {
        self.dependencies.get(workflow_id)
    }

    /// Get all workflows that depend on a given workflow.
    pub fn get_dependents(&self, workflow_id: &str) -> Option<&HashSet<String>> {
        self.dependents.get(workflow_id)
    }

    /// Check for circular dependencies using DFS.
    pub fn has_cycle(&self, start_id: &str) -> bool {
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();
        self.has_cycle_util(start_id, &mut visited, &mut rec_stack)
    }

    /// Utility function for cycle detection.
    fn has_cycle_util(
        &self,
        current: &str,
        visited: &mut HashSet<String>,
        rec_stack: &mut HashSet<String>,
    ) -> bool {
        if rec_stack.contains(current) {
            return true;
        }

        if visited.contains(current) {
            return false;
        }

        visited.insert(current.to_string());
        rec_stack.insert(current.to_string());

        if let Some(deps) = self.dependencies.get(current) {
            for dep in deps {
                if self.has_cycle_util(dep, visited, rec_stack) {
                    return true;
                }
            }
        }

        rec_stack.remove(current);
        false
    }

    /// Get execution order using topological sort.
    pub fn get_execution_order(&self) -> Result<Vec<String>> {
        let mut in_degree: HashMap<String, usize> = HashMap::new();
        let mut zero_in_degree = Vec::new();
        let mut result = Vec::new();

        // Initialize in-degrees for all nodes
        for workflow_id in self.dependencies.keys() {
            in_degree.entry(workflow_id.clone()).or_insert(0);
        }
        for deps in self.dependencies.values() {
            for dep in deps {
                in_degree.entry(dep.clone()).or_insert(0);
            }
        }

        // Calculate in-degrees: if workflow_id depends on deps,
        // then workflow_id has incoming edges from each dep
        for (workflow_id, deps) in &self.dependencies {
            for _ in deps {
                *in_degree.entry(workflow_id.clone()).or_insert(0) += 1;
            }
        }

        // Find nodes with zero in-degree
        for (id, &degree) in &in_degree {
            if degree == 0 {
                zero_in_degree.push(id.clone());
            }
        }

        // Process nodes
        while let Some(current) = zero_in_degree.pop() {
            result.push(current.clone());

            if let Some(dependents) = self.dependents.get(&current) {
                for dependent in dependents {
                    if let Some(degree) = in_degree.get_mut(dependent) {
                        *degree -= 1;
                        if *degree == 0 {
                            zero_in_degree.push(dependent.clone());
                        }
                    }
                }
            }
        }

        // Check if all nodes were processed (no cycle)
        if result.len() != in_degree.len() {
            return Err(WorkflowError::validation("Circular dependency detected"));
        }

        Ok(result)
    }
}

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

/// Dependency scheduler for managing cross-workflow dependencies.
pub struct DependencyScheduler {
    /// Scheduler configuration (reserved for future enhancements).
    _config: SchedulerConfig,
    dependencies: Arc<DashMap<String, WorkflowDependency>>,
    graph: Arc<parking_lot::RwLock<DependencyGraph>>,
    execution_status: Arc<DashMap<String, ExecutionStatus>>,
}

impl DependencyScheduler {
    /// Create a new dependency scheduler.
    pub fn new(config: SchedulerConfig) -> Self {
        Self {
            _config: config,
            dependencies: Arc::new(DashMap::new()),
            graph: Arc::new(parking_lot::RwLock::new(DependencyGraph::new())),
            execution_status: Arc::new(DashMap::new()),
        }
    }

    /// Add a workflow dependency.
    pub fn add_dependency(&self, dependency: WorkflowDependency) -> Result<()> {
        let workflow_id = dependency.workflow_id.clone();

        // Update the graph
        let mut graph = self.graph.write();
        for rule in &dependency.dependencies {
            graph.add_dependency(workflow_id.clone(), rule.workflow_id.clone());
        }

        // Check for cycles
        if graph.has_cycle(&workflow_id) {
            // Rollback
            for rule in &dependency.dependencies {
                graph.remove_dependency(&workflow_id, &rule.workflow_id);
            }
            return Err(WorkflowError::validation(format!(
                "Adding dependency would create a cycle for workflow '{}'",
                workflow_id
            )));
        }

        drop(graph);

        self.dependencies.insert(workflow_id, dependency);
        Ok(())
    }

    /// Remove a workflow dependency.
    pub fn remove_dependency(&self, workflow_id: &str) -> Result<()> {
        let entry = self
            .dependencies
            .remove(workflow_id)
            .ok_or_else(|| WorkflowError::not_found(workflow_id))?;

        let dependency = entry.1;

        // Update the graph
        let mut graph = self.graph.write();
        for rule in &dependency.dependencies {
            graph.remove_dependency(workflow_id, &rule.workflow_id);
        }

        Ok(())
    }

    /// Check if a workflow's dependencies are satisfied.
    pub fn are_dependencies_satisfied(&self, workflow_id: &str) -> Result<bool> {
        let dependency = self
            .dependencies
            .get(workflow_id)
            .ok_or_else(|| WorkflowError::not_found(workflow_id))?;

        let mut satisfied_count = 0;
        let total_count = dependency.dependencies.len();

        for rule in &dependency.dependencies {
            if self.is_dependency_satisfied(rule)? {
                satisfied_count += 1;
            }
        }

        let result = match dependency.strategy {
            DependencyStrategy::All => satisfied_count == total_count,
            DependencyStrategy::Any => satisfied_count > 0,
            DependencyStrategy::AtLeast { count } => satisfied_count >= count,
            DependencyStrategy::Majority => satisfied_count > total_count / 2,
        };

        Ok(result)
    }

    /// Check if a single dependency rule is satisfied.
    fn is_dependency_satisfied(&self, rule: &DependencyRule) -> Result<bool> {
        let status = self
            .execution_status
            .get(&rule.workflow_id)
            .map(|entry| *entry.value())
            .unwrap_or(ExecutionStatus::Pending);

        Ok(status == rule.required_status)
    }

    /// Update the execution status of a workflow.
    pub fn update_status(&self, workflow_id: String, status: ExecutionStatus) {
        self.execution_status.insert(workflow_id, status);
    }

    /// Get workflows that can be executed (dependencies satisfied).
    pub fn get_executable_workflows(&self) -> Result<Vec<String>> {
        let mut executable = Vec::new();

        for entry in self.dependencies.iter() {
            let workflow_id = entry.key();
            if self.are_dependencies_satisfied(workflow_id)? {
                executable.push(workflow_id.clone());
            }
        }

        Ok(executable)
    }

    /// Get the dependency graph.
    pub fn get_graph(&self) -> parking_lot::RwLockReadGuard<'_, DependencyGraph> {
        self.graph.read()
    }

    /// Get execution order for all workflows.
    pub fn get_execution_order(&self) -> Result<Vec<String>> {
        self.graph.read().get_execution_order()
    }

    /// Clear all dependencies.
    pub fn clear(&self) {
        self.dependencies.clear();
        self.execution_status.clear();
        let mut graph = self.graph.write();
        *graph = DependencyGraph::new();
    }
}

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

    #[test]
    fn test_dependency_graph_creation() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("workflow1".to_string(), "workflow2".to_string());

        assert!(graph.get_dependencies("workflow1").is_some());
        assert_eq!(
            graph
                .get_dependencies("workflow1")
                .expect("Missing deps")
                .len(),
            1
        );
    }

    #[test]
    fn test_dependency_graph_cycle_detection() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("workflow1".to_string(), "workflow2".to_string());
        graph.add_dependency("workflow2".to_string(), "workflow3".to_string());
        graph.add_dependency("workflow3".to_string(), "workflow1".to_string());

        assert!(graph.has_cycle("workflow1"));
    }

    #[test]
    fn test_dependency_graph_execution_order() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("workflow1".to_string(), "workflow2".to_string());
        graph.add_dependency("workflow2".to_string(), "workflow3".to_string());

        let order = graph.get_execution_order().expect("Failed to get order");
        assert!(!order.is_empty());
    }

    #[test]
    fn test_dependency_scheduler() {
        let scheduler = DependencyScheduler::new(SchedulerConfig::default());

        let dependency = WorkflowDependency {
            workflow_id: "workflow1".to_string(),
            dependencies: vec![DependencyRule {
                workflow_id: "workflow2".to_string(),
                required_status: ExecutionStatus::Success,
                time_window_secs: None,
                version_requirement: None,
            }],
            strategy: DependencyStrategy::All,
            description: None,
        };

        scheduler
            .add_dependency(dependency)
            .expect("Failed to add dependency");

        // Initially not satisfied
        assert!(
            !scheduler
                .are_dependencies_satisfied("workflow1")
                .expect("Check failed")
        );

        // Update status
        scheduler.update_status("workflow2".to_string(), ExecutionStatus::Success);

        // Now satisfied
        assert!(
            scheduler
                .are_dependencies_satisfied("workflow1")
                .expect("Check failed")
        );
    }

    #[test]
    fn test_dependency_cycle_prevention() {
        let scheduler = DependencyScheduler::new(SchedulerConfig::default());

        let dep1 = WorkflowDependency {
            workflow_id: "workflow1".to_string(),
            dependencies: vec![DependencyRule {
                workflow_id: "workflow2".to_string(),
                required_status: ExecutionStatus::Success,
                time_window_secs: None,
                version_requirement: None,
            }],
            strategy: DependencyStrategy::All,
            description: None,
        };

        scheduler.add_dependency(dep1).expect("Failed to add");

        let dep2 = WorkflowDependency {
            workflow_id: "workflow2".to_string(),
            dependencies: vec![DependencyRule {
                workflow_id: "workflow1".to_string(),
                required_status: ExecutionStatus::Success,
                time_window_secs: None,
                version_requirement: None,
            }],
            strategy: DependencyStrategy::All,
            description: None,
        };

        // Should fail due to cycle
        assert!(scheduler.add_dependency(dep2).is_err());
    }
}