-
Notifications
You must be signed in to change notification settings - Fork 2
feat: auto-refresh Signal Feed every 120s #40
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
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,93 @@ | ||
| """Background poll loop that keeps the Signal Feed cache warm. | ||
|
|
||
| The poller is a single asyncio.Task owned by the FastAPI lifespan, mirroring | ||
| `pr_watcher.py`. Every POLL_INTERVAL seconds it walks every credential row, | ||
| calls the matching fetcher, and overwrites the cache entry. Users opening | ||
| the feed in the meantime read the warm cache in <100ms instead of paying | ||
| the 2-5s live-fetch cost. | ||
|
|
||
| The poller is write-only against `signal_feed_cache` — it never reads. | ||
| Reading would defeat its purpose (a hit would short-circuit the refresh | ||
| the poller exists to perform). | ||
|
|
||
| No startup-staleness gate (unlike pr_watcher), because warming the cache | ||
| on tick 1 produces no user-visible side effect — there's no review being | ||
| dispatched, no notification being sent. Worst case after a long downtime: | ||
| the first tick repopulates from-scratch, exactly as if every user had just | ||
| opened the page. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from typing import Awaitable, Callable | ||
|
|
||
| from app.models.credential import CredentialModel | ||
| from app.services import feed_fetchers, signal_feed_cache | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| POLL_INTERVAL_SECONDS = 120 | ||
|
|
||
| # Only these services produce a Signal Feed payload. A user can have other | ||
| # credentials (e.g. discord) — those rows are skipped, not errored. | ||
| FEED_FETCHERS: dict[str, Callable[[str], Awaitable[dict]]] = { | ||
| "slack": feed_fetchers.slack_messages, | ||
| "gmail": feed_fetchers.gmail_messages, | ||
| "github": feed_fetchers.github_activity, | ||
| } | ||
|
|
||
|
|
||
| class FeedPoller: | ||
| def __init__(self, poll_interval: float = POLL_INTERVAL_SECONDS): | ||
| self._poll_interval = poll_interval | ||
|
|
||
| async def run_forever(self) -> None: | ||
| """Sleep-tick-sleep until cancelled by the lifespan shutdown.""" | ||
| logger.info("feed_poller: started, polling every %ss", self._poll_interval) | ||
| try: | ||
| while True: | ||
| try: | ||
| await self.tick() | ||
| except Exception: # noqa: BLE001 — never let the loop die | ||
| logger.exception("feed_poller: tick crashed; continuing") | ||
| await asyncio.sleep(self._poll_interval) | ||
| except asyncio.CancelledError: | ||
| logger.info("feed_poller: shutting down") | ||
| raise | ||
|
|
||
| async def tick(self) -> None: | ||
| """One pass over every (user, feed-service) credential row. | ||
|
|
||
| Error isolation is at the (user, service) granularity: a Slack | ||
| outage for one user must not skip Gmail for the same user, nor | ||
| any other user's feeds. | ||
| """ | ||
| rows = CredentialModel.list_active_services() | ||
| if not rows: | ||
| return | ||
|
|
||
| refreshed = 0 | ||
| for row in rows: | ||
| user_id = row["user_id"] | ||
| service = row["service"] | ||
| fetcher = FEED_FETCHERS.get(service) | ||
| if fetcher is None: | ||
| continue # discord et al. — no feed surface | ||
| try: | ||
| payload = await fetcher(user_id) | ||
| except Exception: # noqa: BLE001 — isolate per (user, service) | ||
| logger.exception( | ||
| "feed_poller: fetch failed user=%s service=%s", | ||
| user_id, service, | ||
| ) | ||
| continue | ||
|
|
||
| # Only cache successful fetches. A connected=False response | ||
| # means the credential vanished mid-tick (raced with disconnect) | ||
| # or the upstream rejected the token — don't paper over it. | ||
| if payload.get("connected"): | ||
| signal_feed_cache.set(user_id, service, payload) | ||
| refreshed += 1 | ||
|
|
||
| if refreshed: | ||
| logger.info("feed_poller: refreshed %s cache entries", refreshed) | ||
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
Oops, something went wrong.
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.
CLAUDE.md docs convention violation —
FeedPolleris a peer toPRWatcherbut is not documented in CLAUDE.md's Architecture section.CLAUDE.md L25–27 documents the PR watcher service (its behavior, env gate, and file path). The FeedPoller is an equivalent background worker wired into the same lifespan, but the Architecture section has no entry for it.
CLAUDE.md rule: "When behavior or setup changes, update the relevant md (
README.md,LOCAL_SETUP.md,ROADMAP.md, this file) in the same change."The PR updates
backend/.env.examplewithFEED_POLLER_ENABLED(correct), but CLAUDE.md itself needs a matching architecture bullet, e.g.: