xref: /DragonReach/src/executor/mod.rs (revision 56797478d93fa8dbba9da9f520575e529b5f3326)
1 #[cfg(target_os = "dragonos")]
2 use drstd as std;
3 
4 pub mod dep_graph;
5 pub mod service_executor;
6 
7 use std::sync::Arc;
8 use std::{process::Child, sync::Mutex};
9 
10 use crate::{
11     error::runtime_error::{RuntimeError, RuntimeErrorType},
12     manager::UnitManager,
13     unit::Unit,
14 };
15 
16 use self::dep_graph::DepGraph;
17 
18 #[derive(Debug, Clone, Copy)]
19 pub enum ExitStatus {
20     Success,
21     Failure,
22     Abnormal,
23     Abort,
24     Watchdog,
25 }
26 
27 impl ExitStatus {
28     /// ## 从错误码获得退出状态
29     ///
30     /// 注意,该方法只会返回Success(exit_code == 0)和Abnormal(exit_code != 0)两种状态
31     /// 其他DragonReach定义的退出状态需要手动指定
32     ///
33     /// ### return Success(exit_code == 0)、Abnormal(exit_code != 0)
34     pub fn from_exit_code(exit_code: i32) -> Self {
35         match exit_code {
36             0 => return Self::Success,
37             _ => return Self::Abnormal,
38         }
39     }
40 }
41 
42 //Unit的全局执行器
43 pub struct Executor;
44 
45 impl Executor {
46     pub fn exec(unit: &Arc<Mutex<dyn Unit>>) -> Result<(), RuntimeError> {
47         //TODO: 优化此处,解析时也用到了拓扑排序,尝试使用那次拓扑排序的结果
48         let mut graph = DepGraph::construct_graph(unit);
49 
50         let sort_ret = graph.topological_sort()?;
51         for u in sort_ret {
52             if UnitManager::is_running_unit(&u) {
53                 continue;
54             }
55 
56             let mutex = UnitManager::get_unit_with_id(&u).unwrap();
57             let mut unit = mutex.lock().unwrap();
58             if let Err(e) = unit.run() {
59                 return Err(e);
60             }
61         }
62         return Ok(());
63     }
64 }
65