Skip to content

Commit

Permalink
I have redesigned several aspects of the bot's functionality in order…
Browse files Browse the repository at this point in the history
… to enhance its speed.
  • Loading branch information
orenlab committed Jun 26, 2024
1 parent 6cc9d49 commit 9010d8f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
6 changes: 3 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
config = BotSettings()

# Set global name
__version__ = 'v0.1.0-dev-20240622'
__version__ = 'v0.1.0-dev-20240626'
__author__ = 'Denis Rozhnovskiy <pytelemonbot@mail.ru>'
__license__ = 'MIT'
__repository__ = 'https://github.com/orenlab/pytmbot'
Expand Down Expand Up @@ -85,7 +85,7 @@ def __get_bot_token(self) -> str:
bot_mode = parse_cli_args()

# Log the bot mode
bot_logger.debug(f"Bot mode: {bot_mode.mode}")
bot_logger.debug(f"Operational bot mode: {bot_mode.mode}")

# Return the appropriate bot token based on the bot mode
return (
Expand Down Expand Up @@ -124,7 +124,7 @@ def build_bot_instance(self) -> telebot.TeleBot:
self.bot.add_custom_filter(ContainersCallbackFilter())

# Log that the bot has been configured successfully
bot_logger.debug("Bot instance configured successfully.")
bot_logger.debug("Basic configuration done. We are now continuing with...")

return self.bot

Expand Down
15 changes: 15 additions & 0 deletions app/core/handlers/default_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@
from .sensors_handler import SensorsHandler
from .start_handler import StartHandler
from .uptime_handlers import UptimeHandler

__all_defaults_handlers__ = [
AboutBotHandler,
BotUpdatesHandler,
ContainersHandler,
EchoHandler,
FileSystemHandler,
LoadAvgHandler,
MemoryHandler,
NetIOHandler,
ProcessHandler,
SensorsHandler,
StartHandler,
UptimeHandler
]
33 changes: 12 additions & 21 deletions app/core/handlers/handlers_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,8 @@
"""
import multiprocessing as mp

from app.core.handlers.default_handlers import (
StartHandler,
LoadAvgHandler,
MemoryHandler,
SensorsHandler,
ProcessHandler,
UptimeHandler,
FileSystemHandler,
ContainersHandler,
BotUpdatesHandler,
NetIOHandler,
AboutBotHandler,
EchoHandler
)
from app.core.handlers.inline_handlers.swap_handler import InlineSwapHandler
from app.core.handlers.inline_handlers.update_info import InlineUpdateInfoHandler
from app.core.handlers.default_handlers import __all_defaults_handlers__
from app.core.handlers.inline_handlers import __all_inline_handlers__
from app.core.logs import bot_logger


Expand All @@ -46,9 +32,8 @@ def __init__(self, bot_instance):

# Initialize all handler instances
self.handlers = [handler(self.bot) for handler in [
StartHandler, LoadAvgHandler, MemoryHandler, SensorsHandler, ProcessHandler,
UptimeHandler, FileSystemHandler, ContainersHandler, BotUpdatesHandler,
InlineSwapHandler, InlineUpdateInfoHandler, NetIOHandler, AboutBotHandler, EchoHandler
*__all_defaults_handlers__,
*__all_inline_handlers__
]]

def run_handlers(self):
Expand All @@ -63,27 +48,33 @@ def run_handlers(self):
"""

# Log the start of the handlers run
bot_logger.debug("Handlers init and run started...")
bot_logger.debug("Starting handlers initialization...")

handlers_count = 0

# Create a multiprocessing pool
with mp.Pool() as pool:
# Apply async to each handler
for handler in self.handlers:
bot_logger.debug(f"Init handler: {handler.__class__.__name__}")
# Apply the handler's handle method in a separate process
# and capture any exceptions
pool.apply_async(
handler.handle(),
error_callback=self._log_error
)

handlers_count += 1

# Close the pool to prevent any more work
pool.close()

# Block until all tasks are done
pool.join()

# Log the successful completion of the handlers run
bot_logger.debug("Handlers init and run successful.")
bot_logger.debug("Handlers instance initialization successful.")
bot_logger.debug(f"Setup bot instances successful with {handlers_count} handlers.")

@staticmethod
def _log_error(e):
Expand Down
9 changes: 9 additions & 0 deletions app/core/handlers/inline_handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .containers_full_info import InlineContainerFullInfoHandler
from .swap_handler import InlineSwapHandler
from .update_info import InlineUpdateInfoHandler

__all_inline_handlers__ = [
InlineContainerFullInfoHandler,
InlineSwapHandler,
InlineUpdateInfoHandler
]
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __start_polling(self) -> None:
self.sleep_duration += 5

# Log the start of a polling session
bot_logger.info('Start polling session')
bot_logger.info('Start polling session............')

try:
# Poll for updates
Expand Down Expand Up @@ -167,7 +167,7 @@ def __setup_middleware(self, middleware) -> None:
bot_logger.debug("Setting up middleware...")

# Log the middleware that is being set up
bot_logger.debug(f"Middleware: {middleware}")
bot_logger.debug(f"Middleware: {middleware.__class__.__name__}")

try:
# Add the middleware to the bot
Expand Down

0 comments on commit 9010d8f

Please sign in to comment.