Skip to content

Commit 1045513

Browse files
vanessavmacmihow
andauthored
Set up RabbitMQ as Celery broker (#1041)
* feat: rabbitmq as celery broker, default antenna queue * feat: separate CI stack with rabbitmq * fix: broker url trailing slash, database url for CI stack * Add missing CI changes * chore: share env vars with django app * feat: move celery & debugbpy start command out of compose files * feat: update production compose file for rabbitmq & default queue * chore: explain redundant DATABASE_URL env var * feat: use docker override file to activate debuggers * chore: tell agent docs we are using RabbitMQ instead of Redis now --------- Co-authored-by: Michael Bunsen <[email protected]>
1 parent ba88814 commit 1045513

File tree

11 files changed

+74
-8
lines changed

11 files changed

+74
-8
lines changed

.agents/AGENTS.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ Every call to the AI model API incurs a cost and requires electricity. Be smart
1313
- Always prefer command line tools to avoid expensive API requests (e.g., use git and jq instead of reading whole files)
1414
- Use bulk operations and prefetch patterns to minimize database queries
1515

16+
**Performance Optimization:**
17+
- django-cachalot handles automatic query caching - don't add manual caching layers on top
18+
- Focus on optimizing cold queries first before adding caching
19+
- When ordering by annotated fields, pagination COUNT queries include those annotations - use `.values('pk')` to strip them
20+
- For large tables (>10k rows), consider fuzzy counting using PostgreSQL's pg_class.reltuples
21+
22+
**Git Commit Guidelines:**
23+
- Do NOT include "Generated with Claude Code" in commit messages
24+
- ALWAYS include "Co-Authored-By: Claude <[email protected]>" at the end of commit messages
25+
1626
## Project Overview
1727

1828
Antenna is an Automated Monitoring of Insects ML Platform. It's a collaborative platform for processing and reviewing images from automated insect monitoring stations, maintaining metadata, and orchestrating multiple machine learning pipelines for analysis.
@@ -273,7 +283,7 @@ Location: `processing_services/` directory contains example implementations
273283

274284
### Celery Task Queue
275285

276-
**Broker & Result Backend:** Redis
286+
**Broker & Result Backend:** RabbitMQ
277287

278288
**Key Tasks:**
279289
- `ami.jobs.tasks.run_job` - Main ML processing workflow

.envs/.ci/.django

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ MINIO_BROWSER_REDIRECT_URL=http://minio:9001
1919

2020
DEFAULT_PROCESSING_SERVICE_NAME=Test Processing Service
2121
DEFAULT_PROCESSING_SERVICE_ENDPOINT=http://ml_backend:2000
22+
23+
# RabbitMQ
24+
# ------------------------------------------------------------------------------
25+
CELERY_BROKER_URL=amqp://rabbituser:rabbitpass@rabbitmq:5672/
26+
RABBITMQ_DEFAULT_USER=rabbituser
27+
RABBITMQ_DEFAULT_PASS=rabbitpass

.envs/.ci/.postgres

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ POSTGRES_PORT=5432
33
POSTGRES_DB=ami-ci
44
POSTGRES_USER=4JXkOnTAeDmDyIapSRrGEE
55
POSTGRES_PASSWORD=d4xojpnJU3OzPQ0apSCLP1oHR1TYvyMzAlF5KpE9HFL6MPlnbDibwI
6+
# DATABASE_URL is for optional convenience with certain tools (e.g., psql), not used by Django
7+
DATABASE_URL=postgresql://4JXkOnTAeDmDyIapSRrGEE:d4xojpnJU3OzPQ0apSCLP1oHR1TYvyMzAlF5KpE9HFL6MPlnbDibwI@postgres:5432/ami-ci

.envs/.local/.django

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ REDIS_URL=redis://redis:6379/0
1616
CELERY_FLOWER_USER=QSocnxapfMvzLqJXSsXtnEZqRkBtsmKT
1717
CELERY_FLOWER_PASSWORD=BEQgmCtgyrFieKNoGTsux9YIye0I7P5Q7vEgfJD2C4jxmtHDetFaE2jhS7K7rxaf
1818

19+
# RabbitMQ
20+
CELERY_BROKER_URL=amqp://rabbituser:rabbitpass@rabbitmq:5672/
21+
RABBITMQ_DEFAULT_USER=rabbituser
22+
RABBITMQ_DEFAULT_PASS=rabbitpass
23+
1924
# Attempting to keep Flower from showing workers as offline
2025
# FLOWER_BROKER_API=REDIS_URL
2126
FLOWER_PERSISTENT=True

.envs/.local/.postgres

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ POSTGRES_PORT=5432
33
POSTGRES_DB=ami
44
POSTGRES_USER=xekSryPnqczJXkOnTAeDmDyIapSRrGEE
55
POSTGRES_PASSWORD=iMRQjJEGflj5xojpnJU3OzPQ0apSCLP1oHR1TYvyMzAlF5KpE9HFL6MPlnbDibwI
6+
# DATABASE_URL is for optional convenience with certain tools (e.g., psql), not used by Django
7+
DATABASE_URL=postgres://xekSryPnqczJXkOnTAeDmDyIapSRrGEE:iMRQjJEGflj5xojpnJU3OzPQ0apSCLP1oHR1TYvyMzAlF5KpE9HFL6MPlnbDibwI@postgres:5432/ami

compose/local/django/celery/worker/start

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -o nounset
88
# Note that auto reloading is disabled when debugging, manual restart required for code changes.
99
if [ "${DEBUGGER:-0}" = "1" ]; then
1010
# exec watchfiles --filter python 'python -m debugpy --listen 0.0.0.0:5679 -m celery -A config.celery_app worker -l INFO'
11-
exec python -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5679 -m celery -A config.celery_app worker -l INFO
11+
exec python -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5679 -m celery -A config.celery_app worker --queues=antenna -l INFO
1212
else
13-
exec watchfiles --filter python celery.__main__.main --args '-A config.celery_app worker -l INFO'
13+
exec watchfiles --filter python celery.__main__.main --args '-A config.celery_app worker --queues=antenna -l INFO'
1414
fi

compose/production/django/celery/worker/start

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ set -o errexit
44
set -o pipefail
55
set -o nounset
66

7-
exec newrelic-admin run-program celery -A config.celery_app worker -l INFO
7+
exec newrelic-admin run-program celery -A config.celery_app worker --queues=antenna -l INFO

config/settings/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
},
261261
}
262262
}
263+
REDIS_URL = env("REDIS_URL", default=None)
263264

264265
# ADMIN
265266
# ------------------------------------------------------------------------------
@@ -299,6 +300,8 @@
299300
if USE_TZ:
300301
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#std:setting-timezone
301302
CELERY_TIMEZONE = TIME_ZONE
303+
304+
CELERY_TASK_DEFAULT_QUEUE = "antenna"
302305
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#std:setting-broker_url
303306
CELERY_BROKER_URL = env("CELERY_BROKER_URL")
304307
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#std:setting-result_backend

docker-compose.ci.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
# Ensure this launches a separate stack for CI testing to avoid conflicts with local dev setup:
2+
name: antenna-ci
3+
4+
volumes:
5+
ami_ci_postgres_data:
6+
driver: local
7+
minio_ci_data:
8+
driver: local
9+
110
services:
211
django: &django
312
build:
413
context: .
514
dockerfile: ./compose/local/django/Dockerfile
6-
image: ami_ci_django
15+
volumes:
16+
- .:/app:z
17+
extra_hosts:
18+
- "host.docker.internal:host-gateway"
719
depends_on:
820
- postgres
921
- redis
1022
- minio-init
1123
- ml_backend
24+
- rabbitmq
1225
env_file:
1326
- ./.envs/.ci/.django
1427
- ./.envs/.ci/.postgres
@@ -18,6 +31,8 @@ services:
1831
build:
1932
context: .
2033
dockerfile: ./compose/local/postgres/Dockerfile
34+
volumes:
35+
- ami_ci_postgres_data:/var/lib/postgresql/data
2136
env_file:
2237
- ./.envs/.ci/.postgres
2338

@@ -26,12 +41,20 @@ services:
2641

2742
celeryworker:
2843
<<: *django
29-
image: ami_ci_celeryworker
44+
depends_on:
45+
- rabbitmq
3046
command: /start-celeryworker
3147

48+
rabbitmq:
49+
image: rabbitmq:3-management
50+
env_file:
51+
- ./.envs/.ci/.django
52+
3253
minio:
3354
image: minio/minio:RELEASE.2024-11-07T00-52-20Z
3455
command: minio server --console-address ":9001" /data
56+
volumes:
57+
- "minio_ci_data:/data"
3558
env_file:
3659
- ./.envs/.ci/.django
3760
healthcheck:
@@ -45,7 +68,8 @@ services:
4568
env_file:
4669
- ./.envs/.ci/.django
4770
depends_on:
48-
- minio
71+
minio:
72+
condition: service_healthy
4973
volumes:
5074
- ./compose/local/minio/init.sh:/etc/minio/init.sh
5175
entrypoint: /etc/minio/init.sh

docker-compose.production.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ services:
1616
- ./config:/app/config
1717
ports:
1818
- "5001:5000"
19-
extra_hosts:
19+
extra_hosts: # hostname aliases to the IPs of persistant services running outside docker network
2020
- "db:${DATABASE_IP}"
21+
- "rabbitmq:${RABBITMQ_IP}"
2122
- "redis:${REDIS_IP}"
2223
command: /start
2324
scale: 1 # Can't scale until the load balancer is within the compose config

0 commit comments

Comments
 (0)