-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(async): Add daily task to archive stale Slack channels #206
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
base: main
Are you sure you want to change the base?
Changes from all commits
2469a16
c57a49f
331af6c
4bc34ba
eb6b6d6
04dfff0
8d232a9
2ecd031
f6272da
2fae4e2
b95e846
91f6c2c
3968031
e5db3cd
877ad2b
033c52d
f36375b
6f30d54
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from django.db import migrations | ||
|
|
||
| from firetower.incidents.tasks import SCHEDULES | ||
|
|
||
|
|
||
| def create_schedule(apps, schema_editor): | ||
| Schedule = apps.get_model("django_q", "Schedule") | ||
| schedule_name = "archive_stale_channels" | ||
| Schedule.objects.get_or_create( | ||
| name=schedule_name, defaults=SCHEDULES[schedule_name] | ||
| ) | ||
|
|
||
|
|
||
| def delete_schedule(apps, schema_editor): | ||
| Schedule = apps.get_model("django_q", "Schedule") | ||
| schedule_name = "archive_stale_channels" | ||
| Schedule.objects.filter(name=schedule_name).delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("incidents", "0018_add_action_item_model"), | ||
| ("django_q", "0018_task_success_index"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(create_schedule, delete_schedule), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("incidents", "0019_schedule_archive_stale_channels"), | ||
| ("incidents", "0022_actionitem_last_nag"), | ||
| ] | ||
|
|
||
| operations = [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import logging | ||
| import time | ||
|
|
||
| from django_q.tasks import Schedule | ||
|
|
||
| from firetower.incidents.models import ( | ||
| ExternalLink, | ||
| ExternalLinkType, | ||
| IncidentStatus, | ||
| ) | ||
| from firetower.incidents.tasks.decorators import datadog_log | ||
| from firetower.integrations.services.slack import SlackService | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| ARCHIVE_NOTICE = ( | ||
| "This channel is being archived by Firetower because all message history " | ||
| "has been removed by the workspace retention policy and there doesn't " | ||
| "appear to be any active discussions." | ||
| ) | ||
|
|
||
| ARCHIVE_CHANNEL_DELAY_SECONDS = 2 | ||
|
|
||
|
|
||
| @datadog_log | ||
| def archive_stale_channels() -> None: | ||
| slack = SlackService() | ||
| if not slack.client: | ||
| logger.error( | ||
| "Slack client not initialized -- disabling archive_stale_channels schedule" | ||
| ) | ||
| Schedule.objects.filter(name="archive_stale_channels").update(repeats=0) | ||
| return | ||
|
|
||
| own_bot_id = slack.bot_id | ||
| if not own_bot_id: | ||
| logger.error("Could not determine own bot ID, aborting archive run") | ||
| return | ||
|
|
||
| terminal_statuses = [IncidentStatus.DONE, IncidentStatus.CANCELED] | ||
| links = ExternalLink.objects.filter( | ||
| type=ExternalLinkType.SLACK, | ||
| incident__status__in=terminal_statuses, | ||
| ).select_related("incident") | ||
|
|
||
| scanned = 0 | ||
|
Check warning on line 46 in src/firetower/incidents/tasks/archive.py
|
||
| archived = 0 | ||
| skipped = 0 | ||
| errored = 0 | ||
|
|
||
| for i, link in enumerate(links): | ||
| if i > 0: | ||
| time.sleep(ARCHIVE_CHANNEL_DELAY_SECONDS) | ||
|
Check warning on line 53 in src/firetower/incidents/tasks/archive.py
|
||
|
|
||
| scanned += 1 | ||
| channel_id = slack.parse_channel_id_from_url(link.url) | ||
| if not channel_id: | ||
| skipped += 1 | ||
| continue | ||
|
|
||
| try: | ||
| info = slack.get_channel_info(channel_id) | ||
| if info is None: | ||
| logger.warning( | ||
| f"Could not fetch info for channel {channel_id} " | ||
| f"(incident {link.incident.incident_number}), skipping" | ||
| ) | ||
| skipped += 1 | ||
| continue | ||
|
|
||
| if info.get("is_archived"): | ||
| skipped += 1 | ||
| continue | ||
|
|
||
|
github-actions[bot] marked this conversation as resolved.
|
||
| has_activity = False | ||
| own_messages: list[dict] = [] | ||
| for page in slack.iter_channel_history(channel_id): | ||
| for msg in page: | ||
| if msg.get("bot_id") != own_bot_id: | ||
| has_activity = True | ||
| break | ||
| own_messages.append(msg) | ||
| if has_activity: | ||
| break | ||
| if has_activity: | ||
| skipped += 1 | ||
| continue | ||
|
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. System messages block stale archivalMedium Severity The stale-channel check treats any history item whose Reviewed by Cursor Bugbot for commit 033c52d. Configure here.
Member
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. Intentional. System messages (channel_join, channel_purpose, etc.) lack — Claude Code |
||
|
|
||
| for msg in own_messages: | ||
| if msg.get("reply_count", 0) > 0: | ||
| replies = slack.get_thread_replies(channel_id, msg["ts"]) | ||
| if replies: | ||
| has_activity = True | ||
| break | ||
|
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. Thread bot replies ignoredMedium Severity When deciding if a channel is stale, top-level history treats any message whose Additional Locations (1)Reviewed by Cursor Bugbot for commit f36375b. Configure here.
Member
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. Intentional asymmetry. The top-level check is conservative: any non-Firetower message (including other bots) counts as activity to avoid wrongful archival. The thread check uses — Claude Code |
||
| if has_activity: | ||
| skipped += 1 | ||
| continue | ||
|
rgibert marked this conversation as resolved.
rgibert marked this conversation as resolved.
|
||
|
|
||
| notice_ts = slack.post_message(channel_id, ARCHIVE_NOTICE) | ||
|
github-actions[bot] marked this conversation as resolved.
|
||
| if not notice_ts: | ||
| logger.error( | ||
| f"Failed to post archive notice to channel {channel_id} " | ||
| f"(incident {link.incident.incident_number}), skipping archive" | ||
| ) | ||
| errored += 1 | ||
| continue | ||
|
|
||
| try: | ||
| if not slack.archive_channel(channel_id): | ||
| raise RuntimeError( | ||
| f"archive_channel returned False for {channel_id}" | ||
| ) | ||
| archived += 1 | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| logger.info( | ||
| f"Archived stale channel {channel_id} " | ||
| f"(incident {link.incident.incident_number})" | ||
| ) | ||
| except Exception: | ||
| errored += 1 | ||
| logger.exception( | ||
| f"Failed to archive channel {channel_id} " | ||
| f"(incident {link.incident.incident_number}), " | ||
|
Comment on lines
+118
to
+122
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. Bug: A Suggested FixRemove the redundant logging. Either the Prompt for AI AgentAlso affects:
|
||
| f"deleting notice" | ||
| ) | ||
| slack.delete_message(channel_id, notice_ts) | ||
| except Exception: | ||
| errored += 1 | ||
| logger.exception( | ||
| f"Error processing channel {channel_id} " | ||
| f"(incident {link.incident.incident_number})" | ||
| ) | ||
|
|
||
| logger.info( | ||
| f"archive_stale_channels complete: " | ||
| f"scanned={scanned} archived={archived} skipped={skipped} errored={errored}" | ||
| ) | ||


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.
Archiver exceeds worker timeout
Medium Severity
The daily archiver sleeps two seconds before each Slack link after the first, while
Q_CLUSTERuses a 180-second task timeout. With dozens of terminal-incident SlackExternalLinkrows, sleep alone can exceed the limit, so django-q may terminate the run before every channel is scanned.Reviewed by Cursor Bugbot for commit 3968031. Configure here.
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.
Acknowledged. The 2s delay is intentional to stay under Slack's Tier 3 rate limit (~50 req/min). The task is idempotent -- if it times out, the next daily run picks up where it left off since already-archived channels are skipped via
is_archivedcheck. If channel volume grows enough to make this a real problem, we can batch into smaller chunks or move to an async queue, but that's premature for the current scale.— Claude Code