fix: stop the cron scripts from rebuilding the virtual environment #195

Merged
Michiel-Beijen merged 1 commit from Michiel-Beijen/intranet:fix/cron-use-existing-venv into main 2026-11-15 16:39:10 +01:00

The deployment of #192 failed on a permission error in the shared uv cache. The cause is that several accounts each build the same .venv, and the cron scripts are one of them.

The hourly, nightly, weekly and monthly scripts ran uv venv --allow-existing and uv run. Under the account that runs cron this:

  • re-points .venv/bin/python at a Python in that user's home directory, every hour;
  • can re-sync the environment when it differs from the lockfile;
  • leaves entries in the shared uv cache that the deploy user cannot write to.

Change

The five scripts now only activate the environment that rollout-prod.sh built and run python manage.py, exactly as scripts/cron-5minutely.sh already did. They no longer call uv, so they read the environment and write nothing, whichever user runs them.

Files: cron-midnight.sh, cron-weekly.sh, cron-pre-monthly.sh, cron-monthly.sh, scripts/cron-hourly.sh.

Behaviour change

A cron job can no longer repair a missing or outdated environment. The deployment is now the only place that builds it. Each script still starts with a manage.py version check and stops if the environment is broken.

Testing

  • scripts/cron-hourly.sh run locally against a migrated database: exits 0 and leaves .venv untouched.
  • Without a .venv the script stops with exit 1 and does not create one.
  • The other four scripts have the identical two-line change; only a syntax check was run on them.

Not in this PR

  • deploy/crm-gunicorn.service and deploy/crm-daphne.service still start with uv run; they have to be installed on the server by hand, so that is a separate change.
  • scripts/check-users-in.sh, scripts/check-users-out.sh and scripts/generate-pettycash-transactions.sh also use uv run, but are not in the server's crontab.
The deployment of #192 failed on a permission error in the shared uv cache. The cause is that several accounts each build the same `.venv`, and the cron scripts are one of them. The hourly, nightly, weekly and monthly scripts ran `uv venv --allow-existing` and `uv run`. Under the account that runs cron this: - re-points `.venv/bin/python` at a Python in that user's home directory, every hour; - can re-sync the environment when it differs from the lockfile; - leaves entries in the shared uv cache that the deploy user cannot write to. ## Change The five scripts now only activate the environment that `rollout-prod.sh` built and run `python manage.py`, exactly as `scripts/cron-5minutely.sh` already did. They no longer call uv, so they read the environment and write nothing, whichever user runs them. Files: `cron-midnight.sh`, `cron-weekly.sh`, `cron-pre-monthly.sh`, `cron-monthly.sh`, `scripts/cron-hourly.sh`. ## Behaviour change A cron job can no longer repair a missing or outdated environment. The deployment is now the only place that builds it. Each script still starts with a `manage.py version` check and stops if the environment is broken. ## Testing - `scripts/cron-hourly.sh` run locally against a migrated database: exits 0 and leaves `.venv` untouched. - Without a `.venv` the script stops with exit 1 and does not create one. - The other four scripts have the identical two-line change; only a syntax check was run on them. ## Not in this PR - `deploy/crm-gunicorn.service` and `deploy/crm-daphne.service` still start with `uv run`; they have to be installed on the server by hand, so that is a separate change. - `scripts/check-users-in.sh`, `scripts/check-users-out.sh` and `scripts/generate-pettycash-transactions.sh` also use `uv run`, but are not in the server's crontab.
fix: stop the cron scripts from rebuilding the virtual environment
All checks were successful
Verify Pull Request / tests (pull_request) Successful in 2m6s
efb1632301
The hourly, nightly, weekly and monthly cron scripts ran
`uv venv --allow-existing` and `uv run`. Under the account that runs
cron, this re-pointed the shared `.venv` at a Python in that user's
home directory and left entries in the shared uv cache that the deploy
user could not write to, which made a deployment fail.

The scripts now only activate the environment the deployment built,
as the 5-minute script already did.
Michiel-Beijen deleted branch fix/cron-use-existing-venv 2026-11-15 16:39:11 +01:00
Sign in to join this conversation.
No reviewers
No labels
Draft
No milestone
No project
No assignees
1 participant
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
msl/intranet!195
No description provided.