For a side project I needed background jobs without Redis. ~80 lines on Postgres:
UPDATE jobs SET status = 'running', started_at = now()
WHERE id = (
SELECT id FROM jobs WHERE status = 'queued' AND run_at <= now()
ORDER BY run_at FOR UPDATE SKIP LOCKED LIMIT 1
) RETURNING *;
Workers poll every second; SKIP LOCKED lets several workers run without stepping on each other. Retries with backoff are a run_at update. It handles a few thousand jobs a day without breaking a sweat.
- Reacted by