Skip to content

Commit

Permalink
refactor: update concurrency example and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Oct 13, 2023
1 parent a67ed4d commit 0918877
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 79 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ Options:
-h, --help Print help
```

Example taken from [tests/resources/concurrency.yml](https://github.com/Joxit/runtasktic/blob/main/tests/resources/concurrency.yml) of graph generated by dot.

![Dot Sample](./dot-sample.png)

### Completion: Generate completion script for your shell

```
Expand Down
Binary file added dot-sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/commands/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Dot {

let mut graph = TaskFst::new();
for task in config.tasks_values_mut() {
task.set_state(graph.add_state(task.id().to_owned()));
task.set_state(graph.add_state(task.id()));
}

for task in config.tasks().values() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Run {

let mut graph = TaskFst::new();
for task in config.tasks_values_mut() {
task.set_state(graph.add_state(task.id().to_owned()));
task.set_state(graph.add_state(task.id()));
}

for task in config.tasks().values() {
Expand Down
12 changes: 6 additions & 6 deletions src/config/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ pub struct Task {
}

impl Task {
pub fn new(
id: String,
pub fn new<S: Into<String>>(
id: S,
commands: Vec<String>,
depends_on: Vec<String>,
on_failure: Option<OnFailure>,
) -> Task {
Task {
id: id,
commands: commands,
depends_on: depends_on,
on_failure: on_failure,
id: id.into(),
commands,
depends_on,
on_failure,
state: 0,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/yaml_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ impl YamlTasksScheduler for LinkedHashMap<Yaml, Yaml> {
if let Some(tasks) = tasks.as_hash() {
let mut result = HashMap::new();
for (id, task) in tasks.iter() {
let id = id.as_str().ok_or("Task ids must be strings".to_string())?;
let id = id.as_str().ok_or("Task ids must be strings")?;
let commands = task.get_commands();
let depends_on = task.get_depends_on();
let on_failure = task.get_on_failure()?;
result.insert(
id.to_string(),
Task::new(id.to_string(), commands, depends_on, on_failure),
Task::new(id, commands, depends_on, on_failure),
);
}
return Ok(result);
Expand Down
10 changes: 5 additions & 5 deletions src/fst/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ mod test {
#[test]
fn dot_write_file() {
let mut fst = TaskFst::new();
fst.add_state("\"a\"".to_string());
fst.add_state("b a ba".to_string());
fst.add_state("\"a\"");
fst.add_state("b a ba");
fst.add_arc(0, 1);
fst.add_start_state(0);

fst.add_state("c".to_string());
fst.add_state("c");
fst.add_arc(0, 2);
fst.add_arc(1, 2);

fst.add_state("d%s".to_string());
fst.add_state("d%s");
fst.add_arc(2, 3);

fst.add_state("e".to_string());
fst.add_state("e");
fst.add_start_state(4);

fst.add_arc(4, 3);
Expand Down
18 changes: 9 additions & 9 deletions src/fst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ impl TaskFst {
self.states.len()
}

pub fn add_state(&mut self, label: String) -> usize {
pub fn add_state<S: Into<String>>(&mut self, label: S) -> usize {
self.states.push(TaskFstState {
label: label,
label: label.into(),
id: self.states.len(),
next: vec![],
prev: vec![],
Expand Down Expand Up @@ -137,8 +137,8 @@ mod test {
#[test]
pub fn add_arc() {
let mut fst = TaskFst::new();
fst.add_state("a".to_string());
fst.add_state("b".to_string());
fst.add_state("a");
fst.add_state("b");
fst.add_arc(0, 1);

assert_eq!(
Expand Down Expand Up @@ -166,8 +166,8 @@ mod test {
#[test]
pub fn is_cyclic_and_reachable() {
let mut fst = TaskFst::new();
fst.add_state("a".to_string());
fst.add_state("b".to_string());
fst.add_state("a");
fst.add_state("b");

fst.add_arc(0, 1);
assert_eq!(fst.reachable_states(), vec![false, false]);
Expand All @@ -176,7 +176,7 @@ mod test {

assert!(!fst.is_cyclic());

fst.add_state("c".to_string());
fst.add_state("c");

assert_eq!(fst.reachable_states(), vec![true, true, false]);
fst.add_arc(0, 2);
Expand All @@ -185,13 +185,13 @@ mod test {
fst.add_arc(1, 2);
assert!(!fst.is_cyclic());

fst.add_state("d".to_string());
fst.add_state("d");
assert_eq!(fst.reachable_states(), vec![true, true, true, false]);
fst.add_arc(2, 3);
assert_eq!(fst.reachable_states(), vec![true, true, true, true]);
assert!(!fst.is_cyclic());

fst.add_state("e".to_string());
fst.add_state("e");
assert_eq!(fst.reachable_states(), vec![true, true, true, true, false]);
fst.add_start_state(4);
assert_eq!(fst.reachable_states(), vec![true, true, true, true, true]);
Expand Down
101 changes: 52 additions & 49 deletions tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,9 @@ fn sample_yaml() {
let config = Config::from_str(yaml.as_str()).unwrap();

let a = Task::new("a".to_string(), vec![echo("a"), sleep("0.5")], vec![], None);
let b = Task::new(
"b".to_string(),
vec![echo("b"), sleep("0.5")],
vec!["a".to_string()],
None,
);
let c = Task::new(
"c".to_string(),
vec![echo("c")],
vec!["a".to_string()],
None,
);
let d = Task::new(
"d".to_string(),
vec![echo("d")],
vec!["b".to_string(), "c".to_string()],
None,
);
let b = Task::new("b", vec![echo("b"), sleep("0.5")], vs(&["a"]), None);
let c = Task::new("c", vec![echo("c")], vs(&["a"]), None);
let d = Task::new("d", vec![echo("d")], vs(&["b", "c"]), None);

assert_eq!(*config.notification(), None);
assert_eq!(config.concurrency(), -1);
Expand All @@ -51,45 +36,59 @@ fn concurrency_yaml() {
let yaml = fs::read_to_string(CONCURRENCY_YAML).unwrap();
let config = Config::from_str(yaml.as_str()).unwrap();

let a = Task::new(
"a".to_string(),
vec![echo("Begin a"), sleep("0.5"), echo("End a")],
let a1 = Task::new(
"a1",
vec![echo("Begin a1"), sleep("0.5"), echo("End a1")],
vec![],
None,
);
let a2 = Task::new(
"a2",
vec![echo("Begin a2"), sleep("1"), echo("End a2")],
vec![],
None,
);
let b = Task::new(
"b".to_string(),
"b",
vec![echo("Begin b"), sleep("0.5"), echo("End b")],
vec!["a".to_string()],
vs(&["a1", "a2"]),
None,
);
let c = Task::new(
"c".to_string(),
"c",
vec![echo("Begin c"), sleep("1"), echo("End c")],
vec!["a".to_string()],
vs(&["a1"]),
None,
);
let d = Task::new(
"d".to_string(),
"d",
vec![echo("Begin d"), sleep("0.5"), echo("End d")],
vec!["a".to_string()],
vs(&["a1"]),
None,
);
let e = Task::new(
"e".to_string(),
"e",
vec![echo("Begin e"), sleep("0.5"), echo("End e")],
vec!["b".to_string(), "c".to_string(), "d".to_string()],
vs(&["b", "c", "d"]),
None,
);
let f = Task::new(
"f",
vec![echo("Begin f"), sleep("1"), echo("End f")],
vs(&["c"]),
None,
);

assert_eq!(*config.notification(), None);
assert_eq!(config.concurrency(), 2);
assert_eq!(config.tasks().len(), 5);
assert_eq!(config.tasks().get(&"a".to_string()), Some(&a));
assert_eq!(config.tasks().len(), 7);
assert_eq!(config.tasks().get(&"a1".to_string()), Some(&a1));
assert_eq!(config.tasks().get(&"a2".to_string()), Some(&a2));
assert_eq!(config.tasks().get(&"b".to_string()), Some(&b));
assert_eq!(config.tasks().get(&"c".to_string()), Some(&c));
assert_eq!(config.tasks().get(&"d".to_string()), Some(&d));
assert_eq!(config.tasks().get(&"e".to_string()), Some(&e));
assert_eq!(config.tasks().get(&"f".to_string()), Some(&f));
}

#[test]
Expand All @@ -98,33 +97,33 @@ fn notification_yaml() {
let config = Config::from_str(yaml.as_str()).unwrap();

let a = Task::new(
"a".to_string(),
"a",
vec![echo("Begin a"), sleep("0.5"), echo("End a")],
vec![],
None,
);
let b = Task::new(
"b".to_string(),
"b",
vec![echo("Begin b"), sleep("0.5"), echo("End b")],
vec!["a".to_string()],
vs(&["a"]),
None,
);
let c = Task::new(
"c".to_string(),
"c",
vec![echo("Begin c"), sleep("1"), echo("End c")],
vec!["a".to_string()],
vs(&["a"]),
None,
);
let d = Task::new(
"d".to_string(),
"d",
vec![echo("Begin d"), sleep("0.5"), echo("End d")],
vec!["a".to_string()],
vs(&["a"]),
None,
);
let e = Task::new(
"e".to_string(),
"e",
vec![echo("Begin e"), sleep("0.5"), echo("End e")],
vec!["b".to_string(), "c".to_string(), "d".to_string()],
vs(&["b", "c", "d"]),
None,
);

Expand Down Expand Up @@ -159,33 +158,33 @@ fn on_failure_yaml() {
let config = Config::from_str(yaml.as_str()).unwrap();

let a = Task::new(
"a".to_string(),
"a",
vec![echo("Begin a"), format!("unknown-cmd"), echo("End a")],
vec![],
Some(OnFailure::Continue),
);
let b = Task::new(
"b".to_string(),
"b",
vec![echo("Begin b"), format!("unknown-cmd"), echo("End b")],
vec!["a".to_string()],
vs(&["a"]),
Some(OnFailure::Exit),
);
let c = Task::new(
"c".to_string(),
"c",
vec![echo("Begin c"), sleep("1"), echo("End c")],
vec!["a".to_string()],
vs(&["a"]),
None,
);
let d = Task::new(
"d".to_string(),
"d",
vec![echo("Begin d"), sleep("0.5"), echo("End d")],
vec!["a".to_string()],
vs(&["a"]),
None,
);
let e = Task::new(
"e".to_string(),
"e",
vec![echo("Begin e"), sleep("0.5"), echo("End e")],
vec!["b".to_string(), "c".to_string(), "d".to_string()],
vs(&["b", "c", "d"]),
None,
);

Expand All @@ -207,3 +206,7 @@ fn echo(msg: &str) -> String {
fn sleep(time: &str) -> String {
format!("sleep {}", time)
}

fn vs(vec: &[&str]) -> Vec<String> {
vec.iter().map(|s| s.to_string()).collect()
}
23 changes: 17 additions & 6 deletions tests/resources/concurrency.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
tasks:
a:
a1:
commands:
- echo Begin a
- echo Begin a1
- sleep 0.5
- echo End a
- echo End a1
a2:
commands:
- echo Begin a2
- sleep 1
- echo End a2
b:
commands:
- echo Begin b
- sleep 0.5
- echo End b
depends_on: [ a ]
depends_on: [ a1, a2 ]
c:
commands:
- echo Begin c
- sleep 1
- echo End c
depends_on: [ a ]
depends_on: [ a1 ]
d:
commands:
- echo Begin d
- sleep 0.5
- echo End d
depends_on: [ a ]
depends_on: [ a1 ]
e:
commands:
- echo Begin e
- sleep 0.5
- echo End e
depends_on: [ b, c, d ]
f:
commands:
- echo Begin f
- sleep 1
- echo End f
depends_on: [ c ]
concurrency: 2

0 comments on commit 0918877

Please sign in to comment.