dataflow-rs 2.1.5

A lightweight rules engine for building IFTTT-style automation and data processing pipelines in Rust. Define rules with JSONLogic conditions, execute actions, and chain workflows.
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
//! # Workflow Execution Module
//!
//! This module handles the execution of workflows and their associated tasks.
//! It provides a clean separation between workflow orchestration and task execution.

use crate::engine::error::{DataflowError, ErrorInfo, Result};
use crate::engine::executor::InternalExecutor;
use crate::engine::functions::{AsyncFunctionHandler, FILTER_STATUS_HALT, FILTER_STATUS_SKIP};
use crate::engine::message::{AuditTrail, Change, Message};
use crate::engine::task_executor::TaskExecutor;
use crate::engine::trace::{ExecutionStep, ExecutionTrace};
use crate::engine::workflow::Workflow;
use chrono::Utc;
use log::{debug, error, info, warn};
use serde_json::json;
use std::collections::HashMap;
use std::sync::Arc;

/// Result of handling a task, including possible control flow signals
enum TaskControlFlow {
    /// Continue executing the next task
    Continue,
    /// Stop executing further tasks in this workflow (filter halt)
    HaltWorkflow,
}

/// Handles the execution of workflows and their tasks
///
/// The `WorkflowExecutor` is responsible for:
/// - Evaluating workflow conditions
/// - Orchestrating task execution within workflows
/// - Managing workflow-level error handling
/// - Recording audit trails
pub struct WorkflowExecutor {
    /// Task executor for executing individual tasks
    task_executor: Arc<TaskExecutor>,
    /// Internal executor for condition evaluation
    internal_executor: Arc<InternalExecutor>,
}

impl WorkflowExecutor {
    /// Create a new WorkflowExecutor
    pub fn new(task_executor: Arc<TaskExecutor>, internal_executor: Arc<InternalExecutor>) -> Self {
        Self {
            task_executor,
            internal_executor,
        }
    }

    /// Get a clone of the task_functions Arc for reuse in new engines
    pub fn task_functions(
        &self,
    ) -> Arc<HashMap<String, Box<dyn AsyncFunctionHandler + Send + Sync>>> {
        self.task_executor.task_functions()
    }

    /// Execute a workflow if its condition is met
    ///
    /// This method:
    /// 1. Evaluates the workflow condition
    /// 2. Executes tasks sequentially if condition is met
    /// 3. Handles error recovery based on workflow configuration
    /// 4. Updates message metadata and audit trail
    ///
    /// # Arguments
    /// * `workflow` - The workflow to execute
    /// * `message` - The message being processed
    ///
    /// # Returns
    /// * `Result<bool>` - Ok(true) if workflow was executed, Ok(false) if skipped, Err on failure
    pub async fn execute(&self, workflow: &Workflow, message: &mut Message) -> Result<bool> {
        // Use cached context Arc for condition evaluation
        let context_arc = message.get_context_arc();

        // Evaluate workflow condition
        let should_execute = self
            .internal_executor
            .evaluate_condition(workflow.condition_index, context_arc)?;

        if !should_execute {
            debug!("Skipping workflow {} - condition not met", workflow.id);
            return Ok(false);
        }

        // Execute workflow tasks
        match self.execute_tasks(workflow, message).await {
            Ok(_) => {
                info!("Successfully completed workflow: {}", workflow.id);
                Ok(true)
            }
            Err(e) if workflow.continue_on_error => {
                warn!(
                    "Workflow {} encountered error but continuing: {:?}",
                    workflow.id, e
                );
                message.errors.push(
                    ErrorInfo::builder(
                        "WORKFLOW_ERROR",
                        format!("Workflow {} error: {}", workflow.id, e),
                    )
                    .workflow_id(&workflow.id)
                    .build(),
                );
                Ok(true)
            }
            Err(e) => {
                error!("Workflow {} failed: {:?}", workflow.id, e);
                Err(e)
            }
        }
    }

    /// Execute a workflow with step-by-step tracing
    ///
    /// Similar to `execute` but records execution steps for debugging.
    ///
    /// # Arguments
    /// * `workflow` - The workflow to execute
    /// * `message` - The message being processed
    /// * `trace` - The execution trace to record steps to
    ///
    /// # Returns
    /// * `Result<bool>` - Ok(true) if workflow was executed, Ok(false) if skipped, Err on failure
    pub async fn execute_with_trace(
        &self,
        workflow: &Workflow,
        message: &mut Message,
        trace: &mut ExecutionTrace,
    ) -> Result<bool> {
        // Use cached context Arc for condition evaluation
        let context_arc = message.get_context_arc();

        // Evaluate workflow condition
        let should_execute = self
            .internal_executor
            .evaluate_condition(workflow.condition_index, context_arc)?;

        if !should_execute {
            debug!("Skipping workflow {} - condition not met", workflow.id);
            // Record skipped workflow step
            trace.add_step(ExecutionStep::workflow_skipped(&workflow.id));
            return Ok(false);
        }

        // Execute workflow tasks with trace collection
        match self
            .execute_tasks_with_trace(workflow, message, trace)
            .await
        {
            Ok(_) => {
                info!("Successfully completed workflow: {}", workflow.id);
                Ok(true)
            }
            Err(e) if workflow.continue_on_error => {
                warn!(
                    "Workflow {} encountered error but continuing: {:?}",
                    workflow.id, e
                );
                message.errors.push(
                    ErrorInfo::builder(
                        "WORKFLOW_ERROR",
                        format!("Workflow {} error: {}", workflow.id, e),
                    )
                    .workflow_id(&workflow.id)
                    .build(),
                );
                Ok(true)
            }
            Err(e) => {
                error!("Workflow {} failed: {:?}", workflow.id, e);
                Err(e)
            }
        }
    }

    /// Execute all tasks in a workflow
    async fn execute_tasks(&self, workflow: &Workflow, message: &mut Message) -> Result<()> {
        for task in &workflow.tasks {
            // Use cached context Arc - it will be fresh if previous task modified it
            let context_arc = message.get_context_arc();

            // Evaluate task condition
            let should_execute = self
                .internal_executor
                .evaluate_condition(task.condition_index, context_arc)?;

            if !should_execute {
                debug!("Skipping task {} - condition not met", task.id);
                continue;
            }

            // Execute the task
            let result = self.task_executor.execute(task, message).await;

            // Handle task result with control flow
            match self.handle_task_result(
                result,
                &workflow.id,
                &task.id,
                task.continue_on_error,
                message,
            )? {
                TaskControlFlow::HaltWorkflow => break,
                TaskControlFlow::Continue => {}
            }
        }

        Ok(())
    }

    /// Execute all tasks in a workflow with trace collection
    async fn execute_tasks_with_trace(
        &self,
        workflow: &Workflow,
        message: &mut Message,
        trace: &mut ExecutionTrace,
    ) -> Result<()> {
        for task in &workflow.tasks {
            // Use cached context Arc - it will be fresh if previous task modified it
            let context_arc = message.get_context_arc();

            // Evaluate task condition
            let should_execute = self
                .internal_executor
                .evaluate_condition(task.condition_index, context_arc)?;

            if !should_execute {
                debug!("Skipping task {} - condition not met", task.id);
                // Record skipped task step
                trace.add_step(ExecutionStep::task_skipped(&workflow.id, &task.id));
                continue;
            }

            // Execute the task with trace support
            let result = self.task_executor.execute_with_trace(task, message).await;

            // Extract mapping_contexts before passing result to handle_task_result
            let mapping_contexts = match &result {
                Ok((_, _, contexts)) => contexts.clone(),
                Err(_) => None,
            };

            // Convert to the standard result format for handle_task_result
            let standard_result = result.map(|(status, changes, _)| (status, changes));

            // Handle task result with control flow
            let control_flow = self.handle_task_result(
                standard_result,
                &workflow.id,
                &task.id,
                task.continue_on_error,
                message,
            )?;

            // Record executed step with message snapshot and optional mapping contexts
            let mut step = ExecutionStep::executed(&workflow.id, &task.id, message);
            if let Some(contexts) = mapping_contexts {
                step = step.with_mapping_contexts(contexts);
            }
            trace.add_step(step);

            if let TaskControlFlow::HaltWorkflow = control_flow {
                break;
            }
        }

        Ok(())
    }

    /// Handle the result of a task execution
    fn handle_task_result(
        &self,
        result: Result<(usize, Vec<Change>)>,
        workflow_id: &str,
        task_id: &str,
        continue_on_error: bool,
        message: &mut Message,
    ) -> Result<TaskControlFlow> {
        match result {
            Ok((status, changes)) => {
                // Handle filter skip — no audit trail, just continue
                if status == FILTER_STATUS_SKIP {
                    debug!("Task {} signaled skip (filter gate)", task_id);
                    return Ok(TaskControlFlow::Continue);
                }

                // Record audit trail
                message.audit_trail.push(AuditTrail {
                    timestamp: Utc::now(),
                    workflow_id: Arc::from(workflow_id),
                    task_id: Arc::from(task_id),
                    status,
                    changes,
                });

                // Update progress metadata for workflow chaining
                if let Some(metadata) = message.context["metadata"].as_object_mut() {
                    // Update existing progress or create new one
                    if let Some(progress) = metadata.get_mut("progress") {
                        if let Some(progress_obj) = progress.as_object_mut() {
                            progress_obj.insert("workflow_id".to_string(), json!(workflow_id));
                            progress_obj.insert("task_id".to_string(), json!(task_id));
                            progress_obj.insert("status_code".to_string(), json!(status));
                        }
                    } else {
                        metadata.insert(
                            "progress".to_string(),
                            json!({
                                "workflow_id": workflow_id,
                                "task_id": task_id,
                                "status_code": status
                            }),
                        );
                    }
                }
                message.invalidate_context_cache();

                // Handle filter halt — audit trail is recorded, halt the workflow
                if status == FILTER_STATUS_HALT {
                    info!(
                        "Task {} halted workflow {} (filter gate)",
                        task_id, workflow_id
                    );
                    return Ok(TaskControlFlow::HaltWorkflow);
                }

                // Check status code
                if (400..500).contains(&status) {
                    warn!("Task {} returned client error status: {}", task_id, status);
                } else if status >= 500 {
                    error!("Task {} returned server error status: {}", task_id, status);
                    if !continue_on_error {
                        return Err(DataflowError::Task(format!(
                            "Task {} failed with status {}",
                            task_id, status
                        )));
                    }
                }
                Ok(TaskControlFlow::Continue)
            }
            Err(e) => {
                error!("Task {} failed: {:?}", task_id, e);

                // Record error in audit trail
                message.audit_trail.push(AuditTrail {
                    timestamp: Utc::now(),
                    workflow_id: Arc::from(workflow_id),
                    task_id: Arc::from(task_id),
                    status: 500,
                    changes: vec![],
                });

                // Add error to message
                message.errors.push(
                    ErrorInfo::builder("TASK_ERROR", format!("Task {} error: {}", task_id, e))
                        .workflow_id(workflow_id)
                        .task_id(task_id)
                        .build(),
                );

                if !continue_on_error {
                    Err(e)
                } else {
                    Ok(TaskControlFlow::Continue)
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::compiler::LogicCompiler;
    use serde_json::json;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_workflow_executor_skip_condition() {
        // Create a workflow with a false condition
        let workflow_json = r#"{
            "id": "test_workflow",
            "name": "Test Workflow",
            "condition": false,
            "tasks": [{
                "id": "dummy_task",
                "name": "Dummy Task",
                "function": {
                    "name": "map",
                    "input": {"mappings": []}
                }
            }]
        }"#;

        let mut compiler = LogicCompiler::new();
        let mut workflow = Workflow::from_json(workflow_json).unwrap();

        // Compile the workflow condition
        let workflows = compiler.compile_workflows(vec![workflow.clone()]);
        if let Some(compiled_workflow) = workflows.iter().find(|w| w.id == "test_workflow") {
            workflow = compiled_workflow.clone();
        }

        let (datalogic, logic_cache) = compiler.into_parts();
        let internal_executor = Arc::new(InternalExecutor::new(datalogic.clone(), logic_cache));
        let task_executor = Arc::new(TaskExecutor::new(
            Arc::new(HashMap::new()),
            internal_executor.clone(),
            datalogic,
        ));
        let workflow_executor = WorkflowExecutor::new(task_executor, internal_executor);

        let mut message = Message::from_value(&json!({}));

        // Execute workflow - should be skipped due to false condition
        let executed = workflow_executor
            .execute(&workflow, &mut message)
            .await
            .unwrap();
        assert!(!executed);
        assert_eq!(message.audit_trail.len(), 0);
    }

    #[tokio::test]
    async fn test_workflow_executor_execute_success() {
        // Create a workflow with a true condition
        let workflow_json = r#"{
            "id": "test_workflow",
            "name": "Test Workflow",
            "condition": true,
            "tasks": [{
                "id": "dummy_task",
                "name": "Dummy Task",
                "function": {
                    "name": "map",
                    "input": {"mappings": []}
                }
            }]
        }"#;

        let mut compiler = LogicCompiler::new();
        let mut workflow = Workflow::from_json(workflow_json).unwrap();

        // Compile the workflow
        let workflows = compiler.compile_workflows(vec![workflow.clone()]);
        if let Some(compiled_workflow) = workflows.iter().find(|w| w.id == "test_workflow") {
            workflow = compiled_workflow.clone();
        }

        let (datalogic, logic_cache) = compiler.into_parts();
        let internal_executor = Arc::new(InternalExecutor::new(datalogic.clone(), logic_cache));
        let task_executor = Arc::new(TaskExecutor::new(
            Arc::new(HashMap::new()),
            internal_executor.clone(),
            datalogic,
        ));
        let workflow_executor = WorkflowExecutor::new(task_executor, internal_executor);

        let mut message = Message::from_value(&json!({}));

        // Execute workflow - should succeed with empty task list
        let executed = workflow_executor
            .execute(&workflow, &mut message)
            .await
            .unwrap();
        assert!(executed);
    }
}