You're on-call. Support has escalated three tickets from the past week:
- Ticket #1841 — "I got the same welcome email three times"
- Ticket #2203 — "I never received my onboarding email"
- Ticket #2391 — "I received my step-3 email before my step-1 email"
The service has been running fine in staging. Production has multiple worker instances deployed behind a load balancer. The issues started around the same time the team scaled from one worker to three.
Your job is to diagnose what's going wrong and fix it.
| File | Responsibility |
|---|---|
src/scheduler.ts |
Entry point. Starts the poll loop. |
src/jobRunner.ts |
Fetches pending jobs and processes them. |
src/jobRepository.ts |
All database reads/writes for the jobs table. |
src/emailService.ts |
Calls the external email API. |
# Install dependencies
npm install
# Start a local Postgres database (Docker)
docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres:16
# Create the schema and seed data
psql postgresql://postgres:postgres@localhost:5432/postgres -f schema.sql
# Run the tests
npm test
# Start the scheduler locally
DB_PASSWORD=postgres npm start| Variable | Default | Description |
|---|---|---|
DB_HOST |
localhost |
Postgres host |
DB_PORT |
5432 |
Postgres port |
DB_NAME |
jobqueue |
Database name |
DB_USER |
postgres |
Database user |
DB_PASSWORD |
(empty) | Database password |
EMAIL_API_KEY |
(empty) | API key for email provider |
EMAIL_FROM |
onboarding@example.com |
Sender address |
- Do not change the database schema in a way that breaks the existing
SELECTindex on(status, created_at). - The fix must work correctly with N concurrent worker processes (N ≥ 1).
- Unit tests must continue to pass after your changes.
- Do not remove the
attemptscolumn — it will be used downstream.