2024-07-31 02:28:25 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
|
|
|
|
|
|
|
|
|
@Time : 2024/7/27 上午11:12
|
|
|
|
|
@Author : snowykami
|
|
|
|
|
@Email : snowykami@outlook.com
|
|
|
|
|
@File : manager.py
|
|
|
|
|
@Software: PyCharm
|
|
|
|
|
"""
|
2024-08-08 18:06:03 +08:00
|
|
|
|
import asyncio
|
2024-07-31 02:28:25 +08:00
|
|
|
|
import threading
|
|
|
|
|
from multiprocessing import Process
|
2024-08-08 18:06:03 +08:00
|
|
|
|
from typing import TYPE_CHECKING
|
2024-07-31 02:28:25 +08:00
|
|
|
|
|
2024-08-10 22:25:41 +08:00
|
|
|
|
from liteyuki.comm import Channel, get_channel, set_channels
|
2024-07-31 02:28:25 +08:00
|
|
|
|
from liteyuki.log import logger
|
|
|
|
|
|
2024-08-08 18:06:03 +08:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from liteyuki.bot import LiteyukiBot
|
|
|
|
|
|
2024-07-31 02:28:25 +08:00
|
|
|
|
TIMEOUT = 10
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
"ProcessManager"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessManager:
|
|
|
|
|
"""
|
|
|
|
|
在主进程中被调用
|
|
|
|
|
"""
|
|
|
|
|
|
2024-08-10 22:25:41 +08:00
|
|
|
|
def __init__(self, bot: "LiteyukiBot"):
|
2024-07-31 02:28:25 +08:00
|
|
|
|
self.bot = bot
|
2024-08-08 18:06:03 +08:00
|
|
|
|
self.targets: dict[str, tuple[callable, tuple, dict]] = {}
|
|
|
|
|
self.processes: dict[str, Process] = {}
|
2024-07-31 02:28:25 +08:00
|
|
|
|
|
2024-08-15 16:40:29 +08:00
|
|
|
|
def start(self, name: str):
|
2024-07-31 02:28:25 +08:00
|
|
|
|
"""
|
2024-08-08 18:06:03 +08:00
|
|
|
|
开启后自动监控进程,并添加到进程字典中
|
2024-07-31 02:28:25 +08:00
|
|
|
|
Args:
|
|
|
|
|
name:
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
2024-08-08 18:06:03 +08:00
|
|
|
|
if name not in self.targets:
|
2024-07-31 02:28:25 +08:00
|
|
|
|
raise KeyError(f"Process {name} not found.")
|
|
|
|
|
|
2024-08-15 16:40:29 +08:00
|
|
|
|
chan_active = get_channel(f"{name}-active")
|
|
|
|
|
|
|
|
|
|
def _start_process():
|
|
|
|
|
process = Process(target=self.targets[name][0], args=self.targets[name][1],
|
|
|
|
|
kwargs=self.targets[name][2])
|
|
|
|
|
self.processes[name] = process
|
|
|
|
|
|
|
|
|
|
process.start()
|
|
|
|
|
|
|
|
|
|
# 启动进程并监听信号
|
|
|
|
|
_start_process()
|
|
|
|
|
|
|
|
|
|
def _start_monitor():
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
2024-08-10 22:25:41 +08:00
|
|
|
|
data = chan_active.receive()
|
2024-08-15 16:40:29 +08:00
|
|
|
|
if data == 0:
|
|
|
|
|
# 停止
|
2024-08-08 18:06:03 +08:00
|
|
|
|
logger.info(f"Stopping process {name}")
|
2024-08-15 16:40:29 +08:00
|
|
|
|
self.bot.lifespan.before_process_shutdown()
|
|
|
|
|
self.terminate(name)
|
|
|
|
|
break
|
|
|
|
|
elif data == 1:
|
|
|
|
|
# 重启
|
|
|
|
|
logger.info(f"Restarting process {name}")
|
|
|
|
|
self.bot.lifespan.before_process_shutdown()
|
|
|
|
|
self.bot.lifespan.before_process_restart()
|
2024-08-08 18:06:03 +08:00
|
|
|
|
self.terminate(name)
|
2024-08-15 16:40:29 +08:00
|
|
|
|
_start_process()
|
|
|
|
|
continue
|
2024-07-31 02:28:25 +08:00
|
|
|
|
else:
|
|
|
|
|
logger.warning("Unknown data received, ignored.")
|
2024-08-15 16:40:29 +08:00
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
logger.info(f"Stopping process {name}")
|
|
|
|
|
self.bot.lifespan.before_process_shutdown()
|
|
|
|
|
self.terminate_all()
|
|
|
|
|
break
|
2024-07-31 02:28:25 +08:00
|
|
|
|
|
2024-08-15 16:40:29 +08:00
|
|
|
|
threading.Thread(target=_start_monitor).start()
|
|
|
|
|
|
|
|
|
|
def start_all(self):
|
|
|
|
|
"""
|
|
|
|
|
启动所有进程
|
|
|
|
|
"""
|
|
|
|
|
for name in self.targets:
|
|
|
|
|
self.start(name)
|
2024-07-31 02:28:25 +08:00
|
|
|
|
|
2024-08-12 02:40:51 +08:00
|
|
|
|
def add_target(self, name: str, target, args: tuple = (), kwargs=None):
|
|
|
|
|
"""
|
|
|
|
|
添加进程
|
|
|
|
|
Args:
|
|
|
|
|
name: 进程名,用于获取和唯一标识
|
|
|
|
|
target: 进程函数
|
|
|
|
|
args: 进程函数参数
|
|
|
|
|
kwargs: 进程函数关键字参数,通常会默认传入chan_active和chan_passive
|
|
|
|
|
"""
|
|
|
|
|
if kwargs is None:
|
|
|
|
|
kwargs = {}
|
|
|
|
|
chan_active = Channel(_id=f"{name}-active")
|
|
|
|
|
chan_passive = Channel(_id=f"{name}-passive")
|
|
|
|
|
kwargs["chan_active"] = chan_active
|
|
|
|
|
kwargs["chan_passive"] = chan_passive
|
2024-08-08 18:06:03 +08:00
|
|
|
|
self.targets[name] = (target, args, kwargs)
|
2024-08-12 02:40:51 +08:00
|
|
|
|
set_channels(
|
|
|
|
|
{
|
|
|
|
|
f"{name}-active" : chan_active,
|
|
|
|
|
f"{name}-passive": chan_passive
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-31 02:28:25 +08:00
|
|
|
|
def join(self):
|
2024-08-08 18:06:03 +08:00
|
|
|
|
for name, process in self.targets:
|
2024-07-31 02:28:25 +08:00
|
|
|
|
process.join()
|
|
|
|
|
|
2024-08-08 18:06:03 +08:00
|
|
|
|
def terminate(self, name: str):
|
|
|
|
|
"""
|
|
|
|
|
终止进程并从进程字典中删除
|
|
|
|
|
Args:
|
|
|
|
|
name:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if name not in self.targets:
|
|
|
|
|
raise logger.warning(f"Process {name} not found.")
|
|
|
|
|
process = self.processes[name]
|
|
|
|
|
process.terminate()
|
|
|
|
|
process.join(TIMEOUT)
|
|
|
|
|
if process.is_alive():
|
|
|
|
|
process.kill()
|
2024-08-15 16:40:29 +08:00
|
|
|
|
logger.success(f"Process {name} terminated.")
|
2024-08-10 22:25:41 +08:00
|
|
|
|
|
|
|
|
|
def terminate_all(self):
|
|
|
|
|
for name in self.targets:
|
|
|
|
|
self.terminate(name)
|
2024-08-12 04:45:59 +08:00
|
|
|
|
|
|
|
|
|
def is_process_alive(self, name: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
检查进程是否存活
|
|
|
|
|
Args:
|
|
|
|
|
name:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if name not in self.targets:
|
|
|
|
|
raise logger.warning(f"Process {name} not found.")
|
2024-08-15 16:40:29 +08:00
|
|
|
|
return self.processes[name].is_alive()
|