Resolve #1155: Different signature of startup/shutdown events on polling and webhooks (#1156)

* Code refactor
- Use 'or' istead of 'A if A else B'
- Raise new error from catched error: raise Error from e

* Fixed signature of startup/shutdown events
- Include the **dispatcher.workflow_data as the handler arguments
This commit is contained in:
Łukasz Tshipenchko 2023-04-08 20:38:11 +06:00 committed by GitHub
parent 5a99b78292
commit 6efef7137c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

1
CHANGES/1155.bugfix.rst Normal file
View file

@ -0,0 +1 @@
Fixed signature of startup/shutdown events to include the **dispatcher.workflow_data as the handler arguments.

View file

@ -80,9 +80,9 @@ class Dispatcher(Router):
# FSM middleware should always be registered after User context middleware # FSM middleware should always be registered after User context middleware
# because here is used context from previous step # because here is used context from previous step
self.fsm = FSMContextMiddleware( self.fsm = FSMContextMiddleware(
storage=storage if storage else MemoryStorage(), storage=storage or MemoryStorage(),
strategy=fsm_strategy, strategy=fsm_strategy,
events_isolation=events_isolation if events_isolation else DisabledEventIsolation(), events_isolation=events_isolation or DisabledEventIsolation(),
) )
if not disable_fsm: if not disable_fsm:
# Note that when FSM middleware is disabled, the event isolation is also disabled # Note that when FSM middleware is disabled, the event isolation is also disabled
@ -251,14 +251,14 @@ class Dispatcher(Router):
try: try:
update_type = update.event_type update_type = update.event_type
event = update.event event = update.event
except UpdateTypeLookupError: except UpdateTypeLookupError as e:
warnings.warn( warnings.warn(
"Detected unknown update type.\n" "Detected unknown update type.\n"
"Seems like Telegram Bot API was updated and you have " "Seems like Telegram Bot API was updated and you have "
"installed not latest version of aiogram framework", "installed not latest version of aiogram framework",
RuntimeWarning, RuntimeWarning,
) )
raise SkipHandler() raise SkipHandler() from e
kwargs.update(event_update=update) kwargs.update(event_update=update)
@ -373,7 +373,7 @@ class Dispatcher(Router):
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
waiter = loop.create_future() waiter = loop.create_future()
def release_waiter(*args: Any) -> None: def release_waiter(*_: Any) -> None:
if not waiter.done(): if not waiter.done():
waiter.set_result(None) waiter.set_result(None)
@ -488,8 +488,13 @@ class Dispatcher(Router):
signal.SIGINT, self._signal_stop_polling, signal.SIGINT signal.SIGINT, self._signal_stop_polling, signal.SIGINT
) )
workflow_data = {"dispatcher": self, "bots": bots, "bot": bots[-1]} workflow_data = {
workflow_data.update(kwargs) "dispatcher": self,
"bots": bots,
"bot": bots[-1],
**kwargs,
**self.workflow_data,
}
await self.emit_startup(**workflow_data) await self.emit_startup(**workflow_data)
loggers.dispatcher.info("Start polling") loggers.dispatcher.info("Start polling")
try: try: