apalis-workflow 0.1.0-rc.7

A flexible and composable task workflow engine for rust
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
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
use std::{
    collections::{HashMap, VecDeque},
    fmt::{self, Debug},
    marker::PhantomData,
    sync::Mutex,
};

use apalis_core::{
    backend::{BackendExt, codec::Codec},
    error::BoxDynError,
    task::Task,
    task_fn::{TaskFn, task_fn},
};
use petgraph::{
    Direction,
    algo::toposort,
    dot::Config,
    graph::{DiGraph, EdgeIndex, NodeIndex},
};
/// DAG executor implementations
pub mod executor;
/// DAG service implementations
pub mod service;

/// DAG error definitions
pub mod error;
/// DAG node implementations
pub mod node;

/// DAG context implementations
pub mod context;

/// DAG response implementations
pub mod response;

/// DAG Decoding and encoding utilities
pub mod decode;

use serde::{Deserialize, Serialize};
use tower::{Service, util::BoxCloneSyncService};

use crate::{
    DagService,
    dag::{decode::DagCodec, error::DagFlowError, executor::DagExecutor, node::NodeService},
};

pub use context::DagFlowContext;
pub use service::RootDagService;

/// Directed Acyclic Graph (DAG) workflow builder
#[derive(Debug)]
pub struct DagFlow<B>
where
    B: BackendExt,
{
    name: String,
    graph: Mutex<DiGraph<DagService<B::Compact, B::Context, B::IdType>, ()>>,
    node_mapping: Mutex<HashMap<String, NodeIndex>>,
}

impl<B> fmt::Display for DagFlow<B>
where
    B: BackendExt,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "DAG name: {}", self.name)?;
        writeln!(f, "Dot format:")?;
        f.write_str(&self.to_dot())
    }
}

impl<B> DagFlow<B>
where
    B: BackendExt,
{
    /// Create a new DAG workflow builder
    #[must_use]
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_owned(),
            graph: Mutex::new(DiGraph::new()),
            node_mapping: Mutex::new(HashMap::new()),
        }
    }

    /// Add a node to the DAG
    #[must_use]
    pub fn add_node<S, Input, CodecError>(
        &self,
        name: &str,
        service: S,
    ) -> NodeBuilder<'_, Input, S::Response, B>
    where
        S: Service<Task<Input, B::Context, B::IdType>> + Send + 'static + Sync + Clone,
        S::Future: Send + 'static,
        B::Codec: Codec<Input, Compact = B::Compact, Error = CodecError>
            + Codec<S::Response, Compact = B::Compact, Error = CodecError>
            + 'static,
        CodecError: Into<BoxDynError> + Send + 'static,
        S::Error: Into<BoxDynError>,
        B: Send + Sync + 'static,
        Input: DagCodec<B, Error = CodecError> + Send + Sync + 'static,
    {
        let svc: NodeService<S, B, Input> = NodeService::new(service);
        let node = self
            .graph
            .lock()
            .expect("Failed to lock graph mutex")
            .add_node(BoxCloneSyncService::new(svc));
        self.node_mapping
            .lock()
            .expect("Failed to lock node_mapping mutex")
            .insert(name.to_owned(), node);
        NodeBuilder {
            id: node,
            dag: self,
            _phantom: PhantomData,
        }
    }

    /// Add a task function node to the DAG
    pub fn node<F, Input, O, FnArgs, Err, CodecError>(&self, node: F) -> NodeBuilder<'_, Input, O, B>
    where
        TaskFn<F, Input, B::Context, FnArgs>: Service<Task<Input, B::Context, B::IdType>, Response = O, Error = Err> + Clone,
        F: Send + 'static + Sync,
        Input: Send + 'static + Sync,
        FnArgs: Send + 'static + Sync,
        B::Context: Send + Sync + 'static,
        <TaskFn<F, Input, B::Context, FnArgs> as Service<Task<Input, B::Context, B::IdType>>>::Future:
            Send + 'static,
        B::Codec: Codec<Input, Compact = B::Compact, Error = CodecError> + 'static,
        B::Codec: Codec<O, Compact = B::Compact, Error = CodecError> + 'static,
        CodecError: Into<BoxDynError> + Send + 'static,
        Err: Into<BoxDynError>,
        B: Send + Sync + 'static,
        Input: DagCodec<B, Error = CodecError> + Send + Sync + 'static,

    {
        self.add_node(std::any::type_name::<F>(), task_fn(node))
    }

    /// Add a routing node to the DAG
    pub fn route<F, Input, O, FnArgs, Err, CodecError>(
        &self,
        router: F,
    ) -> NodeBuilder<'_, Input, O, B>
    where
        TaskFn<F, Input, B::Context, FnArgs>: Service<Task<Input, B::Context, B::IdType>, Response = O, Error = Err> + Clone,
        F: Send + 'static + Sync,
        Input: Send + 'static + Sync,
        FnArgs: Send + 'static + Sync,
        <TaskFn<F, Input, B::Context, FnArgs> as Service<Task<Input, B::Context, B::IdType>>>::Future:
            Send + 'static,
        O: Into<NodeIndex>,
        B::Context: Send + Sync + 'static,
        B::Codec: Codec<Input, Compact = B::Compact, Error = CodecError> + 'static,
        B::Codec: Codec<O, Compact = B::Compact, Error = CodecError> + 'static,
        CodecError: Into<BoxDynError> + Send + 'static,
        Err: Into<BoxDynError>,
        B: Send + Sync + 'static,
        Input: DagCodec<B, Error = CodecError> + Send + Sync + 'static,

    {
        self.add_node::<TaskFn<F, Input, B::Context, FnArgs>, Input, CodecError>(
            std::any::type_name::<F>(),
            task_fn(router),
        )
    }

    /// Validate the DAG for cycles
    pub fn validate(&self) -> Result<(), DagFlowError> {
        // Validate DAG (check for cycles)
        toposort(
            &*self.graph.lock().expect("Failed to lock graph mutex"),
            None,
        )
        .map_err(DagFlowError::CyclicDAG)?;
        Ok(())
    }

    /// Export the DAG to DOT format
    pub fn to_dot(&self) -> String {
        let names = self
            .node_mapping
            .lock()
            .expect("could not lock nodes")
            .iter()
            .map(|(name, &idx)| (idx, name.clone()))
            .collect::<HashMap<_, _>>();
        let get_node_attributes = |_, (index, _)| {
            format!(
                "label=\"{}\"",
                names.get(&index).cloned().unwrap_or_default()
            )
        };
        let graph = self.graph.lock().expect("could not lock graph");
        let dot = petgraph::dot::Dot::with_attr_getters(
            &*graph,
            &[Config::NodeNoLabel, Config::EdgeNoLabel],
            &|_, _| String::new(),
            &get_node_attributes,
        );
        format!("{dot:?}")
    }

    /// Build the DAG executor
    pub(crate) fn build(self) -> Result<DagExecutor<B>, DagFlowError> {
        // Validate DAG (check for cycles)
        let sorted = toposort(
            &*self.graph.lock().expect("Failed to lock graph mutex"),
            None,
        )
        .map_err(DagFlowError::CyclicDAG)?;

        fn find_edge_nodes<N, E>(graph: &DiGraph<N, E>, direction: Direction) -> Vec<NodeIndex> {
            graph
                .node_indices()
                .filter(|&n| graph.neighbors_directed(n, direction).count() == 0)
                .collect()
        }

        let graph = self
            .graph
            .into_inner()
            .expect("Failed to unlock graph mutex");

        Ok(DagExecutor {
            start_nodes: find_edge_nodes(&graph, Direction::Incoming),
            end_nodes: find_edge_nodes(&graph, Direction::Outgoing),
            graph,
            node_mapping: self
                .node_mapping
                .into_inner()
                .expect("Failed to unlock node_mapping mutex"),
            topological_order: sorted,
            not_ready: VecDeque::new(),
        })
    }
}

/// Builder for a node in the DAG
pub struct NodeBuilder<'a, Input, Output, B>
where
    B: BackendExt,
{
    pub(crate) id: NodeIndex,
    pub(crate) dag: &'a DagFlow<B>,
    _phantom: PhantomData<(Input, Output)>,
}

impl<'a, Input, Output, B> std::fmt::Debug for NodeBuilder<'a, Input, Output, B>
where
    B: BackendExt,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NodeBuilder")
            .field("id", &self.id)
            .finish_non_exhaustive()
    }
}

impl<'a, Input, Output, B> Clone for NodeBuilder<'a, Input, Output, B>
where
    B: BackendExt,
{
    fn clone(&self) -> Self {
        Self {
            id: self.id,
            dag: self.dag,
            _phantom: PhantomData,
        }
    }
}

impl<Input, Output, B> NodeBuilder<'_, Input, Output, B>
where
    B: BackendExt,
{
    /// Specify dependencies for this node
    #[allow(clippy::needless_pass_by_value)]
    pub fn depends_on<D>(self, deps: D) -> NodeHandle<Input, Output>
    where
        D: DepsCheck<Input>,
    {
        let mut edges = Vec::new();
        for dep in deps.to_node_indices() {
            edges.push(self.dag.graph.lock().unwrap().add_edge(dep, self.id, ()));
        }
        NodeHandle {
            id: self.id,
            edges,
            _phantom: PhantomData,
        }
    }
}

/// Handle for a node in the DAG
#[derive(Clone, Debug)]
pub struct NodeHandle<Input, Output> {
    pub(crate) id: NodeIndex,
    pub(crate) edges: Vec<EdgeIndex>,
    pub(crate) _phantom: PhantomData<(Input, Output)>,
}

impl<Input, Output> NodeHandle<Input, Output> {
    /// Get the node ID
    #[must_use]
    pub fn id(&self) -> NodeIndex {
        self.id
    }

    /// Get the edge IDs
    #[must_use]
    pub fn edges(&self) -> &[EdgeIndex] {
        &self.edges
    }
}

/// Trait for converting dependencies into node IDs
pub trait DepsCheck<Input> {
    /// Convert dependencies to node indices
    fn to_node_indices(&self) -> Vec<NodeIndex>;
}

impl DepsCheck<()> for () {
    fn to_node_indices(&self) -> Vec<NodeIndex> {
        Vec::new()
    }
}

impl<'a, Input, Output, B> DepsCheck<Output> for &NodeBuilder<'a, Input, Output, B>
where
    B: BackendExt,
{
    fn to_node_indices(&self) -> Vec<NodeIndex> {
        vec![self.id]
    }
}

impl<Input, Output> DepsCheck<Output> for &NodeHandle<Input, Output> {
    fn to_node_indices(&self) -> Vec<NodeIndex> {
        vec![self.id]
    }
}

impl<Input, Output> DepsCheck<Output> for (&NodeHandle<Input, Output>,) {
    fn to_node_indices(&self) -> Vec<NodeIndex> {
        vec![self.0.id]
    }
}

impl<'a, Input, Output, B> DepsCheck<Output> for (&NodeBuilder<'a, Input, Output, B>,)
where
    B: BackendExt,
{
    fn to_node_indices(&self) -> Vec<NodeIndex> {
        vec![self.0.id]
    }
}

impl<Output, T: DepsCheck<Output>> DepsCheck<Vec<Output>> for Vec<T> {
    fn to_node_indices(&self) -> Vec<NodeIndex> {
        self.iter()
            .flat_map(|item| item.to_node_indices())
            .collect()
    }
}

macro_rules! impl_deps_check {
    ($( $len:literal => ( $( $in:ident $out:ident $idx:tt ),+ ) ),+ $(,)?) => {
        $(
            impl<'a, $( $in, )+ $( $out, )+ B> DepsCheck<( $( $out, )+ )>
                for ( $( &NodeBuilder<'a, $in, $out, B>, )+ ) where B: BackendExt
            {
                fn to_node_indices(&self) -> Vec<NodeIndex> {
                    vec![ $( self.$idx.id ),+ ]
                }
            }

            impl<$( $in, )+ $( $out, )+> DepsCheck<( $( $out, )+ )>
                for ( $( &NodeHandle<$in, $out>, )+ )
            {
                fn to_node_indices(&self) -> Vec<NodeIndex> {
                    vec![ $( self.$idx.id ),+ ]
                }
            }
        )+
    };
}

impl_deps_check! {
    1 => (Input1 Output1 0),
    2 => (Input1 Output1 0, Input2 Output2 1),
    3 => (Input1 Output1 0, Input2 Output2 1, Input3 Output3 2),
    4 => (Input1 Output1 0, Input2 Output2 1, Input3 Output3 2, Input4 Output4 3),
    5 => (Input1 Output1 0, Input2 Output2 1, Input3 Output3 2, Input4 Output4 3, Input5 Output5 4),
    6 => (Input1 Output1 0, Input2 Output2 1, Input3 Output3 2, Input4 Output4 3, Input5 Output5 4, Input6 Output6 5),
    7 => (Input1 Output1 0, Input2 Output2 1, Input3 Output3 2, Input4 Output4 3, Input5 Output5 4, Input6 Output6 5, Input7 Output7 6),
    8 => (Input1 Output1 0, Input2 Output2 1, Input3 Output3 2, Input4 Output4 3, Input5 Output5 4, Input6 Output6 5, Input7 Output7 6, Input8 Output8 7),
}

/// State of the node in DAG execution
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum DagState {
    /// Unknown state
    Unknown,
    /// State for a single node
    SingleNode,
    /// Fan-in state to gather inputs
    FanIn,
    /// Fan-out state to distribute inputs
    FanOut,
}

#[cfg(test)]
mod tests {
    use std::{collections::BTreeMap, num::ParseIntError};

    use apalis_core::{
        error::BoxDynError,
        task_fn::task_fn,
        worker::{
            builder::WorkerBuilder, context::WorkerContext, event::Event,
            ext::event_listener::EventListenerExt,
        },
    };
    use apalis_file_storage::JsonStorage;
    use futures::StreamExt;
    use petgraph::graph::NodeIndex;
    use serde::{Deserialize, Serialize};
    use serde_json::Value;

    use crate::{WorkflowSink, dag::response::DagExecutionResponse};

    use super::*;

    #[tokio::test]
    async fn test_basic_workflow() {
        let dag = DagFlow::new("sequential-workflow");
        let start = dag.add_node("start", task_fn(|task: u32| async move { task as usize }));
        let middle = dag
            .add_node(
                "middle",
                task_fn(|task: usize| async move { task.to_string() }),
            )
            .depends_on(&start);

        let _end = dag
            .add_node(
                "end",
                task_fn(|task: String, worker: WorkerContext| async move {
                    worker.stop().unwrap();
                    task.parse::<usize>()
                }),
            )
            .depends_on(&middle);

        println!("DAG in DOT format:\n{}", dag.to_dot());

        let mut backend = JsonStorage::new_temp().unwrap();

        backend.push_start(42).await.unwrap();

        let worker = WorkerBuilder::new("rango-tango")
            .backend(backend)
            .on_event(|ctx, ev| {
                println!("On Event = {ev:?}");
                if matches!(ev, Event::Error(_)) {
                    ctx.stop().unwrap();
                }
            })
            .build(dag);
        worker.run().await.unwrap();
    }

    #[tokio::test]
    async fn test_fan_out_workflow() {
        let dag = DagFlow::new("fan-out-workflow");
        let source = dag.add_node("source", task_fn(|task: u32| async move { task as usize }));
        let plus_one = dag
            .add_node("plus_one", task_fn(|task: usize| async move { task + 1 }))
            .depends_on(&source);

        let multiply = dag
            .add_node("multiply", task_fn(|task: usize| async move { task * 2 }))
            .depends_on(&source);
        let squared = dag
            .add_node("squared", task_fn(|task: usize| async move { task * task }))
            .depends_on(&source);

        let _collector = dag
            .add_node(
                "collector",
                task_fn(|task: (usize, usize, usize), w: WorkerContext| async move {
                    w.stop().unwrap();
                    task.0 + task.1 + task.2
                }),
            )
            .depends_on((&plus_one, &multiply, &squared));

        println!("DAG in DOT format:\n{}", dag.to_dot());

        let mut backend: JsonStorage<Value> = JsonStorage::new_temp().unwrap();

        backend.push_start(42).await.unwrap();

        let worker = WorkerBuilder::new("rango-tango")
            .backend(backend)
            .on_event(|ctx, ev| {
                println!("On Event = {ev:?}");
                if matches!(ev, Event::Error(_)) {
                    ctx.stop().unwrap();
                }
            })
            .build(dag);
        worker.run().await.unwrap();
    }

    #[tokio::test]
    async fn test_fan_in_workflow() {
        let dag = DagFlow::new("fan-in-workflow");
        let get_name = dag.add_node(
            "get_name",
            task_fn(|task: usize| async move { task as usize }),
        );
        let get_age = dag.add_node(
            "get_age",
            task_fn(|task: u32| async move { task.to_string() }),
        );
        let get_address = dag.add_node(
            "get_address",
            task_fn(|task: i32| async move { task as usize }),
        );
        let main_collector = dag
            .add_node(
                "main_collector",
                task_fn(|task: (String, usize, usize)| async move {
                    task.2 + task.1 + task.0.parse::<usize>().unwrap()
                }),
            )
            .depends_on((&get_age, &get_name, &get_address));

        let side_collector = dag
            .add_node(
                "side_collector",
                task_fn(
                    |task: (usize, usize)| async move { [task.0, task.1].iter().sum::<usize>() },
                ),
            )
            .depends_on((&get_name, &get_address));

        let _final_node = dag
            .add_node(
                "final_node",
                task_fn(|task: (usize, usize), w: WorkerContext| async move {
                    w.stop().unwrap();
                    task.0 + task.1
                }),
            )
            .depends_on((&main_collector, &side_collector));

        println!("DAG in DOT format:\n{}", dag.to_dot());

        let mut backend: JsonStorage<Value> = JsonStorage::new_temp().unwrap();

        backend
            .start_fan_out((42usize, 43u32, 44i32))
            .await
            .unwrap();

        let worker = WorkerBuilder::new("rango-tango")
            .backend(backend)
            .on_event(|ctx, ev| {
                println!("On Event = {ev:?}");
                if matches!(ev, Event::Error(_)) {
                    ctx.stop().unwrap();
                }
            })
            .build(dag);
        worker.run().await.unwrap();
    }

    #[tokio::test]
    async fn fan_in_completes_once_with_testworker() {
        use apalis_core::task_fn::task_fn;
        use apalis_core::worker::test_worker::TestWorker;
        use apalis_file_storage::JsonStorage;
        use serde_json::Value;

        let dag = DagFlow::new("fan-in-testworker");

        let a = dag.add_node("a", task_fn(|t: u32| async move { t }));
        let b = dag.add_node("b", task_fn(|t: u32| async move { t }));

        let _fan_in = dag
            .add_node("fan_in", task_fn(|t: (u32, u32)| async move { t.0 + t.1 }))
            .depends_on((&a, &b));

        let mut backend: JsonStorage<Value> = JsonStorage::new_temp().unwrap();
        backend.start_fan_out((1u32, 2u32)).await.unwrap();

        let worker = TestWorker::new(backend, dag);
        let stm = worker.into_stream();

        let (res_map, final_res) = stm.take(5).collect::<Vec<_>>().await.into_iter().fold(
            (BTreeMap::new(), None),
            |(mut res_map, final_res), item| {
                let (_, resp) = match item {
                    Ok(v) => v,
                    Err(e) => panic!("worker error: {e:?}"),
                };
                let resp = resp.expect("task error");

                let kind = match &resp {
                    DagExecutionResponse::EntryFanOut { .. } => "EntryFanOut",
                    DagExecutionResponse::FanOut { .. } => "FanOut",
                    DagExecutionResponse::EnqueuedNext { .. } => "EnqueuedNext",
                    DagExecutionResponse::WaitingForDependencies { .. } => "WaitingForDependencies",
                    DagExecutionResponse::Complete { .. } => "Complete",
                };
                *res_map.entry(kind).or_insert(0) += 1;

                if let DagExecutionResponse::Complete { result } = resp {
                    return (res_map, Some(result));
                }
                (res_map, final_res)
            },
        );

        assert_eq!(final_res, Some(Value::from(3)));
        assert_eq!(res_map["EntryFanOut"], 1); // Entry
        assert_eq!(res_map["EnqueuedNext"], 2); // Node Results
        assert_eq!(res_map["WaitingForDependencies"], 1); // Non Handler node
        assert_eq!(res_map["Complete"], 1); // Handler Node
    }

    #[tokio::test]
    async fn test_routed_workflow() {
        let dag = DagFlow::new("routed-workflow");

        let entry1 = dag.add_node("entry1", task_fn(|task: u32| async move { task as usize }));
        let entry2 = dag.add_node("entry2", task_fn(|task: u32| async move { task as usize }));
        let entry3 = dag.add_node("entry3", task_fn(|task: u32| async move { task as usize }));

        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
        enum EntryRoute {
            Entry1(NodeIndex),
            Entry2(NodeIndex),
            Entry3(NodeIndex),
        }

        impl Into<NodeIndex> for EntryRoute {
            fn into(self) -> NodeIndex {
                match self {
                    EntryRoute::Entry1(idx) => idx,
                    EntryRoute::Entry2(idx) => idx,
                    EntryRoute::Entry3(idx) => idx,
                }
            }
        }

        impl DepsCheck<usize> for EntryRoute {
            fn to_node_indices(&self) -> Vec<NodeIndex> {
                vec![(*self).into()]
            }
        }

        async fn collect(task: (usize, usize, usize)) -> usize {
            task.0 + task.1 + task.2
        }
        let collector = dag.node(collect).depends_on((&entry1, &entry2, &entry3));

        async fn vec_collect(task: Vec<usize>, _wrk: WorkerContext) -> usize {
            task.iter().sum::<usize>()
        }

        let vec_collector = dag
            .node(vec_collect)
            .depends_on(vec![&entry1, &entry2, &entry3]);

        async fn exit(task: (usize, usize)) -> Result<u32, ParseIntError> {
            (task.0.to_string() + &task.1.to_string()).parse()
        }

        let on_collect = dag.node(exit).depends_on((&collector, &vec_collector));

        async fn check_approval(
            task: u32,
            worker: WorkerContext,
        ) -> Result<EntryRoute, BoxDynError> {
            println!("Approval check for task: {}", task);
            worker.stop().unwrap();
            match task % 3 {
                0 => Ok(EntryRoute::Entry1(NodeIndex::new(0))),
                1 => Ok(EntryRoute::Entry2(NodeIndex::new(1))),
                2 => Ok(EntryRoute::Entry3(NodeIndex::new(2))),
                _ => Err(BoxDynError::from("Invalid task")),
            }
        }

        dag.route(check_approval).depends_on(&on_collect);

        println!("DAG in DOT format:\n{}", dag.to_dot());

        let mut backend = JsonStorage::new_temp().unwrap();

        backend.start_fan_out(vec![17, 18, 19]).await.unwrap();

        let worker = WorkerBuilder::new("rango-tango")
            .backend(backend)
            .on_event(|ctx, ev| {
                println!("On Event = {ev:?}");
                if matches!(ev, Event::Error(_)) {
                    ctx.stop().unwrap();
                }
            })
            .build(dag);
        worker.run().await.unwrap();
    }
}