main.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #![allow(non_snake_case)]
  2. mod contants;
  3. mod error;
  4. mod executor;
  5. mod manager;
  6. mod parse;
  7. mod systemctl;
  8. mod task;
  9. mod time;
  10. mod unit;
  11. use error::ErrorFormat;
  12. use executor::Executor;
  13. use manager::{timer_manager::TimerManager, Manager};
  14. use parse::UnitParser;
  15. use systemctl::listener::Systemctl;
  16. pub struct FileDescriptor(usize);
  17. const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
  18. fn main() {
  19. // 初始化
  20. Systemctl::init();
  21. let mut units_file_name = Vec::new();
  22. //读取目录里面的unit文件
  23. if let Ok(entries) = std::fs::read_dir(DRAGON_REACH_UNIT_DIR) {
  24. for entry in entries {
  25. if let Ok(entry) = entry {
  26. if let Ok(file_type) = entry.file_type() {
  27. if file_type.is_file() {
  28. let filename = entry.file_name().to_str().unwrap().to_string();
  29. units_file_name.push(filename);
  30. }
  31. }
  32. }
  33. }
  34. }
  35. //启动服务
  36. for path in units_file_name {
  37. let id = match UnitParser::from_path(&path) {
  38. Ok(id) => id,
  39. Err(e) => {
  40. eprintln!("Err:{}", e.error_format());
  41. 0
  42. }
  43. };
  44. if id != 0 {
  45. if let Err(e) = Executor::exec(id) {
  46. eprintln!("Err:{}", e.error_format());
  47. }
  48. }
  49. }
  50. // 启动完服务后进入主循环
  51. loop {
  52. // 检查各服务运行状态
  53. Manager::check_running_status();
  54. // 检查cmd进程状态
  55. Manager::check_cmd_proc();
  56. // 检查计时器任务
  57. TimerManager::check_timer();
  58. // 监听systemctl
  59. Systemctl::ctl_listen();
  60. }
  61. }