File size: 2,299 Bytes
6b1e8b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# devggn

import asyncio
import importlib
import psutil
import gc  # For garbage collection
from pyrogram import idle
from aiojobs import create_scheduler
from devgagan.modules import ALL_MODULES
from devgagan.core.mongo.plans_db import check_and_remove_expired_users

# Subprocess monitoring interval (in seconds)
CHECK_INTERVAL = 1800  # 30 minutes
# Memory optimization interval (in seconds)
MEMORY_OPTIMIZATION_INTERVAL = 600  # 10 minutes


# Function to monitor and terminate idle subprocesses
async def close_idle_subprocesses():
    while True:
        for proc in psutil.process_iter(['pid', 'name', 'create_time']):
            try:
                # Terminate subprocesses spawned by the current process
                if proc.ppid() == psutil.Process().pid:
                    proc.terminate()
                    print(f"Terminated subprocess: PID {proc.pid}, Name: {proc.name()}")
            except (psutil.NoSuchProcess, psutil.AccessDenied):
                pass
        await asyncio.sleep(CHECK_INTERVAL)


# Function to reduce memory usage
async def optimize_memory():
    while True:
        gc.collect()
        process = psutil.Process()
        memory_info = process.memory_info()
        print(f"Memory Usage: {memory_info.rss / 1024 / 1024:.2f} MB")
        await asyncio.sleep(MEMORY_OPTIMIZATION_INTERVAL)


# Function to schedule expiry checks
async def schedule_expiry_check():
    scheduler = await create_scheduler()
    while True:
        await scheduler.spawn(check_and_remove_expired_users())
        await asyncio.sleep(3600)  # Check every hour


# Main bot function
async def devggn_boot():
    # Import modules
    for all_module in ALL_MODULES:
        importlib.import_module("devgagan.modules." + all_module)

    # print("»»»» ʙᴏᴛ ᴅᴇᴘʟᴏʏ sᴜᴄᴄᴇssғᴜʟʟʏ ✨ 🎉")
    print("Bot started!")  # Added print statement here

    # Start background tasks
    asyncio.create_task(schedule_expiry_check())
    asyncio.create_task(close_idle_subprocesses())
    asyncio.create_task(optimize_memory())

    # Keep the bot running
    await idle()
    print("Lol ...")


# Run the bot
if __name__ == "__main__":
    # Reuse the existing event loop
    loop = asyncio.get_event_loop()
    loop.run_until_complete(devggn_boot())