mod.rs 1.6 KB

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