From 557000dbece67733d81b4a8061462dcfedf2a1ea Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 12 Mar 2024 10:54:13 +0800 Subject: [PATCH] :bookmark: Update to v1.4.11 add watchdog --- pagermaid/__init__.py | 4 ++-- pagermaid/modules/reload.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index 6553f30..59f5345 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -22,8 +22,8 @@ from pagermaid.scheduler import scheduler import pyromod.listen from pyrogram import Client -pgm_version = "1.4.10" -pgm_version_code = 1410 +pgm_version = "1.4.11" +pgm_version_code = 1411 CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() diff --git a/pagermaid/modules/reload.py b/pagermaid/modules/reload.py index 069d95a..fa5a5f6 100644 --- a/pagermaid/modules/reload.py +++ b/pagermaid/modules/reload.py @@ -1,10 +1,16 @@ -from pagermaid import read_context +import asyncio + +from pyrogram.raw.functions import PingDelayDisconnect + +from pagermaid import read_context, logs from pagermaid.common.reload import reload_all from pagermaid.enums import Message from pagermaid.listener import listener -from pagermaid.services import scheduler +from pagermaid.services import bot, scheduler from pagermaid.utils import lang +ping_watchdog_event = asyncio.Event() + @listener( is_plugin=False, command="reload", need_admin=True, description=lang("reload_des") @@ -18,3 +24,29 @@ async def reload_plugins(message: Message): @scheduler.scheduled_job("cron", hour="4", id="reload.clear_read_context") async def clear_read_context_cron(): read_context.clear() + + +@scheduler.scheduled_job("interval", seconds=10, id="reload.ping_watchdog") +async def ping_task(): + if not bot.is_initialized: + return + if ping_watchdog_event.is_set(): + logs.debug("Ping task watchdog event set, skip") + return + logs.debug("Ping task running") + try: + await bot.session.invoke( + PingDelayDisconnect( + ping_id=0, + disconnect_delay=bot.session.WAIT_TIMEOUT + 10, + ), + retries=0, + ) + except OSError: + logs.debug("Ping task raise OSError, try restart") + ping_watchdog_event.set() + try: + await bot.restart() + finally: + ping_watchdog_event.clear() + logs.debug("Ping task ok")