Skip to content

Commit

Permalink
feat(run): add --starts flag to override default starting tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Jan 20, 2021
1 parent 424687d commit e8c2eab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub struct Run {
/// Configurations path (YAML)
#[structopt()]
config: Vec<PathBuf>,
/// Override the starting task if the job had already been started before.
/// When using many configuration files, start states must be in the first configuration file.
/// Can be many task ids with comma separated values.
#[structopt(long = "starts", short = "s")]
starts: Option<String>,
/// Run the task in background
#[structopt(long = "background", short = "b")]
background: bool,
Expand All @@ -35,15 +40,24 @@ impl Run {
unsafe { signal(SIGHUP, SIG_IGN) };
}

for config in &self.config {
if let Err(e) = self.run(&config.as_path()) {
let starts: Vec<String> = if let Some(starts) = &self.starts {
starts
.split(",")
.map(|s| s.to_string())
.collect::<Vec<String>>()
} else {
vec![]
};
for (i, config) in self.config.iter().enumerate() {
let starts = if i == 0 { starts.clone() } else { vec![] };
if let Err(e) = self.run(&config.as_path(), &starts) {
eprintln!("{}", e);
exit(1);
}
}
}

fn run(&self, config: &Path) -> Result<(), String> {
fn run(&self, config: &Path, starts: &Vec<String>) -> Result<(), String> {
let yaml =
fs::read_to_string(config).map_err(|msg| format!("Can't read the config file: {}", msg))?;

Expand All @@ -56,7 +70,9 @@ impl Run {
}

for task in config.tasks().values() {
if task.depends_on().len() == 0 {
if (task.depends_on().len() == 0 && starts.len() == 0)
|| (starts.len() > 0 && starts.contains(task.id()))
{
graph.add_start_state(task.state());
} else {
for prev in task.depends_on().iter() {
Expand All @@ -81,6 +97,16 @@ impl Run {
let mut exit_failure = 0;
let mut ask_for_exit = false;
let graph_iter = &mut graph.iter();

if starts.len() != 0 {
graph
.reachable_states()
.iter()
.enumerate()
.filter(|(_, reachable)| !*reachable)
.for_each(|(state, _)| graph_iter.set_done(state));
}

loop {
if graph_iter.has_next()
&& (graph_iter.n_in_progress() < config.concurrency() || config.concurrency() < 0)
Expand Down
4 changes: 4 additions & 0 deletions src/fst/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ impl TaskIter {
}
}

pub fn set_done(&mut self, state: usize) {
self.states[state] = TaskStatus::Done;
}

pub fn n_in_progress(&self) -> i64 {
self
.states
Expand Down

0 comments on commit e8c2eab

Please sign in to comment.