fix(web): start the webhook cleanup job at server startup

Closed
PO
4d ago
+65-0

Webhook delivery history was never cleaned up, so the shared database grew with every delivery for every team. The server now starts the daily cleanup job that removes records older than the retention window, which bounds the table's size. The job registers its schedule when its module loads, and no code imported it, while every other background job starts in the same function. Adding that import starts the job. The retention window and the database schema are unchanged.

Production impact at 2ff11b3: Startup adds a daily cleanup job; no schema or request-path change

Repository lint: pnpm run lint (declared in package.json) fails over the whole repository (the declared target takes no file arguments), so the failure is not attributed to this change and did not block it; run it before merging.

Fix walkthrough

Watch the fix walkthrough

Watch the fix explained (1:23). Recorded for commit 2ff11b3cf3c56aca43337659d83d1c623833086e.

Full analysis
flowchart LR
  R["instrumentation.ts register()"] -->|now imports| J["webhook-cleanup-job.ts"]
  J --> S["upsertJobScheduler<br/>daily 03:00 UTC"]
  J --> W["Worker on webhook-cleanup queue"]
  S --> W
  W --> D["db.webhookCall.deleteMany<br/>createdAt < 30 days"]
  D --> T["WebhookCall table bounded"]
Detection evidence

Affected: int_13ee3c91c001c2c8xfdj06a9

Out of scope
  • The 30-day retention window and the WebhookCall schema stay as they are; only the job wiring changes.
  • Row counts and growth rate: the connected Railway account is read-only and returned Not Authorized, so they were not measured.

Production impact

I checked the deployed Railway service unsend (production, 1 replica), which this repository deploys to. The changed function only starts background jobs at boot and is on no request path. The new job registers one BullMQ schedule and deletes rows older than 30 days once daily: additive, no schema change, removes nothing. Railway metrics for the service returned 403 Not Authorized with the read-only token, so its traffic was not readable; the change carries no production mechanism to degrade it.

Affected resources

  • unsend

Observability

  • Startup already logs Registering instrumentation in apps/web/src/instrumentation.ts#register.
  • The enabled job logs [WebhookCleanupJob]: Deleted old webhook calls with the deleted count and cutoff, plus [WebhookCleanupJob]: Job completed / Job failed.
  • This change adds no new telemetry; it starts existing logging.

Polylane assessed this pull request at 2ff11b3.

Causal chain
  • Signal (alert): Webhook delivery records are never deleted: the cleanup job is never started
  • Surfacing site: usesend/useSend at apps/web/src/instrumentation.ts#register
  • Mechanism: register() starts every other background job but never imports ~/server/jobs/webhook-cleanup-job, so the module's daily BullMQ scheduler and Worker are never registered and WebhookCall rows older than 30 days are never deleted.
  • Producer: apps/web/src/instrumentation.ts#register, at apps/web/src/instrumentation.ts#register
  • Last observed: 2026-11-12T23:07:00.000Z
  • Why the fix is still needed while the signal is quiet: This is a code-path finding, not a runtime metric. The module is still unimported on the default branch at commit 89d5ff4, verified by inspection in this run: the causal path is unchanged and the defect remains even though the scan's signal timestamp is older than a day.
  • Blast radius: unknown other resource(s), unknown other tenant(s); data at risk: none named
  • Producer evidence:
    • apps/web/src/instrumentation.ts lines 11-46 start EmailQueueService.init, usage-job, initDomainVerificationJob, cleanup-email-bodies and CampaignSchedulerService.start but not ~/server/jobs/webhook-cleanup-job.
    • Repository-wide search for webhookCleanupQueue, WEBHOOK_CLEANUP_QUEUE, WEBHOOK_RETENTION_DAYS and webhook-cleanup returns only apps/web/src/server/jobs/webhook-cleanup-job.ts and apps/web/src/server/queue/queue-constants.ts:8, so nothing else imports or prunes the job.
    • apps/web/src/server/jobs/webhook-cleanup-job.ts registers its schedule at module load (line 40) and deletes rows older than 30 days (lines 19-26), so it acts only when imported.
    • apps/web/src/server/service/webhook-service.ts calls db.webhookCall.create for each delivery attempt on the dispatch and test paths, so the table grows per event for every team.
    • PR #505, the only PR merged after the scan, changes the SMTP Dockerfile, .dockerignore, publish.yml and pnpm-lock.yaml and does not touch the causal path.
2 files changed (+65/-0)
  • apps/web/src/instrumentation.ts: modified, +2/-0
  • apps/web/src/instrumentation.unit.test.ts: added, +63/-0
Lint output
ys were ignored: "pnpm.overrides". See https://pnpm.io/settings for the new home of each setting.
@usesend/ui:lint: $ eslint . --max-warnings 0
@usesend/lib:lint: $ eslint . --max-warnings 0
usesend-js:lint: cache miss, executing bdfdba0250238d1e
usesend-js:lint: [WARN] The "pnpm" field in package.json is no longer read by pnpm. The following keys were ignored: "pnpm.overrides". See https://pnpm.io/settings for the new home of each setting.
usesend-js:lint: $ eslint . --max-warnings 0
usesend-js:lint: sh: 1: eslint: not found
usesend-js:lint: [ELIFECYCLE] Command failed.
@usesend/ui:lint: [ELIFECYCLE] Command failed.

 Tasks:    1 successful, 3 total
Cached:    0 cached, 3 total
  Time:    3.486s 
Failed:    usesend-js#lint

[ELIFECYCLE] Command failed with exit code 1.
[WARN] The "pnpm" field in package.json is no longer read by pnpm. The following keys were ignored: "pnpm.overrides". See https://pnpm.io/settings for the new home of each setting.
$ turbo lint

Attention:
Turborepo now collects completely anonymous telemetry regarding usage.
This information is used to shape the Turborepo roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://turborepo.dev/docs/telemetry

usesend-js#lint:  ERROR  command (/workspace/usesend/useSend/packages/sdk) /root/.local/share/pnpm/.tools/pnpm/11.11.0/bin/pnpm run lint exited (1)
 ERROR  run failed: command  exited (1)

View autofix


Generated by Polylane. You can ask follow-ups by mentioning @polylane in a comment. If you close this pull request without merging, add a comment that says why. Polylane reads it to make its next fixes better.


Summary by cubic

Starts the webhook cleanup job at server startup so webhook delivery history no longer grows unbounded in the shared database.

The cleanup job registers its daily scheduler and worker only when its module is imported, and nothing imported it before. This adds that import to the server boot sequence and adds unit tests covering both configured and missing Redis URLs. The 30-day retention window and schema are unchanged.