-
Notifications
You must be signed in to change notification settings - Fork 272
Introduce ListenerStartHandler in the listener runner for better managing Django DB connections #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Introduce ListenerStartHandler in the listener runner for better managing Django DB connections #514
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import inspect | ||
| from abc import ABCMeta, abstractmethod | ||
| from logging import Logger | ||
| from typing import Callable, Dict, Any, Awaitable, Optional | ||
|
|
||
| from slack_bolt.kwargs_injection.async_utils import build_async_required_kwargs | ||
| from slack_bolt.request.async_request import AsyncBoltRequest | ||
| from slack_bolt.response import BoltResponse | ||
|
|
||
|
|
||
| class AsyncListenerStartHandler(metaclass=ABCMeta): | ||
| @abstractmethod | ||
| async def handle( | ||
| self, | ||
| request: AsyncBoltRequest, | ||
| response: Optional[BoltResponse], | ||
| ) -> None: | ||
| """Do something extra before the listener execution | ||
|
|
||
| Args: | ||
| request: The request. | ||
| response: The response. | ||
| """ | ||
| raise NotImplementedError() | ||
|
|
||
|
|
||
| class AsyncCustomListenerStartHandler(AsyncListenerStartHandler): | ||
| def __init__(self, logger: Logger, func: Callable[..., Awaitable[None]]): | ||
| self.func = func | ||
| self.logger = logger | ||
| self.arg_names = inspect.getfullargspec(func).args | ||
|
|
||
| async def handle( | ||
| self, | ||
| request: AsyncBoltRequest, | ||
| response: Optional[BoltResponse], | ||
| ) -> None: | ||
| kwargs: Dict[str, Any] = build_async_required_kwargs( | ||
| required_arg_names=self.arg_names, | ||
| logger=self.logger, | ||
| request=request, | ||
| response=response, | ||
| next_keys_required=False, | ||
| ) | ||
| await self.func(**kwargs) | ||
|
|
||
|
|
||
| class AsyncDefaultListenerStartHandler(AsyncListenerStartHandler): | ||
| def __init__(self, logger: Logger): | ||
| self.logger = logger | ||
|
|
||
| async def handle( | ||
| self, | ||
| request: AsyncBoltRequest, | ||
| response: Optional[BoltResponse], | ||
| ): | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ def handle( | |
| request: BoltRequest, | ||
| response: Optional[BoltResponse], | ||
| ) -> None: | ||
| """Handles an unhandled exception. | ||
| """Do something extra after the listener execution | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This had been wrong 🤦 |
||
|
|
||
| Args: | ||
| request: The request. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import inspect | ||
| from abc import ABCMeta, abstractmethod | ||
| from logging import Logger | ||
| from typing import Callable, Dict, Any, Optional | ||
|
|
||
| from slack_bolt.kwargs_injection import build_required_kwargs | ||
| from slack_bolt.request.request import BoltRequest | ||
| from slack_bolt.response.response import BoltResponse | ||
|
|
||
|
|
||
| class ListenerStartHandler(metaclass=ABCMeta): | ||
| @abstractmethod | ||
| def handle( | ||
| self, | ||
| request: BoltRequest, | ||
| response: Optional[BoltResponse], | ||
| ) -> None: | ||
| """Do something extra before the listener execution. | ||
|
|
||
| This handler is useful if a developer needs to maintain/clean up | ||
| thread-local resources such as Django ORM database connections | ||
| before a listener execution starts. | ||
|
|
||
| Args: | ||
| request: The request. | ||
| response: The response. | ||
| """ | ||
| raise NotImplementedError() | ||
|
|
||
|
|
||
| class CustomListenerStartHandler(ListenerStartHandler): | ||
| def __init__(self, logger: Logger, func: Callable[..., None]): | ||
| self.func = func | ||
| self.logger = logger | ||
| self.arg_names = inspect.getfullargspec(func).args | ||
|
|
||
| def handle( | ||
| self, | ||
| request: BoltRequest, | ||
| response: Optional[BoltResponse], | ||
| ): | ||
| kwargs: Dict[str, Any] = build_required_kwargs( | ||
| required_arg_names=self.arg_names, | ||
| logger=self.logger, | ||
| request=request, | ||
| response=response, | ||
| next_keys_required=False, | ||
| ) | ||
| self.func(**kwargs) | ||
|
|
||
|
|
||
| class DefaultListenerStartHandler(ListenerStartHandler): | ||
| def __init__(self, logger: Logger): | ||
| self.logger = logger | ||
|
|
||
| def handle( | ||
| self, | ||
| request: BoltRequest, | ||
| response: Optional[BoltResponse], | ||
| ): | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| """Check the latest version at https://pypi.org/project/slack-bolt/""" | ||
| __version__ = "1.9.4" | ||
| __version__ = "1.10.0a" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll bump the minor version as this pull request introduces new functionalities |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This had been wrong 🤦