Valkey, tasks, and scheduled jobs¶
Valkey sidecar¶
add_django_website(..., valkey_sidecar=True) (default) attaches a Valkey container (mcr.microsoft.com/azurelinux/base/valkey:8) to the Web App and sets VALKEY_SIDECAR=true. Valkey is Redis-protocol compatible, so the cache and RQ keep using redis://localhost:6379.
Django settings then configure django_redis against redis://localhost:6379/1 for the cache, with exceptions ignored so the site can start if Valkey is briefly unavailable.
Cache and queue live in separate databases¶
The cache and the RQ queue (and therefore RQ's cron registry) deliberately use different databases of the same Valkey instance:
| database | used by |
|---|---|
1 |
Django cache (CACHES["default"]): {% cache %} fragments, Wagtail renditions |
0 |
RQ queue, django-tasks backend, RQ cron registry (reached through CACHES["rq"], which is only a connection descriptor for RQ_QUEUES) |
purge_cache and cache.clear() issue a FLUSHDB on the cache's database, so clearing the cache cannot wipe queued jobs or the cron registry any more. Application code and Wagtail signals use pulumi_django_azure.cache.purge_cache() for the same effect; the management command is a thin CLI wrapper over that function. Never call .clear() on the rq alias, and only use purge_cache --all-databases when you mean to reset the whole instance (that is a FLUSHALL, which does take the cron registry with it).
Running a single shared database also broke the nightly-purge pattern: a scheduled job that purged the cache with FLUSHALL deleted its own schedule.
Django tasks¶
django_tasks=True (default) requires valkey_sidecar=True and sets DJANGO_TASKS=true. Settings wire:
RQ_QUEUESusing the dedicatedrqconnection (database 0), not the cacheTASKSbackenddjango_tasks_rq.RQBackend
patch_django_settings_for_azure adds django_rq and django_tasks_rq to INSTALLED_APPS.
When DJANGO_TASKS=true, startup starts the worker through supervisorctl:
(with a long timeout so Entra credentials can refresh). The program is defined with autostart=false, so an app that disables tasks never runs it, and its output goes to the container's stdout/stderr so it appears in the App Service log stream.
Worker only accepts django.tasks Tasks
The worker uses django_tasks_rq.Job, which rejects plain callables with
SuspiciousOperation: Task … does not point to a Task. Background work must
be defined with @task / @task() and enqueued via .enqueue().
Scheduled tasks (rqcron)¶
django_rq_cron=True (default False) requires valkey_sidecar=True and sets:
DJANGO_RQ_CRON=trueRQ_CRON_CONFIGdefaulting tocron_config.py
Override the config path by passing RQ_CRON_CONFIG in environment_variables before the default is applied.
At startup, supervisord runs the RQ worker. When DJANGO_RQ_CRON=true, startup also starts the cron program (defined in cicd/supervisord.conf with autostart=false):
(with the same 6-hour timeout pattern as the RQ worker).
Registering cron jobs (required pattern)¶
Because the worker only accepts django-tasks Tasks, do not call rq.cron.register with a plain function, and do not pass a Task object directly to rq.cron.register (Task has no __name__, so config import fails).
Use pulumi_django_azure.cron.register_task with a @task-decorated callable:
# cron_config.py
from django.tasks import task
from pulumi_django_azure.cron import register_task
@task()
def send_report() -> None:
...
register_task(send_report, queue_name="default", cron="0 9 * * *")
Or register a Task defined elsewhere:
# cron_config.py
from myapp.tasks import send_report
from pulumi_django_azure.cron import register_task
register_task(send_report, queue_name="default", cron="0 9 * * *")
Use queue_name="default" so the existing supervisord RQ worker picks up scheduled jobs.
Cron expressions follow the scheduler process timezone (RQ 3). App Service containers default to UTC, so cron="0 9 * * *" is 09:00 UTC in production when TZ is unset. On a machine with another zone, the same string is 09:00 local time. Set the TZ App Setting (for example TZ=UTC) to lock the zone. interval schedules count seconds from the last enqueue.
Why a helper?
register_task adapts the Task for RQ cron registration and enqueues via
task.enqueue() so jobs carry the metadata django_tasks_rq.Job expects.
Plain rq.cron.register(func) and rq.cron.register(task) are both broken
under the default django_tasks=True worker.
Enable in Pulumi:
Custom config path:
django.add_django_website(
# ...
environment_variables={"RQ_CRON_CONFIG": "myapp/cron_config.py"},
django_rq_cron=True,
)
Setting django_rq_cron=True with valkey_sidecar=False raises ValueError.
Disabling¶
Setting django_tasks=True with valkey_sidecar=False raises ValueError.