wdl_engine/eval/v1.rs
1//! Implementation of evaluation for V1 documents.
2
3mod expr;
4mod task;
5mod workflow;
6
7use std::fs::File;
8use std::io::BufWriter;
9use std::path::Path;
10
11use anyhow::Context;
12use anyhow::Result;
13pub use expr::*;
14use serde::Serialize;
15pub use task::*;
16pub use workflow::*;
17
18use super::EvaluatedTask;
19use super::EvaluationResult;
20use crate::Outputs;
21use crate::TaskExecutionResult;
22
23/// The name of the inputs file to write for each task and workflow in the
24/// outputs directory.
25const INPUTS_FILE: &str = "inputs.json";
26
27/// The name of the outputs file to write for each task and workflow in the
28/// outputs directory.
29const OUTPUTS_FILE: &str = "outputs.json";
30
31/// Represents the kind of progress made during evaluation.
32#[derive(Debug, Clone, Copy)]
33pub enum ProgressKind<'a> {
34 /// A task with the given id has started evaluation.
35 TaskStarted {
36 /// The identifier of the task.
37 id: &'a str,
38 },
39 /// A task has been retried.
40 TaskRetried {
41 /// The identifier of the task.
42 id: &'a str,
43 /// The retry number for the task's execution, starting at 0 to indicate
44 /// first retry.
45 ///
46 /// This value is incremented upon each retry.
47 retry: u64,
48 },
49 /// A task with the given id has started execution.
50 ///
51 /// Note that a task may have multiple executions as a result of retrying
52 /// failed executions.
53 TaskExecutionStarted {
54 /// The identifier of the task.
55 id: &'a str,
56 },
57 /// A task with the given id has completed execution.
58 TaskExecutionCompleted {
59 /// The identifier of the task.
60 id: &'a str,
61 /// The result from the task's execution.
62 ///
63 /// This may be `Err` if the task failed to complete.
64 result: &'a Result<TaskExecutionResult>,
65 },
66 /// A task with the given id has completed evaluation.
67 TaskCompleted {
68 /// The identifier of the task.
69 id: &'a str,
70 /// The result of task evaluation.
71 result: &'a EvaluationResult<EvaluatedTask>,
72 },
73 /// A workflow with the given id has started evaluation.
74 WorkflowStarted {
75 /// The identifier of the workflow.
76 id: &'a str,
77 },
78 /// A workflow with the given id has completed evaluation.
79 WorkflowCompleted {
80 /// The identifier of the workflow.
81 id: &'a str,
82 /// The result of workflow evaluation.
83 result: &'a EvaluationResult<Outputs>,
84 },
85}
86
87impl ProgressKind<'_> {
88 /// Gets the id of the task or workflow.
89 pub fn id(&self) -> &str {
90 match self {
91 Self::TaskStarted { id, .. }
92 | Self::TaskRetried { id, .. }
93 | Self::TaskExecutionStarted { id, .. }
94 | Self::TaskExecutionCompleted { id, .. }
95 | Self::TaskCompleted { id, .. }
96 | Self::WorkflowStarted { id, .. }
97 | Self::WorkflowCompleted { id, .. } => id,
98 }
99 }
100}
101
102/// Serializes a value into a JSON file.
103fn write_json_file(path: impl AsRef<Path>, value: &impl Serialize) -> Result<()> {
104 let path = path.as_ref();
105 let file = File::create(path)
106 .with_context(|| format!("failed to create file `{path}`", path = path.display()))?;
107 serde_json::to_writer_pretty(BufWriter::new(file), value)
108 .with_context(|| format!("failed to write file `{path}`", path = path.display()))
109}