diff --git a/.gitignore b/.gitignore index bfe70e6..311329f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,8 @@ Cargo.lock # IDE .idea -.vscode +/.vscode/* +!/.vscode/settings.json # Exclude execute log -/*.log \ No newline at end of file +/*.log diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..39a5ca1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.cargo.features": "all" +} diff --git a/README.md b/README.md index 880b3ea..0e206b8 100644 --- a/README.md +++ b/README.md @@ -55,18 +55,21 @@ Users need to program to implement the `Action` trait to define the specific log - `predecessor_tasks`: the predecessor tasks of this task - `action`: is a dynamic type that implements the Action trait in user programming, and it is the specific logic to be executed by the task + +Here is the `examples/impl_action.rs` example: + ```rust +//! Implement the Action trait to define the task logic. + use std::sync::Arc; -use dagrs::{ - Action, - Dag, DefaultTask, EnvVar,log, Input, Output, RunningError,LogLevel -}; +use dagrs::{log, Action, Dag, DefaultTask, EnvVar, Input, LogLevel, Output, RunningError}; struct SimpleAction(usize); + /// Implement the `Action` trait for `SimpleAction`, defining the logic of the `run` function. -/// The logic here is simply to get the output value (`usize`) of all predecessor tasks and then accumulate. -impl Action for SimpleAction{ - fn run(&self, input: Input,env:Arc) -> Result { +/// The logic here is simply to get the output value (usize) of all predecessor tasks and then accumulate. +impl Action for SimpleAction { + fn run(&self, input: Input, env: Arc) -> Result { let base = env.get::("base").unwrap(); let mut sum = self.0; input @@ -76,27 +79,29 @@ impl Action for SimpleAction{ } } -// Initialize the global logger -let _initialized = log::init_logger(LogLevel::Info,None); -// Generate four tasks. -let a= DefaultTask::new(SimpleAction(10),"Task a"); -let mut b=DefaultTask::new(SimpleAction(20),"Task b"); -let mut c=DefaultTask::new(SimpleAction(30),"Task c"); -let mut d=DefaultTask::new(SimpleAction(40),"Task d"); -// Set the precursor for each task. -b.set_predecessors(&[&a]); -c.set_predecessors(&[&a]); -d.set_predecessors(&[&b,&c]); -// Take these four tasks as a Dag. -let mut dag=Dag::with_tasks(vec![a,b,c,d]); -// Set a global environment variable for this dag. -let mut env = EnvVar::new(); -env.set("base", 2usize); -dag.set_env(env); -// Begin execution. -assert!(dag.start().unwrap()); -// Get execution result -assert_eq!(dag.get_result::().unwrap(),220); +fn main() { + // Initialize the global logger + let _initialized = log::init_logger(LogLevel::Info, None); + // Generate four tasks. + let a = DefaultTask::new(SimpleAction(10), "Task a"); + let mut b = DefaultTask::new(SimpleAction(20), "Task b"); + let mut c = DefaultTask::new(SimpleAction(30), "Task c"); + let mut d = DefaultTask::new(SimpleAction(40), "Task d"); + // Set the precursor for each task. + b.set_predecessors(&[&a]); + c.set_predecessors(&[&a]); + d.set_predecessors(&[&b, &c]); + // Take these four tasks as a Dag. + let mut dag = Dag::with_tasks(vec![a, b, c, d]); + // Set a global environment variable for this dag. + let mut env = EnvVar::new(); + env.set("base", 2usize); + dag.set_env(env); + // Begin execution. + assert!(dag.start().unwrap()); + // Get execution result + assert_eq!(dag.get_result::().unwrap(), 220); +} ``` **explain:** diff --git a/dagrs_core/Cargo.toml b/dagrs_core/Cargo.toml index 28cef64..65be3be 100644 --- a/dagrs_core/Cargo.toml +++ b/dagrs_core/Cargo.toml @@ -2,6 +2,7 @@ name = "dagrs_core" version = "0.3.0" edition = "2021" +license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -21,4 +22,4 @@ yaml = [] [[bin]] name = "dagrs" -required-features = ["yaml"] \ No newline at end of file +required-features = ["yaml"] diff --git a/dagrs_core/src/engine/dag.rs b/dagrs_core/src/engine/dag.rs index 5705966..d5c6149 100644 --- a/dagrs_core/src/engine/dag.rs +++ b/dagrs_core/src/engine/dag.rs @@ -90,7 +90,7 @@ impl Dag { } /// Create a dag by adding a series of tasks. - pub fn with_tasks(tasks: Vec) -> Dag { + pub fn with_tasks(tasks: Vec) -> Self { let mut dag = Dag::new(); tasks.into_iter().for_each(|task| { let task = Box::new(task) as Box; @@ -103,7 +103,7 @@ impl Dag { #[cfg(feature = "yaml")] pub fn with_yaml( file: &str, - specific_actions: HashMap>, + specific_actions: HashMap>, ) -> Result { use crate::YamlParser; let parser = Box::new(YamlParser); @@ -114,7 +114,7 @@ impl Dag { pub fn with_config_file_and_parser( file: &str, parser: Box, - specific_actions: HashMap>, + specific_actions: HashMap>, ) -> Result { Dag::read_tasks(file, parser, specific_actions) } @@ -124,7 +124,7 @@ impl Dag { fn read_tasks( file: &str, parser: Box, - specific_actions: HashMap>, + specific_actions: HashMap>, ) -> Result { let mut dag = Dag::new(); let tasks = parser.parse_tasks(file, specific_actions)?; diff --git a/dagrs_core/src/parser/mod.rs b/dagrs_core/src/parser/mod.rs index a611406..6b9050b 100644 --- a/dagrs_core/src/parser/mod.rs +++ b/dagrs_core/src/parser/mod.rs @@ -73,6 +73,6 @@ pub trait Parser { fn parse_tasks( &self, file: &str, - specific_actions: HashMap>, + specific_actions: HashMap>, ) -> Result>, ParserError>; } diff --git a/dagrs_core/src/parser/yaml_parser.rs b/dagrs_core/src/parser/yaml_parser.rs index 645eb59..2358dcf 100644 --- a/dagrs_core/src/parser/yaml_parser.rs +++ b/dagrs_core/src/parser/yaml_parser.rs @@ -37,7 +37,7 @@ impl YamlParser { &self, id: &str, item: &Yaml, - specific_action: Option>, + specific_action: Option>, ) -> Result { // Get name first let name = item["name"] @@ -63,7 +63,7 @@ impl YamlParser { id, precursors, name, - Arc::new(CommandAction::new(cmd)) as Arc, + Arc::new(CommandAction::new(cmd)) as Arc, )) } } @@ -73,7 +73,7 @@ impl Parser for YamlParser { fn parse_tasks( &self, file: &str, - mut specific_actions: HashMap>, + mut specific_actions: HashMap>, ) -> Result>, ParserError> { let content = self.load_file(file)?; // Parse Yaml diff --git a/dagrs_core/src/task/default_task.rs b/dagrs_core/src/task/default_task.rs index f46cfa8..1d2e507 100644 --- a/dagrs_core/src/task/default_task.rs +++ b/dagrs_core/src/task/default_task.rs @@ -49,6 +49,39 @@ impl DefaultTask { } } + /// Allocate a new [`DefaultTask`] from any action and name. + /// The specific task behavior is a structure that implements [`Action`]. + /// + /// # Example + /// + /// ```rust + /// use dagrs::{DefaultTask, Output,Input, Action,EnvVar,RunningError}; + /// use std::sync::Arc; + /// struct SimpleAction(usize); + /// + /// impl Action for SimpleAction { + /// fn run(&self, input: Input, env: Arc) -> Result { + /// Ok(Output::new(self.0 + 10)) + /// } + /// } + /// + /// let action = Arc::new(SimpleAction(10)); + /// let task = DefaultTask::from(action, String::from("Increment action")); + /// ``` + /// + /// `SimpleAction` is a struct that impl [`Action`]. Since task will be + /// executed in separated threads, [`Send`] and [`Sync`] is needed. + /// + /// **Note:** This method will take the ownership of struct that impl [`Action`]. + pub fn from(action: Arc, name: String) -> Self { + DefaultTask { + id: ID_ALLOCATOR.alloc(), + action, + name, + precursors: Vec::new(), + } + } + /// Tasks that shall be executed before this one. /// /// # Example diff --git a/dagrs_core/src/task/yaml_task.rs b/dagrs_core/src/task/yaml_task.rs index 545ad4a..c709cd5 100644 --- a/dagrs_core/src/task/yaml_task.rs +++ b/dagrs_core/src/task/yaml_task.rs @@ -30,7 +30,7 @@ impl YamlTask { yaml_id: &str, precursors: Vec, name: String, - action: Arc, + action: Arc, ) -> Self { Self { yid: yaml_id.to_owned(), diff --git a/dagrs_derive/Cargo.toml b/dagrs_derive/Cargo.toml index a7610cc..82b3ae2 100644 --- a/dagrs_derive/Cargo.toml +++ b/dagrs_derive/Cargo.toml @@ -2,6 +2,7 @@ name = "dagrs_derive" version = "0.3.0" edition = "2021" +license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/examples/custom_parser.rs b/examples/custom_parser.rs index 0af0a89..40b2591 100644 --- a/examples/custom_parser.rs +++ b/examples/custom_parser.rs @@ -33,14 +33,14 @@ impl MyTask { txt_id: &str, precursors: Vec, name: String, - action: impl Action + Send + Sync + 'static, + action: Arc, ) -> Self { Self { tid: (txt_id.to_owned(), dagrs::alloc_id()), name, precursors, precursors_id: Vec::new(), - action: Arc::new(action), + action, } } @@ -92,19 +92,19 @@ impl ConfigParser { } fn parse_one(&self, item: String) -> MyTask { - let attr: Vec<&str> = item.split(",").collect(); + let attr: Vec<&str> = item.split(',').collect(); let pres_item = *attr.get(2).unwrap(); let pres = if pres_item.eq("") { Vec::new() } else { - pres_item.split(" ").map(|pre| pre.to_string()).collect() + pres_item.split(' ').map(|pre| pre.to_string()).collect() }; - let id = *attr.get(0).unwrap(); + let id = *attr.first().unwrap(); let name = attr.get(1).unwrap().to_string(); let cmd = *attr.get(3).unwrap(); - MyTask::new(id, pres, name, CommandAction::new(cmd)) + MyTask::new(id, pres, name, Arc::new(CommandAction::new(cmd))) } } diff --git a/examples/custom_task.rs b/examples/custom_task.rs index 4c40a33..a49627a 100644 --- a/examples/custom_task.rs +++ b/examples/custom_task.rs @@ -13,10 +13,10 @@ struct MyTask { } impl MyTask { - pub fn new(action: impl Action + 'static + Send + Sync, name: &str) -> Self { + pub fn new(action: Arc, name: &str) -> Self { MyTask { id: alloc_id(), - action: Arc::new(action), + action, name: name.to_owned(), predecessor_tasks: Vec::new(), } @@ -59,7 +59,7 @@ macro_rules! generate_task { Ok(Output::new(sum)) } } - MyTask::new($action($val), $name) + MyTask::new(Arc::new($action($val)), $name) }}; } diff --git a/examples/dependencies.rs b/examples/dependencies.rs index d1a5d6c..0cb6585 100644 --- a/examples/dependencies.rs +++ b/examples/dependencies.rs @@ -1,5 +1,5 @@ use dagrs_core::{ - log, Action, Dag, DefaultTask, EnvVar, Input, LogLevel, Output, RunningError, Task, + log, DefaultTask, EnvVar, LogLevel, }; use dagrs_derive::dependencies; diff --git a/examples/engine.rs b/examples/engine.rs index 7504fff..ef33463 100644 --- a/examples/engine.rs +++ b/examples/engine.rs @@ -2,10 +2,10 @@ extern crate dagrs; -use std::{collections::HashMap, sync::Arc}; +use std::{collections::HashMap}; use dagrs::{ - gen_task, log, Action, Dag, DefaultTask, Engine, EnvVar, Input, LogLevel, Output, RunningError, + gen_task, log, Dag, DefaultTask, Engine, LogLevel, }; fn main() { // initialization log. diff --git a/tests/dag_job_test.rs b/tests/dag_job_test.rs index d338fa3..afbd1cf 100644 --- a/tests/dag_job_test.rs +++ b/tests/dag_job_test.rs @@ -1,6 +1,7 @@ +//! Some tests of the dag engine. + use std::{collections::HashMap, sync::Arc}; -///! Some tests of the dag engine. use dagrs::{ log, Action, Dag, DagError, DefaultTask, EnvVar, Input, LogLevel, Output, RunningError, };