Improved shutdown
This commit is contained in:
@@ -1,24 +1,45 @@
|
||||
from os import getpid
|
||||
from os import getpid, makedirs, path
|
||||
from time import time
|
||||
|
||||
from pyrogram import filters
|
||||
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
|
||||
|
||||
from classes.poster_client import PosterClient
|
||||
from pyrogram.types import Message
|
||||
|
||||
from modules.app import app
|
||||
from modules.app import app, users_with_context
|
||||
from modules.logger import logWrite
|
||||
from modules.utils import configGet, killProc, locale
|
||||
from modules.scheduler import scheduler
|
||||
from modules.utils import configGet, jsonSave, locale
|
||||
|
||||
|
||||
@app.on_message(
|
||||
~filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"])
|
||||
)
|
||||
@app.on_message(~filters.scheduled & filters.command(["shutdown"], prefixes=["", "/"]))
|
||||
async def cmd_kill(app: PosterClient, msg: Message):
|
||||
if msg.from_user.id in app.admins:
|
||||
global users_with_context
|
||||
if len(users_with_context) > 0:
|
||||
await msg.reply_text(
|
||||
f"There're {len(users_with_context)} unfinished users' contexts. If you turn off the bot, those will be lost. Please confirm shutdown using a button below.",
|
||||
reply_markup=InlineKeyboardMarkup(
|
||||
[
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
"Confirm shutdown", callback_data="shutdown"
|
||||
)
|
||||
]
|
||||
]
|
||||
),
|
||||
)
|
||||
return
|
||||
pid = getpid()
|
||||
logWrite(
|
||||
locale("shutdown", "console", locale=configGet("locale")).format(str(pid))
|
||||
)
|
||||
logWrite(f"Shutting down bot with pid {pid}")
|
||||
await msg.reply_text(
|
||||
locale("shutdown", "message", locale=configGet("locale")).format(str(pid))
|
||||
locale("shutdown", "message", locale=msg.from_user.language_code).format(
|
||||
pid
|
||||
),
|
||||
)
|
||||
killProc(pid)
|
||||
scheduler.shutdown()
|
||||
makedirs(configGet("cache", "locations"), exist_ok=True)
|
||||
jsonSave(
|
||||
{"timestamp": time()},
|
||||
path.join(configGet("cache", "locations"), "shutdown_time"),
|
||||
)
|
||||
exit()
|
||||
|
Reference in New Issue
Block a user