def begin(self, coro: Optional[Awaitable[Any]] = None) -> None: """ start userge """ loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGHUP, _shutdown) loop.add_signal_handler(signal.SIGTERM, _shutdown) run = loop.run_until_complete try: run(self.start()) running_tasks: List[asyncio.Task] = [] for task in self._tasks: running_tasks.append(loop.create_task(task())) if coro: _LOG.info(_LOG_STR, "Running Coroutine") run(coro) else: _LOG.info(_LOG_STR, "Idling USERGE-X") logbot.edit_last_msg("USERGE-X has Started Successfully !") logbot.end() idle() _LOG.info(_LOG_STR, "Exiting USERGE-X") for task in running_tasks: task.cancel() run(self.stop()) run(loop.shutdown_asyncgens()) except asyncio.exceptions.CancelledError: pass finally: if not loop.is_running(): loop.close()
def begin(self, coro: Optional[Awaitable[Any]] = None) -> None: """ start userge """ lock = asyncio.Lock() running_tasks: List[asyncio.Task] = [] async def _finalize() -> None: async with lock: for t in running_tasks: t.cancel() if self.is_initialized: await self.stop() else: _close_db() pool._stop() # pylint: disable=protected-access # pylint: disable=expression-not-assigned [ t.cancel() for t in asyncio.all_tasks() if t is not asyncio.current_task() ] await self.loop.shutdown_asyncgens() self.loop.stop() _LOG.info(_LOG_STR, "Loop Stopped !") async def _shutdown(_sig: signal.Signals) -> None: global _SEND_SIGNAL # pylint: disable=global-statement _LOG.info( _LOG_STR, f"Received Stop Signal [{_sig.name}], Exiting Userge ...") await _finalize() if _sig == _sig.SIGUSR1: _SEND_SIGNAL = True for sig in (signal.SIGHUP, signal.SIGTERM, signal.SIGINT, signal.SIGUSR1): self.loop.add_signal_handler( sig, lambda _sig=sig: self.loop.create_task(_shutdown(_sig))) self.loop.run_until_complete(self.start()) for task in self._tasks: running_tasks.append(self.loop.create_task(task())) logbot.edit_last_msg("Userge has Started Successfully !") logbot.end() mode = "[DUAL]" if RawClient.DUAL_MODE else "[BOT]" if Config.BOT_TOKEN else "[USER]" try: if coro: _LOG.info(_LOG_STR, f"Running Coroutine - {mode}") self.loop.run_until_complete(coro) else: _LOG.info(_LOG_STR, f"Idling Userge - {mode}") idle() self.loop.run_until_complete(_finalize()) except (asyncio.exceptions.CancelledError, RuntimeError): pass finally: self.loop.close() _LOG.info(_LOG_STR, "Loop Closed !") if _SEND_SIGNAL: os.kill(os.getpid(), signal.SIGUSR1)
async def _load_plugins(self) -> None: _IMPORTED.clear() _INIT_TASKS.clear() logbot.edit_last_msg("Importing All Plugins", _LOG.info, _LOG_STR) for name in get_all_plugins(): try: await self.load_plugin(name) except ImportError as i_e: _LOG.error(_LOG_STR, f"[{name}] - {i_e}") await self.finalize_load() _LOG.info(_LOG_STR, f"Imported ({len(_IMPORTED)}) Plugins => " + str([i.__name__ for i in _IMPORTED]))
async def _load_plugins(self) -> None: _IMPORTED.clear() _INIT_TASKS.clear() logbot.edit_last_msg("Tüm Eklentiler İçe Aktarıldı", _LOG.info, _LOG_STR) for name in get_all_plugins(): try: await self.load_plugin(name) except ImportError as i_e: _LOG.error(_LOG_STR, f"[{name}] - {i_e}") await self.finalize_load() _LOG.info( _LOG_STR, f"({len(_IMPORTED)}) => Eklentisi içe aktarıldı " + str([i.__name__ for i in _IMPORTED]))
def begin(self, coro: Optional[Awaitable[Any]] = None) -> None: """ start USERGE-X """ lock = asyncio.Lock() running_tasks: List[asyncio.Task] = [] async def _finalize() -> None: async with lock: for task in running_tasks: task.cancel() if self.is_initialized: await self.stop() # pylint: disable=expression-not-assigned [ t.cancel() for t in asyncio.all_tasks() if t is not asyncio.current_task() ] await self.loop.shutdown_asyncgens() self.loop.stop() _LOG.info(_LOG_STR, "Loop Stopped !") async def _shutdown(sig: signal.Signals) -> None: _LOG.info( _LOG_STR, f"Received Stop Signal [{sig.name}], Exiting USERGE-X ...") await _finalize() for sig in (signal.SIGHUP, signal.SIGTERM, signal.SIGINT): self.loop.add_signal_handler( sig, lambda sig=sig: self.loop.create_task(_shutdown(sig))) self.loop.run_until_complete(self.start()) for task in self._tasks: running_tasks.append(self.loop.create_task(task())) logbot.edit_last_msg("USERGE-X has Started Successfully !") logbot.end() try: if coro: _LOG.info(_LOG_STR, "Running Coroutine") self.loop.run_until_complete(coro) else: _LOG.info(_LOG_STR, "Idling USERGE-X") idle() self.loop.run_until_complete(_finalize()) except (asyncio.exceptions.CancelledError, RuntimeError): pass finally: self.loop.close() _LOG.info(_LOG_STR, "Loop Closed !")
def begin(self, coro: Optional[Awaitable[Any]] = None) -> None: """ start userge """ loop = asyncio.get_event_loop() run = loop.run_until_complete run(self.start()) running_tasks: List[asyncio.Task] = [] for task in self._tasks: running_tasks.append(loop.create_task(task())) if coro: _LOG.info(_LOG_STR, "Running Coroutine") run(coro) else: _LOG.info(_LOG_STR, "Idling Userge") logbot.edit_last_msg("Userge has Started Successfully !") logbot.end() idle() _LOG.info(_LOG_STR, "Exiting Userge") for task in running_tasks: task.cancel() run(self.stop()) run(loop.shutdown_asyncgens()) loop.close()
# All rights reserved. __all__ = ['get_collection'] import asyncio from typing import List from motor.motor_asyncio import AsyncIOMotorClient from motor.core import AgnosticClient, AgnosticDatabase, AgnosticCollection from userge import logging, Config, logbot _LOG = logging.getLogger(__name__) _LOG_STR = "$$$>>> %s <<<$$$" logbot.edit_last_msg("Connecting to Database ...", _LOG.info, _LOG_STR) _MGCLIENT: AgnosticClient = AsyncIOMotorClient(Config.DB_URI) _RUN = asyncio.get_event_loop().run_until_complete if "Userge" in _RUN(_MGCLIENT.list_database_names()): _LOG.info(_LOG_STR, "Userge Database Found :) => Now Logging to it...") else: _LOG.info(_LOG_STR, "Userge Database Not Found :( => Creating New Database...") _DATABASE: AgnosticDatabase = _MGCLIENT["Userge"] _COL_LIST: List[str] = _RUN(_DATABASE.list_collection_names()) def get_collection(name: str) -> AgnosticCollection: """ Create or Get Collection from your database """