Skip to content

Commit

Permalink
Handle skipped jobs in output
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Jul 8, 2024
1 parent e9ea94e commit 7ca8016
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use handlebars::Handlebars;
use serde::{Deserialize, Serialize};

use crate::error::*;
use crate::queue::JobState;
use crate::Options;


Expand Down Expand Up @@ -172,13 +173,13 @@ impl JobDescription {
}

impl InnerJobRealization {
pub fn run(&self, status_writer: &mut impl Write, log_writer: &mut impl Write, options: &Options) -> ZinnResult<String> {
pub fn run(&self, status_writer: &mut impl Write, log_writer: &mut impl Write, options: &Options) -> ZinnResult<JobState> {
// skip if dry run
if options.dry_run {
if options.trace {
let _ = writeln!(log_writer, "{}", self.cmd());
}
return Ok(String::from("(dry run)"));
return Ok(JobState::Finished);
}

// check if all input files exist
Expand Down Expand Up @@ -207,7 +208,7 @@ impl InnerJobRealization {
}
}
if !dirty {
return Ok(String::from("Nothing to do"));
return Ok(JobState::Skipped);
}
}

Expand All @@ -226,7 +227,6 @@ impl InnerJobRealization {
.spawn()?;


let output = String::new();
let mut last_line: Option<String> = None;

for line in BufReader::new(io_reader).lines().map_while(Result::ok) {
Expand Down Expand Up @@ -254,7 +254,7 @@ impl InnerJobRealization {
if !status.success() {
Err(ZinnError::Child())
} else {
Ok(output)
Ok(JobState::Finished)
}
}

Expand Down
30 changes: 21 additions & 9 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ pub struct Queue {
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum JobState {
pub enum JobState {
Ready,
Failed,
Running,
Finished,
Skipped,
}

struct InnerQueue {
Expand Down Expand Up @@ -85,9 +86,25 @@ impl Queue {
}

impl InnerQueue {
fn is_completed(&self, job: JobRealization) -> bool {
if let Some(state) = self.states.get(&job) {
*state == JobState::Finished || *state == JobState::Skipped || *state == JobState::Failed
} else {
false
}
}

fn is_completed_successfully(&self, job: JobRealization) -> bool {
if let Some(state) = self.states.get(&job) {
*state == JobState::Finished || *state == JobState::Skipped
} else {
false
}
}

fn dependencies_satisfied(&self, job: JobRealization) -> bool {
for dep in job.dependencies() {
if self.states.get(&dep) != Some(JobState::Finished).as_ref() {
if !self.is_completed_successfully(dep) {
return false;
}
}
Expand Down Expand Up @@ -115,18 +132,13 @@ impl InnerQueue {

/// Determines whether the task is running or may be run in the future
fn task_alive(&self, job: JobRealization) -> bool {
let state = match self.states.get(&job) {
Some(state) => *state,
None => return false,
};

if state == JobState::Finished || state == JobState::Failed {
if self.is_completed(job.clone()) {
return false;
}

for dep in job.dependencies() {
if !self.task_alive(dep.clone())
&& self.states.get(&dep) != Some(JobState::Finished).as_ref() {
&& !self.is_completed_successfully(dep.clone()) {
return false;
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Write;

use indicatif::ProgressBar;

use crate::queue::Queue;
use crate::queue::{JobState, Queue};
use crate::Options;

struct BarMessageWriter(String, ProgressBar);
Expand Down Expand Up @@ -56,12 +56,18 @@ pub fn run_worker(queue: Queue, bar: ProgressBar, main_bar: ProgressBar, options
bar.set_message("");
if let Some(job) = queue.fetch() {
bar.set_prefix(job.to_string());
if let Err(e) = job.run(&mut status_writer, &mut log_writer, &options) {
if let Ok(state) = job.run(&mut status_writer, &mut log_writer, &options) {
match state {
JobState::Finished => bar.println(console::style(format!("=> DONE {}", job)).green().to_string()),
JobState::Skipped => bar.println(console::style(format!("=> SKIPPED {}", job)).yellow().to_string()),
JobState::Failed => bar.println(console::style(format!("=> FAILED {}", job)).red().to_string()),
_ => panic!("Invalid job state after run: {:?}", state),

}
queue.finished(job);
} else if let Err(e) = job.run(&mut status_writer, &mut log_writer, &options) {
bar.println(console::style(format!("=> FAILED {}: {}", job, e)).red().to_string());
queue.failed(job);
} else {
bar.println(console::style(format!("=> DONE {}", job)).green().to_string());
queue.finished(job);
}
main_bar.inc(1);
} else {
Expand Down

0 comments on commit 7ca8016

Please sign in to comment.