Improved shutdown

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

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)