summaryrefslogtreecommitdiffstats
path: root/src/job/dag.rs
blob: ebc49e208d06c485841f43d4db03247f3288169f (plain)
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
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use daggy::Dag as DaggyDag;
use daggy::NodeIndex;
use daggy::Walker;
use getset::Getters;
use uuid::Uuid;

use crate::job::Job;
use crate::job::JobResource;
use crate::package::Package;
use crate::package::PhaseName;
use crate::package::Shebang;
use crate::util::docker::ImageName;

#[derive(Debug, Getters)]
pub struct Dag {
    #[getset(get = "pub")]
    dag: DaggyDag<Job, i8>,

    #[getset(get = "pub")]
    root_idx: NodeIndex,
}

impl Dag {
    pub fn from_package_dag(
        dag: crate::package::Dag,
        script_shebang: Shebang,
        image: ImageName,
        phases: Vec<PhaseName>,
        resources: Vec<JobResource>,
    ) -> Self {
        let build_job = |_, p: &Package| {
            Job::new(
                p.clone(),
                script_shebang.clone(),
                image.clone(),
                phases.clone(),
                resources.clone(),
            )
        };

        Dag {
            dag: dag.dag().map(build_job, |_, e| *e),
            root_idx: *dag.root_idx(),
        }
    }

    pub fn iter<'a>(&'a self) -> impl Iterator<Item = JobDefinition> + 'a {
        self.dag
            .graph()
            .node_indices()
            .map(move |idx| {
                let job = self.dag.graph().node_weight(idx).unwrap(); // TODO
                let children = self.dag.children(idx);
                let children_uuids = children.iter(&self.dag)
                    .filter_map(|(_, node_idx)| {
                        self.dag.graph().node_weight(node_idx)
                    })
                    .map(Job::uuid)
                    .cloned()
                    .collect();

                JobDefinition {
                    job,
                    dependencies: children_uuids
                }
            })
    }

}

#[derive(Debug)]
pub struct JobDefinition<'a> {
    pub job: &'a Job,
    pub dependencies: Vec<Uuid>,
}