Improved shutdown

This commit is contained in:
Profitroll 2023-03-20 13:55:42 +01:00
parent 03b6bbe039
commit 20666fc0f8
4 changed files with 134 additions and 25 deletions

View File

@ -116,6 +116,6 @@
"commands_admin": [
"import",
"export",
"reboot"
"shutdown"
]
}

View File

@ -0,0 +1,29 @@
from os import getpid, makedirs, path
from time import time
from modules.app import app
from pyrogram import filters
from pyrogram.types import CallbackQuery
from classes.poster_client import PosterClient
from modules.scheduler import scheduler
from modules.logger import logWrite
from modules.utils import configGet, jsonSave, locale
@app.on_callback_query(filters.regex("shutdown"))
async def callback_query_nothing(app: PosterClient, clb: CallbackQuery):
if clb.from_user.id in app.admins:
pid = getpid()
logWrite(f"Shutting down bot with pid {pid}")
await clb.answer()
await clb.message.reply_text(
locale("shutdown", "message", locale=clb.from_user.language_code).format(
pid
),
)
scheduler.shutdown()
makedirs(configGet("cache", "locations"), exist_ok=True)
jsonSave(
{"timestamp": time()},
path.join(configGet("cache", "locations"), "shutdown_time"),
)
exit()

View File

@ -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()

View File

@ -1,14 +1,18 @@
from os import getpid
from os import getpid, path
from datetime import datetime
from sys import exit
from time import time
from modules.cli import *
from modules.logger import logWrite
from modules.scheduler import scheduler
from modules.utils import configGet, killProc, locale
from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale
# Import ===================================================================================================================================
try:
from pyrogram.sync import idle
from pyrogram.errors import bad_request_400
from dateutil.relativedelta import relativedelta
from modules.app import app
except ModuleNotFoundError:
@ -57,6 +61,7 @@ pid = getpid()
# Imports ==================================================================================================================================
from plugins.commands.general import *
from plugins.callbacks.shutdown import *
if configGet("submit", "mode"):
from plugins.callbacks.nothing import *
@ -112,15 +117,55 @@ if configGet("post", "mode"):
# asyncio.run(main())
if __name__ == "__main__":
logWrite(locale("startup", "console", locale=configGet("locale")).format(str(pid)))
logWrite(locale("startup", "console").format(str(pid)))
app.start()
if configGet("startup", "reports"):
app.send_message(
app.owner,
locale("startup", "message", locale=configGet("locale")).format(str(pid)),
)
try:
if path.exists(path.join(configGet("cache", "locations"), "shutdown_time")):
downtime = relativedelta(
datetime.now(),
datetime.fromtimestamp(
jsonLoad(
path.join(configGet("cache", "locations"), "shutdown_time")
)["timestamp"]
),
)
if downtime.days >= 1:
app.send_message(
configGet("owner"),
locale(
"startup_downtime_days",
"message",
).format(pid, downtime.days),
)
elif downtime.hours >= 1:
app.send_message(
configGet("owner"),
locale(
"startup_downtime_hours",
"message",
).format(pid, downtime.hours),
)
else:
app.send_message(
configGet("owner"),
locale(
"startup_downtime_minutes",
"message",
locale=configGet("locale"),
).format(pid, downtime.minutes),
)
else:
app.send_message(
configGet("owner"),
locale("startup", "message").format(pid),
)
except bad_request_400.PeerIdInvalid:
logWrite(
f"Could not send startup message to bot owner. Perhaps user has not started the bot yet."
)
if configGet("post", "mode"):
scheduler.start()
@ -132,10 +177,24 @@ if __name__ == "__main__":
idle()
app.send_message(
app.owner,
locale("shutdown", "message", locale=configGet("locale")).format(str(pid)),
try:
app.send_message(
configGet("owner"),
locale("shutdown", "message").format(pid),
)
except bad_request_400.PeerIdInvalid:
logWrite(
f"Could not send shutdown message to bot owner. Perhaps user has not started the bot yet."
)
makedirs(configGet("cache", "locations"), exist_ok=True)
jsonSave(
{"timestamp": time()},
path.join(configGet("cache", "locations"), "shutdown_time"),
)
logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid)))
logWrite(locale("shutdown", "console").format(str(pid)))
scheduler.shutdown()
killProc(pid)