script_process.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # 脚本进程
  4. # github https://github.com/runhey
  5. import multiprocessing
  6. from asyncio import QueueEmpty, CancelledError, sleep
  7. from enum import Enum
  8. from module.logger import logger
  9. from module.server.script_websocket import ScriptWSManager
  10. class ScriptState(int, Enum):
  11. INACTIVE = 0
  12. RUNNING = 1
  13. WARNING = 2
  14. UPDATING = 3
  15. class ScriptProcess(ScriptWSManager):
  16. def __init__(self, config_name: str) -> None:
  17. super().__init__()
  18. self.config_name = config_name # config_name
  19. self.log_pipe_out, self.log_pipe_in = multiprocessing.Pipe(False)
  20. self.state_queue = multiprocessing.Queue()
  21. self.state: ScriptState = ScriptState.INACTIVE
  22. self._process = None
  23. async def start(self):
  24. self.state = ScriptState.RUNNING
  25. await self.broadcast_state({"state": self.state})
  26. if self._process:
  27. logger.warning(f'Script {self.config_name} is initialized')
  28. if self._process and self._process.is_alive():
  29. logger.warning(f'Script {self.config_name} is already running and first stop it')
  30. self.stop()
  31. self._process = multiprocessing.Process(target=func,
  32. args=(self.config_name, self.state_queue, self.log_pipe_in,),
  33. name=self.config_name,
  34. daemon=True)
  35. self._process.start()
  36. async def stop(self):
  37. self.state = ScriptState.INACTIVE
  38. await self.broadcast_state({"state": self.state})
  39. if self._process is None:
  40. logger.warning(f'Script {self.config_name} process is removed')
  41. return
  42. if not self._process.is_alive():
  43. logger.warning(f'Script {self.config_name} is not running')
  44. return
  45. self._process.terminate()
  46. self._process = None
  47. async def coroutine_broadcast_state(self):
  48. try:
  49. while 1:
  50. if self.state == ScriptState.INACTIVE:
  51. await sleep(1)
  52. continue
  53. await sleep(0.1)
  54. try:
  55. if self.state_queue.empty():
  56. await sleep(1)
  57. continue
  58. data = self.state_queue.get_nowait()
  59. if not data:
  60. await sleep(0.5)
  61. continue
  62. await self.broadcast_state(data)
  63. except QueueEmpty as e:
  64. logger.warning(f'QueueEmpty: {e}')
  65. await sleep(0.5)
  66. continue
  67. except Exception as e:
  68. logger.error(f'Error: {e}')
  69. continue
  70. except CancelledError as e:
  71. logger.warning(f'{self.config_name} state coroutine is cancelled')
  72. return
  73. async def coroutine_broadcast_log(self):
  74. try:
  75. while 1:
  76. if self.state == ScriptState.INACTIVE:
  77. await sleep(1)
  78. continue
  79. await sleep(0.05)
  80. try:
  81. if not self.log_pipe_out.poll():
  82. await sleep(0.3)
  83. continue
  84. log = self.log_pipe_out.recv()
  85. if not log:
  86. await sleep(0.5)
  87. continue
  88. await self.broadcast_log(log)
  89. except EOFError as e:
  90. await sleep(0.5)
  91. logger.warning(f'EOFError: {e}')
  92. continue
  93. except Exception as e:
  94. logger.error(f'Log Error: {e}')
  95. continue
  96. except CancelledError as e:
  97. logger.warning(f'{self.config_name} log coroutine is cancelled')
  98. return
  99. def func(config: str, state_queue: multiprocessing.Queue, log_pipe_in) -> None:
  100. def start_log() -> None:
  101. try:
  102. from module.logger import set_file_logger, set_func_logger
  103. set_file_logger(name=config)
  104. set_func_logger(log_pipe_in.send)
  105. except Exception as e:
  106. logger.exception(f'Start log error')
  107. logger.error(f'Error: {e}')
  108. raise
  109. start_log()
  110. logger.hr(f'Script [{config}] process start')
  111. import time
  112. try:
  113. # while 1:
  114. # time.sleep(1)
  115. # logger.info(f'Script {config} is running')
  116. # state_queue.put({"state": ScriptState.RUNNING})
  117. from script import Script
  118. script = Script(config_name=config)
  119. script.state_queue = state_queue
  120. script.loop()
  121. except SystemExit as e:
  122. logger.info(f'Script {config} process exit')
  123. logger.error(f'Error: {e}')
  124. state_queue.put({"state": ScriptState.WARNING})
  125. time.sleep(0.1)
  126. exit(-1)
  127. except Exception as e:
  128. logger.exception(f'Run script {config} error')
  129. logger.error(f'Error: {e}')
  130. raise
  131. if __name__ == '__main__':
  132. p = ScriptProcess('oas1')
  133. p.start()
  134. from time import sleep
  135. sleep(10)
  136. logger.info(p._process.exitcode)