Skip to content
lorebow

Admin processes

Run one-off tasks like migrations as separate processes, from the same release and with the same config as the app.

KFounder and editor 162 words 1 min read Find it on the map
On this page

Run admin and management tasks as one-off processes. Apps need occasional jobs that aren't part of serving requests: migrating the database schema, fixing a bad record, opening a console to look around.

Twelve-factor's rule is that these run exactly like the app does: from the same release, with the same config and dependencies, against the same backing services. Only the command is different.

A migration during a deploypython manage.py migrateStep 1 of 3
Releasev43migrateone-off processweb × 3long-runningDatabaseDATABASE_URL

1.Start it from the release

The platform starts one extra process from release v43, with the same image and environment as the web processes.

#Keep the scripts in the repo

An admin task is code, so it goes in the codebase and ships with each release. Then the script always matches the code it runs against.

scripts/reset_visits.pypython
import os
import redis
 
store = redis.Redis.from_url(os.environ["REDIS_URL"])
store.set("visits", 0)
print("visits reset")

#Run it the way the app runs

One-off processes on different platforms
# Locally, in the app's virtual environment
$ python scripts/reset_visits.py
visits reset
# In a container, from the same image as the app
$ docker compose run --rm web python scripts/reset_visits.py
visits reset
# In Kubernetes, inside a running pod
$ kubectl exec deploy/shop -- python scripts/reset_visits.py
visits reset

What to avoid: logging into a production server and running a script you keep on your laptop, against packages and settings that may not match the app's.

Referenced by