-
Notifications
You must be signed in to change notification settings - Fork 16.6k
chore(reports): add task for slack channels warm-up #32585
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1015,6 +1015,7 @@ class CeleryConfig: # pylint: disable=too-few-public-methods | |
| "superset.tasks.scheduler", | ||
| "superset.tasks.thumbnails", | ||
| "superset.tasks.cache", | ||
| "superset.tasks.slack", | ||
| ) | ||
| result_backend = "db+sqlite:///celery_results.sqlite" | ||
| worker_prefetch_multiplier = 1 | ||
|
|
@@ -1046,6 +1047,11 @@ class CeleryConfig: # pylint: disable=too-few-public-methods | |
| # "schedule": crontab(minute="*", hour="*"), | ||
| # "kwargs": {"retention_period_days": 180}, | ||
| # }, | ||
| # Uncomment to enable Slack channel cache warm-up | ||
| # "slack.cache_channels": { | ||
| # "task": "slack.cache_channels", | ||
| # "schedule": crontab(minute="0", hour="*"), | ||
| # }, | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -1488,6 +1494,7 @@ def EMAIL_HEADER_MUTATOR( # pylint: disable=invalid-name,unused-argument # noq | |
| # Slack API token for the superset reports, either string or callable | ||
| SLACK_API_TOKEN: Callable[[], str] | str | None = None | ||
| SLACK_PROXY = None | ||
| SLACK_CACHE_TIMEOUT = int(timedelta(days=1).total_seconds()) | ||
|
Contributor
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. thanks! |
||
|
|
||
| # The webdriver to use for generating reports. Use one of the following | ||
| # firefox | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import logging | ||
|
|
||
| from flask import current_app | ||
|
|
||
| from superset.extensions import celery_app | ||
| from superset.utils.slack import get_channels | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @celery_app.task(name="slack.cache_channels") | ||
| def cache_channels() -> None: | ||
| try: | ||
| get_channels( | ||
| force=True, cache_timeout=current_app.config["SLACK_CACHE_TIMEOUT"] | ||
|
Member
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. @dpgaspar can you confirm if this task would work properly for fetching the slack token (when it is a callable) for multi-tenant applications? Do we need to pass in some user context to this function?
Member
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. it should just work |
||
| ) | ||
| except Exception as ex: | ||
| logger.exception("An error occurred while caching Slack channels: %s", ex) | ||
| raise | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
|
|
||
| import logging | ||
| from typing import Any, Optional | ||
| from typing import Callable, Optional | ||
|
|
||
| from flask import current_app | ||
| from slack_sdk import WebClient | ||
|
|
@@ -60,7 +60,7 @@ def get_slack_client() -> WebClient: | |
| key="slack_conversations_list", | ||
| cache=cache_manager.cache, | ||
| ) | ||
| def get_channels(limit: int, extra_params: dict[str, Any]) -> list[SlackChannelSchema]: | ||
| def get_channels() -> list[SlackChannelSchema]: | ||
| """ | ||
| Retrieves a list of all conversations accessible by the bot | ||
| from the Slack API, and caches results (to avoid rate limits). | ||
|
|
@@ -71,11 +71,12 @@ def get_channels(limit: int, extra_params: dict[str, Any]) -> list[SlackChannelS | |
| client = get_slack_client() | ||
| channel_schema = SlackChannelSchema() | ||
| channels: list[SlackChannelSchema] = [] | ||
| extra_params = {"types": ",".join(SlackChannelTypes)} | ||
| cursor = None | ||
|
|
||
| while True: | ||
| response = client.conversations_list( | ||
| limit=limit, cursor=cursor, exclude_archived=True, **extra_params | ||
| limit=999, cursor=cursor, exclude_archived=True, **extra_params | ||
|
Member
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. why are we hardcoding this now?
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. I inlined the Going for the maximum (999) allowed by Slack is most efficient, imo. Lower values are more likely to result in rate-limiting (which is based on requests, not rows returned or similar).
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. If we see a need we could add paging to our |
||
| ) | ||
| channels.extend( | ||
| channel_schema.load(channel) for channel in response.data["channels"] | ||
|
|
@@ -89,7 +90,6 @@ def get_channels(limit: int, extra_params: dict[str, Any]) -> list[SlackChannelS | |
|
|
||
| def get_channels_with_search( | ||
| search_string: str = "", | ||
| limit: int = 999, | ||
| types: Optional[list[SlackChannelTypes]] = None, | ||
| exact_match: bool = False, | ||
| force: bool = False, | ||
|
|
@@ -99,18 +99,25 @@ def get_channels_with_search( | |
| all channels and filter them ourselves | ||
| This will search by slack name or id | ||
| """ | ||
| extra_params = {} | ||
| extra_params["types"] = ",".join(types) if types else None | ||
| try: | ||
| channels = get_channels( | ||
| limit=limit, | ||
| extra_params=extra_params, | ||
| force=force, | ||
| cache_timeout=86400, | ||
| cache_timeout=current_app.config["SLACK_CACHE_TIMEOUT"], | ||
| ) | ||
| except (SlackClientError, SlackApiError) as ex: | ||
| raise SupersetException(f"Failed to list channels: {ex}") from ex | ||
|
|
||
| if types and not len(types) == len(SlackChannelTypes): | ||
| conditions: list[Callable[[SlackChannelSchema], bool]] = [] | ||
|
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. The intention here was to make this more easily pluggable in the future if we add more types.
Contributor
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. Nice! |
||
| if SlackChannelTypes.PUBLIC in types: | ||
| conditions.append(lambda channel: not channel["is_private"]) | ||
| if SlackChannelTypes.PRIVATE in types: | ||
| conditions.append(lambda channel: channel["is_private"]) | ||
|
|
||
| channels = [ | ||
| channel for channel in channels if any(cond(channel) for cond in conditions) | ||
| ] | ||
|
|
||
| # The search string can be multiple channels separated by commas | ||
| if search_string: | ||
| search_array = recipients_string_to_list(search_string) | ||
|
|
||
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.
Nice!