process_manager.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from module.gui.process.script_process import ScriptProcess
  5. from module.gui.context.add import Add
  6. from module.logger import logger
  7. class ProcessManager:
  8. """
  9. 进程管理
  10. """
  11. def __init__(self) -> None:
  12. """
  13. init
  14. """
  15. super().__init__()
  16. self.processes = {}
  17. def create_all(self) -> None:
  18. """
  19. 创建所有的配置实例
  20. :return:
  21. """
  22. configs = Add().all_script_files()
  23. for config in configs:
  24. self.add(config)
  25. def add(self, config: str) -> None:
  26. """
  27. add
  28. :param config:
  29. :return:
  30. """
  31. if config not in self.processes:
  32. self.processes[config] = ScriptProcess(config)
  33. self.processes[config].start()
  34. logger.info(f'add script {config}')
  35. else:
  36. logger.info(f'script {config} is already running')
  37. def remove(self, config: str) -> None:
  38. """
  39. remove
  40. :param config:
  41. :return:
  42. """
  43. if config in self.processes:
  44. self.processes[config].stop()
  45. del self.processes[config]
  46. logger.info(f'remove script {config}')
  47. else:
  48. logger.info(f'script {config} is not running')
  49. def restart(self, config: str) -> None:
  50. """
  51. restart
  52. :param config:
  53. :return:
  54. """
  55. if config in self.processes:
  56. self.processes[config].restart()
  57. logger.info(f'restart script {config}')
  58. else:
  59. logger.info(f'script {config} is not running')
  60. def stop_all(self) -> None:
  61. """
  62. stop_all
  63. :return:
  64. """
  65. for config in self.processes:
  66. self.processes[config].stop()
  67. logger.info(f'stop all script')
  68. def restart_all(self) -> None:
  69. """
  70. restart_all
  71. :return:
  72. """
  73. for config in self.processes:
  74. self.processes[config].restart()
  75. logger.info(f'restart all script')