Path: blob/master/bot/modules/restart.py
1625 views
from sys import executable1from aiofiles import open as aiopen2from aiofiles.os import path as aiopath, remove3from asyncio import gather, create_subprocess_exec4from os import execl as osexecl56from .. import intervals, scheduler, sabnzbd_client, LOGGER7from ..helper.ext_utils.bot_utils import new_task8from ..helper.telegram_helper.message_utils import (9send_message,10delete_message,11)12from ..helper.ext_utils.db_handler import database13from ..helper.ext_utils.files_utils import clean_all14from ..helper.telegram_helper.button_build import ButtonMaker15from ..core.telegram_manager import TgClient16from ..core.config_manager import Config17from ..core.jdownloader_booter import jdownloader18from ..core.torrent_manager import TorrentManager192021@new_task22async def restart_bot(_, message):23buttons = ButtonMaker()24buttons.data_button("Yes!", "botrestart confirm")25buttons.data_button("Cancel", "botrestart cancel")26button = buttons.build_menu(2)27await send_message(message, "Are you sure you want to restart the bot ?!", button)282930async def send_incomplete_task_message(cid, msg_id, msg):31try:32if msg.startswith("Restarted Successfully!"):33await TgClient.bot.edit_message_text(34chat_id=cid,35message_id=msg_id,36text=msg,37)38await remove(".restartmsg")39else:40await TgClient.bot.send_message(41chat_id=cid,42text=msg,43disable_notification=True,44)45except Exception as e:46LOGGER.error(e)474849async def restart_notification():50if await aiopath.isfile(".restartmsg"):51with open(".restartmsg") as f:52chat_id, msg_id = map(int, f)53else:54chat_id, msg_id = 0, 05556if Config.INCOMPLETE_TASK_NOTIFIER and Config.DATABASE_URL:57if notifier_dict := await database.get_incomplete_tasks():58for cid, data in notifier_dict.items():59msg = "Restarted Successfully!" if cid == chat_id else "Bot Restarted!"60for tag, links in data.items():61msg += f"\n\n{tag}: "62for index, link in enumerate(links, start=1):63msg += f" <a href='{link}'>{index}</a> |"64if len(msg.encode()) > 4000:65await send_incomplete_task_message(cid, msg_id, msg)66msg = ""67if msg:68await send_incomplete_task_message(cid, msg_id, msg)6970if await aiopath.isfile(".restartmsg"):71try:72await TgClient.bot.edit_message_text(73chat_id=chat_id, message_id=msg_id, text="Restarted Successfully!"74)75except:76pass77await remove(".restartmsg")787980@new_task81async def confirm_restart(_, query):82await query.answer()83data = query.data.split()84message = query.message85if data[1] == "confirm":86reply_to = message.reply_to_message87intervals["stopAll"] = True88restart_message = await send_message(reply_to, "Restarting...")89await delete_message(message)90await TgClient.stop()91if scheduler.running:92scheduler.shutdown(wait=False)93if qb := intervals["qb"]:94qb.cancel()95if jd := intervals["jd"]:96jd.cancel()97if nzb := intervals["nzb"]:98nzb.cancel()99if st := intervals["status"]:100for intvl in list(st.values()):101intvl.cancel()102await clean_all()103await TorrentManager.close_all()104if sabnzbd_client.LOGGED_IN:105await gather(106sabnzbd_client.pause_all(),107sabnzbd_client.delete_job("all", True),108sabnzbd_client.purge_all(True),109sabnzbd_client.delete_history("all", delete_files=True),110)111await sabnzbd_client.close()112if jdownloader.is_connected:113await gather(114jdownloader.device.downloadcontroller.stop_downloads(),115jdownloader.device.linkgrabber.clear_list(),116jdownloader.device.downloads.cleanup(117"DELETE_ALL",118"REMOVE_LINKS_AND_DELETE_FILES",119"ALL",120),121)122await jdownloader.close()123proc1 = await create_subprocess_exec(124"pkill",125"-9",126"-f",127"gunicorn|aria2c|qbittorrent-nox|ffmpeg|rclone|java|sabnzbdplus|7z|split",128)129proc2 = await create_subprocess_exec("python3", "update.py")130await gather(proc1.wait(), proc2.wait())131async with aiopen(".restartmsg", "w") as f:132await f.write(f"{restart_message.chat.id}\n{restart_message.id}\n")133osexecl(executable, executable, "-m", "bot")134else:135await delete_message(message)136137138