privateone commited on
Commit
e3d3fc0
·
1 Parent(s): 32585f3

Code Updates & Optimisations, Inactivity Recreation

Browse files
FileStream/__main__.py CHANGED
@@ -6,13 +6,13 @@ from aiohttp import web
6
  from pyrogram import idle
7
  import logging.handlers as handlers
8
 
9
- #------------------Local Imports----------------------#
10
 
11
  from FileStream.bot import FileStream
12
- from FileStream.server import web_server
13
  from FileStream.Tools import Time_ISTKolNow
14
  from FileStream.config import Telegram, Server
15
  from FileStream.bot.clients import initialize_clients
 
16
 
17
  # Configure logging
18
  logging.basicConfig(
@@ -29,8 +29,6 @@ logging.getLogger("aiohttp").setLevel(logging.ERROR)
29
  logging.getLogger("pyrogram").setLevel(logging.ERROR)
30
  logging.getLogger("aiohttp.web").setLevel(logging.ERROR)
31
 
32
- server = web.AppRunner(web_server())
33
-
34
  async def start_services():
35
  print(
36
  "****\n"
@@ -47,17 +45,21 @@ async def start_services():
47
  FileStream.id = bot_info.id
48
  FileStream.username = bot_info.username
49
  FileStream.fname = bot_info.first_name
50
- print("------------------------------ DONE ------------------------------\n\n")
51
 
52
  print("---------------------- Initializing Clients ----------------------")
53
  await initialize_clients()
54
  print("------------------------------ DONE ------------------------------\n\n")
55
 
56
  print("--------------------- Initializing Web Server ---------------------")
57
- await server.setup()
 
58
  await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start()
59
  print("------------------------------ DONE ------------------------------\n\n")
60
 
 
 
 
61
  print(
62
  f"------------------------- Service Started -------------------------\n"
63
  f"Bot =>> {bot_info.first_name}\n" +
@@ -66,13 +68,7 @@ async def start_services():
66
  "------------------------------------------------------------------"
67
  )
68
 
69
- # Uncomment the following section to send messages to specific sources
70
- """
71
- all_sources = [Telegram.ULOG_GROUP, Telegram.FLOG_CHANNEL, Telegram.PFLOG_CHANNEL]
72
- for source in all_sources:
73
- await FileStream.send_message(chat_id=source, text=f"Hi, I am Online @{Time_ISTKolNow()}", disable_web_page_preview=True)
74
- """
75
- await idle()
76
 
77
  async def cleanup():
78
  await server.cleanup()
@@ -81,7 +77,8 @@ async def cleanup():
81
 
82
  if __name__ == "__main__":
83
  try:
84
- loop.run_until_complete(start_services())
 
85
  except KeyboardInterrupt:
86
  print("\n------------------ Stopping Services KeyBoard Interrupt -----------------")
87
  except Exception as err:
 
6
  from pyrogram import idle
7
  import logging.handlers as handlers
8
 
9
+ # ------------------Local Imports----------------------#
10
 
11
  from FileStream.bot import FileStream
 
12
  from FileStream.Tools import Time_ISTKolNow
13
  from FileStream.config import Telegram, Server
14
  from FileStream.bot.clients import initialize_clients
15
+ from FileStream.server import web_server, clear_inactive_clients
16
 
17
  # Configure logging
18
  logging.basicConfig(
 
29
  logging.getLogger("pyrogram").setLevel(logging.ERROR)
30
  logging.getLogger("aiohttp.web").setLevel(logging.ERROR)
31
 
 
 
32
  async def start_services():
33
  print(
34
  "****\n"
 
45
  FileStream.id = bot_info.id
46
  FileStream.username = bot_info.username
47
  FileStream.fname = bot_info.first_name
48
+ print("------------------------------ DONE ------------------------------\n")
49
 
50
  print("---------------------- Initializing Clients ----------------------")
51
  await initialize_clients()
52
  print("------------------------------ DONE ------------------------------\n\n")
53
 
54
  print("--------------------- Initializing Web Server ---------------------")
55
+ server = web.AppRunner(web_server()) # Initialize the app runner
56
+ await server.setup() # Setup the web server
57
  await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start()
58
  print("------------------------------ DONE ------------------------------\n\n")
59
 
60
+ # Start the background task to clear inactive clients after web server setup
61
+ asyncio.create_task(clear_inactive_clients()) # Now this works as event loop is running
62
+
63
  print(
64
  f"------------------------- Service Started -------------------------\n"
65
  f"Bot =>> {bot_info.first_name}\n" +
 
68
  "------------------------------------------------------------------"
69
  )
70
 
71
+ await idle() # Keep the bot alive
 
 
 
 
 
 
72
 
73
  async def cleanup():
74
  await server.cleanup()
 
77
 
78
  if __name__ == "__main__":
79
  try:
80
+ loop = asyncio.get_event_loop() # Ensure the event loop is retrieved correctly
81
+ loop.run_until_complete(start_services()) # Run the services asynchronously
82
  except KeyboardInterrupt:
83
  print("\n------------------ Stopping Services KeyBoard Interrupt -----------------")
84
  except Exception as err:
FileStream/server/__init__.py CHANGED
@@ -17,7 +17,7 @@ async def clear_inactive_clients():
17
  Periodically checks and clears inactive clients from ACTIVE_CLIENTS.
18
  """
19
  while True:
20
- await asyncio.sleep(INACTIVITY_TIMEOUT) # Check every 5 minutes
21
 
22
  # Find clients that are inactive and clear them
23
  now = asyncio.get_event_loop().time()
@@ -44,6 +44,5 @@ def web_server():
44
  web_app.add_routes(routes)
45
  web_app.add_subapp('/app', sub_app)
46
  web_app.add_subapp('/api', api)
47
- # Start the background task to clear inactive clients
48
- asyncio.create_task(clear_inactive_clients())
49
  return web_app
 
17
  Periodically checks and clears inactive clients from ACTIVE_CLIENTS.
18
  """
19
  while True:
20
+ await asyncio.sleep(INACTIVITY_TIMEOUT) # Check every 10 minutes
21
 
22
  # Find clients that are inactive and clear them
23
  now = asyncio.get_event_loop().time()
 
44
  web_app.add_routes(routes)
45
  web_app.add_subapp('/app', sub_app)
46
  web_app.add_subapp('/api', api)
47
+ # Return the app to be used with AppRunner
 
48
  return web_app