Decide whether the bot's timers should keep the database compute awake continuously
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 12/100
- Type d'issue
- Refactorisation
- Clarté
- À clarifier
- Activité
- Active
- Stack technique
- postgresql, typescript
- Domaine
- backend, database, infrastructure
Piste de recherche
This is a design decision, not a code change. Start in bot/index.ts by reading the timer constants (LISTEN_HEARTBEAT_INTERVAL_MS, the 5-minute sweeps) and in lib/slack/poller.ts the Slack poll interval, then check railway.bot.toml for deployment settings. Done means a written decision on the four options, coordinated with #128 / PR #130 for the dashboard heartbeat, and any follow-up code is agreed with maintainers first.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Copied from DeadantCo/answerLoops#30 · originally opened by sethr07 on 2026-11-20
Copied from https://github.com/answerLoops/answerLoops/issues/131 — originally opened by
@FaithTarberton 2026-09-08
Background
The bot is a long-running process, not a webhook receiver: it holds a Discord Gateway connection (discord.js Client with GatewayIntentBits), is started as tsx bot/index.ts, and is deployed by railway.bot.toml with restartPolicyType = "on_failure". It must stay up to ingest Discord messages — app/api/discord/* is OAuth and channel listing only, with no HTTP ingestion path.
While it runs, several of its timers query the database often enough that a serverless compute can never reach its idle window (Neon's default is 5 minutes):
Timer (constant in bot/index.ts) |
Interval | What it does |
|---|---|---|
LISTEN_HEARTBEAT_INTERVAL_MS |
4 min | SELECT 1 on the dedicated LISTEN connection; a failure triggers reconnect and re-LISTEN. Database work every beat. |
STUCK_TICKET_SWEEP_INTERVAL_MS |
5 min | getStuckPendingTickets(), then forwards each stuck ticket to /api/ingest/retry-stuck. Skipped entirely when BOT_SECRET is unset. |
EMBEDDING_MIGRATION_SWEEP_INTERVAL_MS |
5 min | Calls the app route that finds and re-embeds workspaces whose vectors are not on their target model (the request itself is bounded by EMBEDDING_MIGRATION_REQUEST_TIMEOUT_MS). Skipped without BOT_SECRET. |
KB_SYNC_SAFETY_SWEEP_INTERVAL_MS |
5 min | Safety net for the knowledge-base sync queue: reclaims jobs stuck in running (KB_SYNC_STUCK_THRESHOLD_MS) and claims queued ones. The queue is normally driven by a kb_sync_job_queued notification, so this only matters when a notification is dropped. |
KB_RETENTION_SWEEP_INTERVAL_MS |
24 hours | Blanks visitor text on old review items and prunes expired article-usage rows. Also runs once at startup. |
ORG_PURGE_SWEEP_INTERVAL_MS |
6 hours | Hard-purges workspaces whose deletion grace period has elapsed. One indexed query when nothing is due. Also runs once at startup. |
Slack poller (DEFAULT_INTERVAL_MS in lib/slack/poller.ts, from SLACK_POLL_INTERVAL_SECONDS) |
60 sec | No work on cloud unless a workspace has the slack_force_polling flag, because the bot only hands it the flagged orgs. Does database and network work when self-hosted. |
The three 5-minute sweeps and the 4-minute heartbeat all sit at or under a 5-minute idle window, so each one on its own is enough to keep a serverless compute awake. The 6-hour and 24-hour sweeps are negligible.
The effect is that the compute runs continuously whenever the bot is up, rather than suspending when nothing is happening. Because this is a single database with row-level tenancy (org_id), that is one shared compute for every tenant — there is no per-customer endpoint to suspend independently, so this is a fixed floor rather than something that scales with usage.
Why this is worth a decision
The 4-minute heartbeat interval appears chosen to sit under a 5-minute autosuspend window, i.e. it prevents suspension as much as it detects a dead connection. That was a reasonable trade when it was written — #71 / #77 exist because a postgres.js LISTEN goes silently stale on a suspending compute, with nothing logged and config hot-reload quietly dead until the process restarted.
But the cost of that choice is now the dominant term in idle database spend, and it is worth being deliberate about rather than inheriting. Note that lengthening the interval alone does not recover suspension: any periodic query restarts the idle countdown, so a 10-minute beat keeps the compute up roughly half the time rather than all of it. The 5-minute sweeps have the same property and would all have to move too.
Options
- Accept it. Config changes reach the bot within seconds and a dead LISTEN is caught within four minutes. Simplest, and defensible if compute cost is not a real constraint.
- Lengthen the heartbeat and all of the 5-minute sweeps together, accepting slower config propagation and slower stuck-ticket recovery in exchange for partial suspension. Only meaningful if both move past the idle window.
- Drop the periodic heartbeat and re-check config lazily. The bot only needs current config when it processes a message — and processing a message already queries the database, which wakes the compute anyway. A config version check at message time (with LISTEN kept as a fast path rather than the source of truth) would let the compute suspend while genuinely idle, at the cost of a config change not reaching an idle bot until its next message. Needs care: this is the failure #71 / #77 were about, so it should be correct-by-construction rather than relying on the LISTEN staying alive.
- Make the stuck-ticket sweep (and the embedding migration sweep) event-driven rather than 5-minute polls, the way the KB sync worker already is, independently of whichever heartbeat option is chosen.
Interaction with #128
Decide together with #128 / PR #130 (dashboard heartbeat).
PR #130 adds a 4-minute heartbeat to the dashboard's own SSE LISTEN connection. That was justified partly on the grounds that it costs nothing extra in compute-hours because the bot already holds the compute awake. If this issue is resolved by letting the compute suspend, that reasoning no longer holds and the dashboard heartbeat becomes the thing preventing suspension whenever a tab is open. The two should be decided together.
The dashboard case is less severe — its stream exists only while a tab is open, and closes when the tab is backgrounded — but it would need revisiting.
Acceptance
Decide and document. If the decision is to change behaviour, the bot should still detect and recover from a dead LISTEN connection without relying on a process restart, and a config change should still reach the bot before it acts on the next message.
Start here: new to the project? Read CONTRIBUTING.md first. It covers the ground rules, running answerLoops locally, and how to open a pull request.
- Langage dominant
- TypeScript
- Étoiles
- 0
- Forks
- 0
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
- Fournit un Dockerfile ou un fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de DeadantCo/answerLoopsdev
-
Widget: match the chat bubble color to the rest of the widgetPeut-être pris @sethr07 l’a pris aujourd’hui. Ouverteenhancement frontend help wanted
Difficulté 2/5 Une demi-journée Accessibilité débutants 75/100
DeadantCo/answerLoopsdev#68 · 3 commentaires ·
-
Widget: make the chat window fit on small phonesPeut-être pris @sethr07 l’a pris aujourd’hui. Ouverteenhancement frontend good first issue help wanted
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
DeadantCo/answerLoopsdev#67 · 9 commentaires ·
-
Add 'select all' option to Slack channel picker in SettingsPeut-être pris @sethr07 l’a pris aujourd’hui. Ouvertefrontend good first issue
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
DeadantCo/answerLoopsdev#22 · 5 commentaires ·
-
enhancement frontend help wanted
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
DeadantCo/answerLoopsdev#141 ·
-
backend enhancement frontend help wanted
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
DeadantCo/answerLoopsdev#140 ·
Toutes les issues de DeadantCo/answerLoopsdev
Issues similaires
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulté 1/5 Moins d'une heure Accessibilité débutants 92/100
lingdojo/kana-dojo#34545 · 1 commentaire · 5 réactions ·
Les mainteneurs répondent en général sous 1 jour
-
Blocklist addition requestOuverteblocklist addition
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
MetaMask/eth-phishing-detect#315455 ·
Les mainteneurs répondent en général sous 1 jour
-
area: telegram bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
griffinwork40/agent-afk#5275 ·
Les mainteneurs répondent en général sous 1 jour
-
macOS: the Save Diagnostics panel goes behind the editor after an app switch and can't be reachedOuvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
getopenscreen/openscreen#1667 ·
Les mainteneurs répondent en général sous 1 jour
-
documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
erkamyaman/rust-anything#371 ·
Les mainteneurs répondent en général sous 1 jour