This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2016 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, and | ||
* you may not use this file except in compliance with the Apache License | ||
* Version 2.0. You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the Apache License Version 2.0 is distributed on an "AS | ||
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the Apache License Version 2.0 for the specific language | ||
* governing permissions and limitations there under. | ||
*/ | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
use chrono::DateTime; | ||
use chrono::UTC; | ||
use chrono::Duration; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Clone)] | ||
pub struct RunResult { | ||
pub run_started: DateTime<UTC>, | ||
pub duration: Duration, | ||
pub requests_job_termination: bool, | ||
pub task_execution_error: Option<String>, | ||
pub stdout: Option<String>, | ||
pub stderr: Option<String>, | ||
pub return_code: i32 | ||
} | ||
|
||
#[derive(Clone, PartialEq, Debug)] | ||
pub enum State { | ||
WAITING, | ||
SUCCESS, | ||
FAILED, | ||
SKIPPED | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct Task<'a, T:'a> { | ||
pub name: String, | ||
pub state: State, | ||
pub children: Vec<&'a[Task<'a, T>]>, | ||
pub task_spec: T, | ||
pub run_result: Option<RunResult> | ||
} | ||
|
||
impl<'a, T> Task<'a, T> { | ||
pub fn new<S: Into<String>>(name: S, task_spec: T) -> Self { | ||
Task { | ||
name: name.into(), | ||
state: State::WAITING, | ||
children: vec![], | ||
task_spec: task_spec, | ||
run_result: None | ||
} | ||
} | ||
} | ||
|
||
pub struct TaskList<'a, T: 'a> { | ||
pub tasks: Vec<Vec<&'a[Task<'a, T>]>> | ||
} | ||
|
||
impl<'a, T> TaskList<'a, T> { | ||
|
||
pub fn new() -> Self { | ||
TaskList { | ||
tasks: vec![] | ||
} | ||
} | ||
|
||
pub fn add_group(&mut self, tasks:Vec<&'a[Task<T>]>) { | ||
self.tasks.push(tasks); | ||
} | ||
|
||
pub fn set_child(&mut self, child:&Task<T>, parent:&Task<T>) -> Result<(), &'static str> { | ||
let mut seen:HashMap<_, _> = HashMap::new(); | ||
for task_grp in self.tasks.iter() { | ||
for task in task_grp { | ||
if task.name == child.name { | ||
if let Some(parent) = seen.get(&parent.name) { | ||
parent.children.push(task); | ||
return Ok(()) | ||
} else { | ||
return Err("Cannot add a child if the parent isn't in an earlier task group") | ||
} | ||
} | ||
seen.insert(task.name.clone(), task); | ||
} | ||
} | ||
Err("nop") | ||
} | ||
|
||
pub fn get_task_by_name(&self, name:&str) -> Option<&Task<T>> { | ||
for task_group in self.tasks.iter() { | ||
for task in task_group.iter() { | ||
if task.name == name { | ||
return Some(task) | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2016 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, and | ||
* you may not use this file except in compliance with the Apache License | ||
* Version 2.0. You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the Apache License Version 2.0 is distributed on an "AS | ||
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the Apache License Version 2.0 for the specific language | ||
* governing permissions and limitations there under. | ||
*/ | ||
|
||
use super::*; | ||
|
||
|
||
#[test] | ||
fn task_new_defaults_good() { | ||
let task = Task::<String>::new("hello", "world".to_string()); | ||
assert_eq!(task.name, "hello"); | ||
assert_eq!(task.state, State::WAITING); | ||
assert_eq!(task.children.len(), 0); | ||
assert_eq!(task.task_spec, "world".to_string()); | ||
assert!(task.run_result.is_some()==false) | ||
} | ||
|
||
#[test] | ||
fn test_get_by_name() { | ||
let mut tl = TaskList::<String>::new(); | ||
assert!(tl.get_task_by_name("banana").is_some()==false); | ||
|
||
tl.add_group(vec![ | ||
Task::<String>::new("hello", "world".to_string()), | ||
Task::<String>::new("yes", "world".to_string()) | ||
]); | ||
|
||
tl.add_group(vec![ | ||
Task::<String>::new("thing", "world".to_string()), | ||
Task::<String>::new("yah", "world".to_string()) | ||
]); | ||
|
||
assert!(tl.get_task_by_name("hello").is_some()); | ||
assert!(tl.get_task_by_name("yes").is_some()); | ||
assert!(tl.get_task_by_name("thing").is_some()); | ||
assert!(tl.get_task_by_name("yah").is_some()); | ||
} |