-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhr.py
60 lines (49 loc) · 2.04 KB
/
hr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import subprocess
import time
import logging
from aiogram import Bot
import asyncio
API_TOKEN = '6838193855:AAHi1KDZv6Xgz_9yONP_dpF_TlrHcNQ03EU'
ADMIN_ID = '6512242172'
MAX_RESTARTS = 5
RESTART_PERIOD = 60 # Seconds
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
bot = Bot(API_TOKEN)
def start_bot():
"""Start the bot script as a subprocess."""
return subprocess.Popen(['python', 'm.py'])
async def notify_admin(message):
"""Send a notification message to the admin via Telegram."""
try:
await bot.send_message(ADMIN_ID, message)
logging.info("Admin notified: %s", message)
except Exception as e:
logging.error("Failed to send message to admin: %s", e)
async def main():
"""Main function to manage bot process lifecycle."""
restart_count = 0
last_restart_time = time.time()
while True:
if restart_count >= MAX_RESTARTS:
current_time = time.time()
if current_time - last_restart_time < RESTART_PERIOD:
wait_time = RESTART_PERIOD - (current_time - last_restart_time)
logging.warning("Maximum restart limit reached. Waiting for %.2f seconds...", wait_time)
await notify_admin(f"⚠️ Maximum restart limit reached. Waiting for {int(wait_time)} seconds before retrying.")
await asyncio.sleep(wait_time)
restart_count = 0
last_restart_time = time.time()
logging.info("Starting the bot...")
process = start_bot()
await notify_admin("🚀 ruk re sabar rakh...")
while process.poll() is None:
await asyncio.sleep(5)
logging.warning("Bot process terminated. Restarting in 10 seconds...")
await notify_admin("⚠️ The bot chud gaya will be restarted in 10 seconds.")
restart_count += 1
await asyncio.sleep(10)
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
logging.info("hr script terminated by user.")