Reschedule cron job when confirmed subscriber changes frequency #2

Merged
matt merged 1 commit from mailship-e04-frequency-change-updates-db-but-never-re-63d into main 2026-09-10 19:55:43 +00:00
Collaborator

mailship-e04

PUT /subscription/email with a changed frequency updated the row in the database but never rescheduled the running CronJob. A confirmed subscriber switching from daily to weekly kept the daily cadence until the process restarted.

Changes:

  • src/actions.ts — In registerSubscription, the existing code distinguished between new/unconfirmed subscriptions (send confirmation email) and already-confirmed ones (skip email). Added an else branch that calls worker.registerSubscription(sub) for confirmed subscriptions, which triggers addJob — stopping the old cron job and creating a new one with the updated frequency.

  • src/worker/email.ts — Exported getJobCronSource() to allow test code to inspect the active cron job's expression, enabling a direct assertion that addJob updated the schedule.

  • test/reschedule-on-frequency-change.test.js — New failing-before/passing-after test. Creates a confirmed subscription, registers a daily job, then calls registerSubscription with weekly frequency. Asserts the cron expression changed from 0 0 17 * * * (daily) to 0 0 17 * * 1 (weekly). Before the fix this assertion failed; after the fix it passes.

How to test:

MAILSHIP_URL=http://localhost:4741 \
MAILSHIP_NAME=Test \
MAILSHIP_SECRET=$(openssl rand -hex 32) \
SMTP_HOST=... SMTP_PORT=... SMTP_USER=... SMTP_PASSWORD=... SMTP_FROM=... \
DEFAULT_RELAYS=wss://relay.example.com \
INDEXER_RELAYS=... SEARCH_RELAYS=... \
PORT=4741 BASE_URL=http://localhost:4741 \
DATA_DIR=/tmp/mailship-test \
node test/reschedule-on-frequency-change.test.js
mailship-e04 `PUT /subscription/email` with a changed frequency updated the row in the database but never rescheduled the running CronJob. A confirmed subscriber switching from daily to weekly kept the daily cadence until the process restarted. **Changes:** - `src/actions.ts` — In `registerSubscription`, the existing code distinguished between new/unconfirmed subscriptions (send confirmation email) and already-confirmed ones (skip email). Added an `else` branch that calls `worker.registerSubscription(sub)` for confirmed subscriptions, which triggers `addJob` — stopping the old cron job and creating a new one with the updated frequency. - `src/worker/email.ts` — Exported `getJobCronSource()` to allow test code to inspect the active cron job's expression, enabling a direct assertion that `addJob` updated the schedule. - `test/reschedule-on-frequency-change.test.js` — New failing-before/passing-after test. Creates a confirmed subscription, registers a daily job, then calls `registerSubscription` with weekly frequency. Asserts the cron expression changed from `0 0 17 * * *` (daily) to `0 0 17 * * 1` (weekly). Before the fix this assertion failed; after the fix it passes. **How to test:** ```bash MAILSHIP_URL=http://localhost:4741 \ MAILSHIP_NAME=Test \ MAILSHIP_SECRET=$(openssl rand -hex 32) \ SMTP_HOST=... SMTP_PORT=... SMTP_USER=... SMTP_PASSWORD=... SMTP_FROM=... \ DEFAULT_RELAYS=wss://relay.example.com \ INDEXER_RELAYS=... SEARCH_RELAYS=... \ PORT=4741 BASE_URL=http://localhost:4741 \ DATA_DIR=/tmp/mailship-test \ node test/reschedule-on-frequency-change.test.js ```
hudson added 1 commit 2026-09-10 15:31:54 +00:00
When a confirmed subscriber calls PUT /subscription/email with a changed
frequency, db.updateSubscription updates the row but the running CronJob
captured the original frequency in createJob and was never rescheduled.
A subscriber switching daily→weekly kept the daily cadence until restart.

Fix: in actions.ts:registerSubscription, call worker.registerSubscription(sub)
when the subscription is already confirmed, so addJob stops the old job and
creates a new one with the updated frequency.

Changes:
- src/actions.ts: add else branch calling worker.registerSubscription when
  sub.confirmed_at is set
- test/reschedule-on-frequency-change.test.js: new failing-before/passing-after
  test verifying the cron expression is updated after frequency change

Closes mailship-e04
matt approved these changes 2026-09-10 19:55:39 +00:00
matt merged commit 694791bfdd into main 2026-09-10 19:55:43 +00:00
matt deleted branch mailship-e04-frequency-change-updates-db-but-never-re-63d 2026-09-10 19:55:49 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: matt/mailship#2
No description provided.