| 17 | UPDATING = 3 |
| 18 | |
| 19 | class ScriptProcess(ScriptWSManager): |
| 20 | |
| 21 | def __init__(self, config_name: str) -> None: |
| 22 | super().__init__() |
| 23 | self.config_name = config_name # config_name |
| 24 | self.log_pipe_out, self.log_pipe_in = multiprocessing.Pipe(False) |
| 25 | self.state_queue = multiprocessing.Queue() |
| 26 | self.state: ScriptState = ScriptState.INACTIVE |
| 27 | self._process = None |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | async def start(self): |
| 33 | self.state = ScriptState.RUNNING |
| 34 | await self.broadcast_state({"state": self.state}) |
| 35 | if self._process: |
| 36 | logger.warning(f'Script {self.config_name} is initialized') |
| 37 | if self._process and self._process.is_alive(): |
| 38 | logger.warning(f'Script {self.config_name} is already running and first stop it') |
| 39 | self.stop() |
| 40 | self._process = multiprocessing.Process(target=func, |
| 41 | args=(self.config_name, self.state_queue, self.log_pipe_in,), |
| 42 | name=self.config_name, |
| 43 | daemon=True) |
| 44 | self._process.start() |
| 45 | |
| 46 | |
| 47 | async def stop(self): |
| 48 | self.state = ScriptState.INACTIVE |
| 49 | await self.broadcast_state({"state": self.state}) |
| 50 | if self._process is None: |
| 51 | logger.warning(f'Script {self.config_name} process is removed') |
| 52 | return |
| 53 | if not self._process.is_alive(): |
| 54 | logger.warning(f'Script {self.config_name} is not running') |
| 55 | return |
| 56 | self._process.terminate() |
| 57 | self._process = None |
| 58 | |
| 59 | async def coroutine_broadcast_state(self): |
| 60 | try: |
| 61 | while 1: |
| 62 | if self.state == ScriptState.INACTIVE: |
| 63 | await sleep(1) |
| 64 | continue |
| 65 | await sleep(0.1) |
| 66 | try: |
| 67 | if self.state_queue.empty(): |
| 68 | await sleep(1) |
| 69 | continue |
| 70 | data = self.state_queue.get_nowait() |
| 71 | if not data: |
| 72 | await sleep(0.5) |
| 73 | continue |
| 74 | if 'state' in data and data['state'] == ScriptState.WARNING: |
| 75 | self.state = ScriptState.WARNING |
| 76 | await self.broadcast_state(data) |
no outgoing calls
no test coverage detected