Decide whether the bot's timers should keep the database compute awake continuously
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 12/100
- Tipo de issue
- Refactorización
- Claridad
- Necesita aclaración
- Estado de actividad
- Activo
- Stack tecnológico
- postgresql, typescript
- Área
- backend, database, infrastructure
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- TypeScript
- Estrellas
- 0
- Forks
- 0
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Incluye un Dockerfile o un archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de DeadantCo/answerLoopsdev
-
Widget: match the chat bubble color to the rest of the widgetPosiblemente ocupada @sethr07 la tomó hoy. Abiertoenhancement frontend help wanted
Dificultad 2/5 Medio día Aptitud para principiantes 75/100
DeadantCo/answerLoopsdev#68 · 3 comentarios ·
-
Widget: make the chat window fit on small phonesPosiblemente ocupada @sethr07 la tomó hoy. Abiertoenhancement frontend good first issue help wanted
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
DeadantCo/answerLoopsdev#67 · 9 comentarios ·
-
Add 'select all' option to Slack channel picker in SettingsPosiblemente ocupada @sethr07 la tomó hoy. Abiertofrontend good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
DeadantCo/answerLoopsdev#22 · 5 comentarios ·
-
enhancement frontend help wanted
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
DeadantCo/answerLoopsdev#141 ·
-
backend enhancement frontend help wanted
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
DeadantCo/answerLoopsdev#140 ·
Todos los issues de DeadantCo/answerLoopsdev
Issues similares
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
lingdojo/kana-dojo#34545 · 1 comentario · 5 reacciones ·
Los mantenedores suelen responder en 1 día
-
Blocklist addition requestAbiertoblocklist addition
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
MetaMask/eth-phishing-detect#315455 ·
Los mantenedores suelen responder en 1 día
-
area: telegram bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
griffinwork40/agent-afk#5275 ·
Los mantenedores suelen responder en 1 día
-
macOS: the Save Diagnostics panel goes behind the editor after an app switch and can't be reachedAbiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
getopenscreen/openscreen#1667 ·
Los mantenedores suelen responder en 1 día
-
documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
erkamyaman/rust-anything#371 ·
Los mantenedores suelen responder en 1 día