Run the scheduled jobs in the web process and drop Celery #148

Merged
CrowderSoup merged 1 commit from claude/retire-celery into main 2026-12-01 23:12:52 +00:00
Owner

vTodo task #686. Stacked on #147 (both change uv.lock). Merge #147 first, and this diff then shrinks to its own commit.

Nothing in vTodo queues Celery work. The worker container, which ran Celery with embedded beat, only existed for two 10-minute jobs. Both now run on a daemon thread in the web process.

What changed

  • config/periodic.py: a small scheduler. config/asgi.py starts it when RUN_PERIODIC_JOBS is on, and the web role in config/deploy.yml sets that. The first round runs 60 s after boot so it stays clear of the health check, then each job runs every settings.PERIODIC_JOBS interval (600 s). Each run:
    • takes a cache lock (Redis) for 80% of its interval. The old and new containers overlap during a deploy, and this keeps them from both running a job. If the cache is down, the job runs anyway.
    • is a GlitchTip transaction (op=task, named for the job) and is tagged with the job's name in Pyroscope as endpoint, as the Celery tasks were.
    • pings the same heartbeat monitors as before (HEARTBEAT_GCAL_SYNC_URL, HEARTBEAT_VUPLOAD_PURGE_URL) when it finishes. A failure is logged to GlitchTip, and the missing ping makes the monitor alert.
    • gives its pooled DB connection back afterwards.
    • logs periodic job … finished in Ns, which shows in kamal app logs.
  • python manage.py runjobs [name] runs the jobs once by hand, without the lock. It exits non-zero if a job fails.
  • Removed: the worker role, config/celery.py, celery, and Sentry's celery extra. The task functions are now plain functions.
  • .forgejo/workflows/deploy.yml has a new "Retire the old worker role" step that docker rm -fs the old worker container. Kamal leaves a removed role's container running, and that container would keep running the jobs. This follows the old mcp-role step and does nothing once the container is gone.
  • Redis stays. It's the cache for sessions, invite rate limits and these locks, and it's the droplet's shared Redis accessory. The URL keeps the CELERY_BROKER_URL name so the existing secret keeps working. Renaming it would need a secret change, so I left that out.

Checked locally

  • pytest: 976 passed (12 new in tests/test_periodic.py). makemigrations --check reports no changes.
  • I built the image and ran it with RUN_PERIODIC_JOBS=True. The log showed periodic jobs started: … at boot and both jobs finishing at the 60 s mark. /up/ returned 200, and runjobs purge worked in the container. The image has no celery.

After deploy

  • Both GlitchTip heartbeats should stay green.
  • docker ps --filter label=role=worker on the droplet should return nothing.

🤖 Generated with Claude Code

vTodo task #686. **Stacked on #147** (both change `uv.lock`). Merge #147 first, and this diff then shrinks to its own commit. Nothing in vTodo queues Celery work. The `worker` container, which ran Celery with embedded beat, only existed for two 10-minute jobs. Both now run on a daemon thread in the web process. **What changed** - `config/periodic.py`: a small scheduler. `config/asgi.py` starts it when `RUN_PERIODIC_JOBS` is on, and the web role in `config/deploy.yml` sets that. The first round runs 60 s after boot so it stays clear of the health check, then each job runs every `settings.PERIODIC_JOBS` interval (600 s). Each run: - takes a cache lock (Redis) for 80% of its interval. The old and new containers overlap during a deploy, and this keeps them from both running a job. If the cache is down, the job runs anyway. - is a GlitchTip transaction (`op=task`, named for the job) and is tagged with the job's name in Pyroscope as `endpoint`, as the Celery tasks were. - pings the same heartbeat monitors as before (`HEARTBEAT_GCAL_SYNC_URL`, `HEARTBEAT_VUPLOAD_PURGE_URL`) when it finishes. A failure is logged to GlitchTip, and the missing ping makes the monitor alert. - gives its pooled DB connection back afterwards. - logs `periodic job … finished in Ns`, which shows in `kamal app logs`. - `python manage.py runjobs [name]` runs the jobs once by hand, without the lock. It exits non-zero if a job fails. - Removed: the `worker` role, `config/celery.py`, `celery`, and Sentry's `celery` extra. The task functions are now plain functions. - `.forgejo/workflows/deploy.yml` has a new "Retire the old worker role" step that `docker rm -f`s the old worker container. Kamal leaves a removed role's container running, and that container would keep running the jobs. This follows the old mcp-role step and does nothing once the container is gone. - **Redis stays.** It's the cache for sessions, invite rate limits and these locks, and it's the droplet's shared Redis accessory. The URL keeps the `CELERY_BROKER_URL` name so the existing secret keeps working. Renaming it would need a secret change, so I left that out. **Checked locally** - `pytest`: 976 passed (12 new in `tests/test_periodic.py`). `makemigrations --check` reports no changes. - I built the image and ran it with `RUN_PERIODIC_JOBS=True`. The log showed `periodic jobs started: …` at boot and both jobs finishing at the 60 s mark. `/up/` returned 200, and `runjobs purge` worked in the container. The image has no celery. **After deploy** - Both GlitchTip heartbeats should stay green. - `docker ps --filter label=role=worker` on the droplet should return nothing. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Run the scheduled jobs in the web process and drop Celery
All checks were successful
tests / pytest (push) Successful in 1m17s
0e4c9a4709
Nothing in vTodo queued Celery work: the worker container (Celery plus
embedded beat) only ran two jobs every 10 minutes, Google Calendar sync and
the vUpload delete retry. They now run on a daemon thread in the web
process (config/periodic.py), started from config/asgi.py when
RUN_PERIODIC_JOBS is on, which the web role sets.

Each run takes a cache lock for most of its interval, so the old and new
containers during a deploy (or a second uvicorn worker) don't both run it.
It's a GlitchTip transaction and a Pyroscope endpoint named after the job,
hands its pooled DB connection back, and pings the job's heartbeat when it
finishes. `manage.py runjobs [name]` runs them once by hand.

The worker role, config/celery.py, and the celery dependency (and Sentry's
celery extra) are gone. The deploy workflow removes the old worker
container, which Kamal would otherwise leave running. Redis stays: it's the
cache (sessions, rate limits, the jobs' locks) and the droplet's shared
Redis accessory. Its URL keeps the CELERY_BROKER_URL name so the existing
secret still works.

vTodo task #686.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.