Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
WIP #60 idea lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjabear committed Sep 22, 2016
1 parent 455dbf7 commit 7310878
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/factotum/executor/execution_strategy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
mod tests;
use super::*;
use std::process::Command;
use chrono::DateTime;
use chrono::UTC;
use std::time::{Duration, Instant};
use std::time::{Duration};

pub struct CommandRequest {
name: String,
Expand Down
1 change: 1 addition & 0 deletions src/factotum/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use chrono::DateTime;
use chrono::UTC;

mod execution_strategy;
mod task_list;

enum TaskResult {
Ok(i32, Duration),
Expand Down
110 changes: 110 additions & 0 deletions src/factotum/executor/task_list/mod.rs
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
}


}

48 changes: 48 additions & 0 deletions src/factotum/executor/task_list/tests.rs
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());
}

0 comments on commit 7310878

Please sign in to comment.