2024-03-12 02:54:13 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from pyrogram.raw.functions import PingDelayDisconnect
|
|
|
|
|
|
|
|
from pagermaid import read_context, logs
|
2023-01-31 16:24:56 +00:00
|
|
|
from pagermaid.common.reload import reload_all
|
2023-01-14 05:49:08 +00:00
|
|
|
from pagermaid.enums import Message
|
2022-06-20 13:55:14 +00:00
|
|
|
from pagermaid.listener import listener
|
2024-03-12 02:54:13 +00:00
|
|
|
from pagermaid.services import bot, scheduler
|
2023-01-14 05:49:08 +00:00
|
|
|
from pagermaid.utils import lang
|
2022-06-20 13:55:14 +00:00
|
|
|
|
2024-03-12 02:54:13 +00:00
|
|
|
ping_watchdog_event = asyncio.Event()
|
|
|
|
|
2022-06-20 13:55:14 +00:00
|
|
|
|
2023-03-12 03:56:01 +00:00
|
|
|
@listener(
|
|
|
|
is_plugin=False, command="reload", need_admin=True, description=lang("reload_des")
|
|
|
|
)
|
2022-06-20 13:55:14 +00:00
|
|
|
async def reload_plugins(message: Message):
|
2023-03-12 03:56:01 +00:00
|
|
|
"""To reload plugins."""
|
2022-11-14 14:11:27 +00:00
|
|
|
await reload_all()
|
2022-06-20 13:55:14 +00:00
|
|
|
await message.edit(lang("reload_ok"))
|
2023-01-14 05:49:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@scheduler.scheduled_job("cron", hour="4", id="reload.clear_read_context")
|
|
|
|
async def clear_read_context_cron():
|
|
|
|
read_context.clear()
|
2024-03-12 02:54:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@scheduler.scheduled_job("interval", seconds=10, id="reload.ping_watchdog")
|
|
|
|
async def ping_task():
|
2024-03-18 12:48:24 +00:00
|
|
|
if not bot.session.is_started.is_set():
|
|
|
|
logs.debug("Ping task bot not started, skip")
|
2024-03-12 02:54:13 +00:00
|
|
|
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:
|
2024-03-18 12:48:24 +00:00
|
|
|
await bot.session.restart()
|
2024-03-12 02:54:13 +00:00
|
|
|
finally:
|
|
|
|
ping_watchdog_event.clear()
|
|
|
|
logs.debug("Ping task ok")
|