|
| 1 | +# Django Integration |
| 2 | + |
| 3 | +## Settings |
| 4 | + |
| 5 | +When running `queueio run`, |
| 6 | +Django isn't started automatically. |
| 7 | +If your routines use the Django ORM or other Django features, |
| 8 | +queueio needs to call `django.setup()` before importing routine modules. |
| 9 | + |
| 10 | +The `queueio[django]` extra includes |
| 11 | +[django-cmd](https://pypi.org/project/django-cmd/), |
| 12 | +which reads `tool.django.settings` from `pyproject.toml` |
| 13 | +and sets `DJANGO_SETTINGS_MODULE` automatically: |
| 14 | + |
| 15 | +```toml |
| 16 | +[tool.django] |
| 17 | +settings = "myproject.settings" |
| 18 | +``` |
| 19 | + |
| 20 | +If `DJANGO_SETTINGS_MODULE` is set in the environment |
| 21 | +(whether by `django-cmd` or directly), |
| 22 | +queueio calls `django.setup()` before registering routines. |
| 23 | + |
| 24 | +## Runserver |
| 25 | + |
| 26 | +!!! warning "Development server only" |
| 27 | + |
| 28 | + The runserver integration only affects the `runserver` management command. |
| 29 | + Production servers like gunicorn need their own lifecycle hooks. |
| 30 | + |
| 31 | +queueio ships a Django app |
| 32 | +that overrides the `runserver` management command |
| 33 | +to wrap the server in `with activate():`, |
| 34 | +giving you automatic lifecycle management |
| 35 | +of the broker connection and background threads |
| 36 | +during development. |
| 37 | + |
| 38 | +### Setup |
| 39 | + |
| 40 | +If you use [djp](https://djp.readthedocs.io/), |
| 41 | +`queueio.django` is added to `INSTALLED_APPS` automatically. |
| 42 | + |
| 43 | +Otherwise, add it manually: |
| 44 | + |
| 45 | +```python |
| 46 | +INSTALLED_APPS = [ |
| 47 | + "queueio.django", |
| 48 | + # ... |
| 49 | +] |
| 50 | +``` |
| 51 | + |
| 52 | +This must come **after** whichever app provides the `runserver` command |
| 53 | +you want to chain with |
| 54 | +(e.g. `daphne`, `django.contrib.staticfiles`), |
| 55 | +because Django resolves management commands by last-app-wins order. |
| 56 | + |
| 57 | +### Custom provider |
| 58 | + |
| 59 | +If you use a custom `runserver` provider |
| 60 | +like `django.contrib.staticfiles` or `daphne`, |
| 61 | +tell queueio which app to chain with: |
| 62 | + |
| 63 | +```toml |
| 64 | +[tool.queueio.django] |
| 65 | +runserver = "daphne" |
| 66 | +``` |
| 67 | + |
| 68 | +The value is the app label (or dotted module path). |
| 69 | +queueio resolves it to `{value}.management.commands.runserver` |
| 70 | +and subclasses that command. |
| 71 | +If omitted, it defaults to Django's built-in `runserver`. |
| 72 | + |
| 73 | +### `runserver_plus` |
| 74 | + |
| 75 | +There is also a built-in `runserver_plus` command |
| 76 | +that wraps [django-extensions](https://django-extensions.readthedocs.io/)' `runserver_plus` |
| 77 | +with `activate()`. |
| 78 | +This is available automatically |
| 79 | +when `django-extensions` is installed |
| 80 | +and `queueio.django` is in `INSTALLED_APPS`. |
| 81 | + |
| 82 | +A complete working example is in `queueio/samples/django/`. |
0 commit comments