|
| 1 | +# Notification System |
| 2 | + |
| 3 | +Developer reference for the notification subsystem. For user-facing setup (webhook URLs, RSS endpoint, social media links), see [README.md](../README.md). For cloud credential provisioning, see [CONFIGURATION.md](../CONFIGURATION.md). |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +When the reported system status changes (e.g. green to yellow), `check_status.py` dispatches notifications to all configured channels: |
| 10 | + |
| 11 | +| Channel | Module | Credentials required | Notes | |
| 12 | +|---|---|---|---| |
| 13 | +| Bluesky | `lib/notifiers/bluesky.py` | Yes | Posts via AT Protocol | |
| 14 | +| Mastodon | `lib/notifiers/mastodon.py` | Yes | Posts via Mastodon API | |
| 15 | +| RSS | `lib/notifiers/rss.py` | No | Always enabled; writes XML to cache | |
| 16 | +| Webhooks | `lib/notifiers/webhooks.py` | Yes (URLs) | Auto-detects Slack, Discord, Teams | |
| 17 | + |
| 18 | +All channels use shared status messages from `lib/notifiers/messages.py`. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Architecture |
| 23 | + |
| 24 | +### Call Flow |
| 25 | + |
| 26 | +``` |
| 27 | +check_status.py |
| 28 | + ├── detect status, apply hysteresis → reported_status |
| 29 | + ├── determine should_notify (see Notification Triggers below) |
| 30 | + │ |
| 31 | + ├── notify_status_change(status, previous_status, delay_summaries, timestamp) |
| 32 | + │ ├── bluesky.post_to_bluesky() # if BLUESKY_* env vars set |
| 33 | + │ ├── mastodon.post_to_mastodon() # if MASTODON_* env vars set |
| 34 | + │ ├── rss.update_rss_feed() # always |
| 35 | + │ └── webhooks.send_webhooks() # if WEBHOOK_URLS set |
| 36 | + │ |
| 37 | + ├── check results for failures |
| 38 | + └── if no failures: cache_data['last_notified_status'] = current_reported |
| 39 | +``` |
| 40 | + |
| 41 | +The dispatcher (`lib/notifiers/dispatcher.py`) checks environment variables for each channel. Unconfigured channels return a `skipped` result immediately without attempting any network calls. |
| 42 | + |
| 43 | +### Notification Triggers |
| 44 | + |
| 45 | +`check_status.py` sends notifications in two cases: |
| 46 | + |
| 47 | +1. **Hysteresis transition** — `apply_status_hysteresis()` reports `status_changed=True` and there was a previous reported status. This is the normal path: the smoothed status has changed. |
| 48 | + |
| 49 | +2. **Missed notification recovery** — The hysteresis status did not change, but `last_notified_status` differs from the current reported status. This catches cases where a previous notification attempt failed partway through (some channels succeeded, some didn't), so the next successful check retries the notification. |
| 50 | + |
| 51 | +```python |
| 52 | +# Case 1: hysteresis transition |
| 53 | +if hysteresis_result['status_changed'] and previous_reported_status is not None: |
| 54 | + should_notify = True |
| 55 | + |
| 56 | +# Case 2: missed notification recovery |
| 57 | +elif previous_last_notified is not None and previous_last_notified != current_reported: |
| 58 | + should_notify = True |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Channel Return Format |
| 64 | + |
| 65 | +Every channel function returns a dict with this common structure: |
| 66 | + |
| 67 | +```python |
| 68 | +{ |
| 69 | + 'success': bool, # True if the notification was delivered |
| 70 | + 'skipped': bool, # True if channel is not configured (credentials missing) |
| 71 | + 'error': str, # Error message, or None on success |
| 72 | + # ... plus channel-specific fields (see below) |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### Tri-state logic |
| 77 | + |
| 78 | +| `success` | `skipped` | Meaning | |
| 79 | +|---|---|---| |
| 80 | +| `True` | `False` | Delivered successfully | |
| 81 | +| `False` | `True` | Channel not configured — not a failure | |
| 82 | +| `False` | `False` | Attempted but failed (network error, auth error, etc.) | |
| 83 | + |
| 84 | +`check_status.py` treats `skipped` results as non-failures. Only `success=False, skipped=False` results count as failures and prevent `last_notified_status` from being updated. |
| 85 | + |
| 86 | +### Channel-specific fields |
| 87 | + |
| 88 | +| Channel | Extra fields | |
| 89 | +|---|---| |
| 90 | +| Bluesky | `uri` — AT Protocol URI of the created post | |
| 91 | +| Mastodon | `url` — URL of the created post | |
| 92 | +| RSS | `path` — file path or `gs://` URL where the feed was written | |
| 93 | +| Webhooks | `sent` — count of successful webhooks, `failed` — count of failed webhooks | |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Duplicate Prevention |
| 98 | + |
| 99 | +### `last_notified_status` |
| 100 | + |
| 101 | +The cache stores `last_notified_status` — the status string (`'green'`, `'yellow'`, `'red'`) that was last successfully sent to all channels. |
| 102 | + |
| 103 | +**Update rule:** `last_notified_status` is only updated when *no channel fails*. If any configured channel fails (not skipped, but actually fails), the value is left unchanged so the next check cycle will retry via the missed notification recovery path. |
| 104 | + |
| 105 | +**Flow:** |
| 106 | + |
| 107 | +``` |
| 108 | +1. Read previous_last_notified from cache |
| 109 | +2. Determine should_notify (transition or recovery) |
| 110 | +3. Dispatch to all channels |
| 111 | +4. If any_failed: |
| 112 | + last_notified_status stays unchanged → next cycle retries |
| 113 | +5. If no failures: |
| 114 | + last_notified_status = current_reported → no retry needed |
| 115 | +``` |
| 116 | + |
| 117 | +### Interaction with hysteresis |
| 118 | + |
| 119 | +The hysteresis system (`apply_status_hysteresis()`) prevents rapid status flips by requiring consistent readings before changing `reported_status`. Notifications are gated behind hysteresis — a notification is only sent when `reported_status` actually changes, not on every raw detection fluctuation. |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## Configuration |
| 124 | + |
| 125 | +### Environment Variables |
| 126 | + |
| 127 | +| Variable | Channel | Description | |
| 128 | +|---|---|---| |
| 129 | +| `BLUESKY_HANDLE` | Bluesky | Account handle (e.g. `munimetro.bsky.social`) | |
| 130 | +| `BLUESKY_APP_PASSWORD` | Bluesky | App password for the account | |
| 131 | +| `MASTODON_INSTANCE` | Mastodon | Instance URL (e.g. `https://mastodon.social`) | |
| 132 | +| `MASTODON_ACCESS_TOKEN` | Mastodon | Access token for the account | |
| 133 | +| `WEBHOOK_URLS` | Webhooks | Comma-separated list of webhook URLs | |
| 134 | + |
| 135 | +RSS requires no environment variables — it writes to the local cache directory (or GCS when `CLOUD_RUN` is set). |
| 136 | + |
| 137 | +For cloud credential setup and secret management, see [CONFIGURATION.md](../CONFIGURATION.md). |
| 138 | + |
| 139 | +### Webhook Platform Detection |
| 140 | + |
| 141 | +`send_webhooks()` auto-detects the platform from the URL and formats the payload accordingly: |
| 142 | + |
| 143 | +| URL pattern | Platform | Payload format | |
| 144 | +|---|---|---| |
| 145 | +| `hooks.slack.com` | Slack | Slack incoming webhook | |
| 146 | +| `discord.com/api/webhooks` | Discord | Discord embed | |
| 147 | +| `webhook.office.com` / `.logic.azure.com` | Teams | MessageCard | |
| 148 | +| Anything else | Generic | JSON with `status`, `previous_status`, `description`, `delay_summaries`, `timestamp` | |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## Key Files |
| 153 | + |
| 154 | +| File | Purpose | |
| 155 | +|---|---| |
| 156 | +| `lib/notifiers/__init__.py` | Public API: re-exports `notify_status_change` and channel functions | |
| 157 | +| `lib/notifiers/dispatcher.py` | `notify_status_change()` — dispatches to all channels, checks env vars | |
| 158 | +| `lib/notifiers/bluesky.py` | `post_to_bluesky()` — AT Protocol client | |
| 159 | +| `lib/notifiers/mastodon.py` | `post_to_mastodon()` — Mastodon API client | |
| 160 | +| `lib/notifiers/rss.py` | `update_rss_feed()`, `read_rss_feed()` — RSS 2.0 feed generation | |
| 161 | +| `lib/notifiers/webhooks.py` | `send_webhooks()` — multi-platform webhook delivery | |
| 162 | +| `lib/notifiers/messages.py` | `STATUS_MESSAGES` — shared message templates | |
| 163 | +| `api/check_status.py` | Notification trigger logic, `last_notified_status` management | |
0 commit comments