Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .claude/skills/new-service/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ The script refuses invalid names, existing members and unknown contracts. It wri
| `src/<pkg>/worker.py` (worker) | `handle` / `process_one` / `run` consume loop |
| `tests/test_<pkg>.py` | starter tests (health check / consume loop) |
| root `pyproject.toml` | adds the import name to ruff `known-first-party` and `apps/<name>/src` to `mypy_path` |
| `compose.yaml` | service entry; host port = highest used + 1; `BROKER_URL` + redis dependency when needed |
| `compose.yaml` | service entry; host port = highest used + 1; broker services get the shared `*broker-env` / `*broker-depends` anchors, so they follow whichever broker is selected |
| `pyproject.toml` + `Dockerfile` (broker services) | `redis` / `rabbitmq` extras and the `BROKER_EXTRAS` build arg, so images can carry any broker client |

CI (`scripts/ci/affected.py`) and the workspace glob pick up the new member automatically.
Nothing to edit there.
Expand Down
48 changes: 37 additions & 11 deletions .claude/skills/new-service/scripts/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
description = "__DESCRIPTION__"
requires-python = ">=3.13"
__DEPS_COMMENT__dependencies = [__DEPS__]

__BROKER_EXTRAS_TOML__
[project.scripts]
__NAME__ = "__PKG__.main:main"

Expand All @@ -55,13 +55,13 @@
# Build from the repo root so the workspace lockfile and sibling packages are in context:
# docker build -f apps/__NAME__/Dockerfile -t __NAME__ .
# Only this app and the workspace packages it depends on are installed.

__DOCKER_BROKER_DOC__
ARG PYTHON_VERSION=3.13
ARG APP=__NAME__

__DOCKER_BROKER_ARG__
FROM ghcr.io/astral-sh/uv:python${PYTHON_VERSION}-bookworm-slim AS builder
ARG APP
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy UV_PYTHON_DOWNLOADS=0
__DOCKER_BROKER_ARG_STAGE__ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy UV_PYTHON_DOWNLOADS=0
WORKDIR /app

# 1) Third-party deps only (cached layer, rebuilt only when the lock changes).
Expand All @@ -70,12 +70,12 @@
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \\
--mount=type=bind,source=packages,target=packages \\
--mount=type=bind,source=apps,target=apps \\
uv sync --frozen --no-dev --no-install-workspace --package ${APP}
uv sync --frozen --no-dev --no-install-workspace --package ${APP}__DOCKER_EXTRA_FLAGS__

# 2) This app + its workspace deps, installed as real (non-editable) wheels.
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv \\
uv sync --frozen --no-dev --no-editable --package ${APP}
uv sync --frozen --no-dev --no-editable --package ${APP}__DOCKER_EXTRA_FLAGS__

FROM python:${PYTHON_VERSION}-slim-bookworm AS runtime
ARG APP
Expand Down Expand Up @@ -191,6 +191,7 @@ def test_health(client: TestClient) -> None:

class Settings(ServiceSettings):
service_name: str = "__NAME__"
# The scheme picks the transport: redis://, amqp://, tcp:// or memory://.
broker_url: str = "redis://localhost:6379/0"


Expand All @@ -204,7 +205,8 @@ def handle_signal(signum: int, frame: FrameType | None) -> None:

signal.signal(signal.SIGTERM, handle_signal)
signal.signal(signal.SIGINT, handle_signal)
run(broker_from_url(settings.broker_url), stop)
with broker_from_url(settings.broker_url) as broker:
run(broker, stop)


if __name__ == "__main__":
Expand Down Expand Up @@ -248,6 +250,7 @@ def run(broker: Broker, stop: threading.Event, handler: Handler = handle) -> Non
process_one(broker, handler)
except Exception: # keep the worker alive; a real one would dead-letter the event
log.exception("failed to process event")
stop.wait(1.0) # don't spin while the broker is unreachable
log.info("stopped")
'''

Expand Down Expand Up @@ -289,11 +292,25 @@ def runner() -> None:
ports: ["__HOST_PORT__:8000"]
"""

# Uses the anchors defined at the top of compose.yaml, so the service follows whichever
# broker the env file selects (BROKER_URL + COMPOSE_PROFILES).
COMPOSE_BROKER = """\
environment:
BROKER_URL: redis://redis:6379/0
depends_on:
redis: { condition: service_healthy }
environment: *broker-env
depends_on: *broker-depends
"""

BROKER_EXTRAS_TOML = """
# Broker client libraries, picked at image build time (Dockerfile ARG BROKER_EXTRAS).
# The code only uses the `messaging.Broker` interface; BROKER_URL picks the backend.
[project.optional-dependencies]
redis = ["messaging[redis]"]
rabbitmq = ["messaging[rabbitmq]"]
"""

DOCKER_BROKER_DOC = """#
# BROKER_EXTRAS chooses which broker client libraries go into the image (default: all,
# so BROKER_URL alone picks redis://, amqp:// or tcp://). memory:// and tcp:// need none.
# docker build --build-arg BROKER_EXTRAS=rabbitmq -f apps/__NAME__/Dockerfile -t __NAME__ .
"""

COMPOSE_WORKER = """
Expand Down Expand Up @@ -420,6 +437,15 @@ def main() -> int:
else ""
),
"SOURCES": sources,
"BROKER_EXTRAS_TOML": BROKER_EXTRAS_TOML if uses_broker else "",
"DOCKER_BROKER_DOC": DOCKER_BROKER_DOC.replace("__NAME__", name) if uses_broker else "",
"DOCKER_BROKER_ARG": 'ARG BROKER_EXTRAS="redis rabbitmq"\n' if uses_broker else "",
"DOCKER_BROKER_ARG_STAGE": "ARG BROKER_EXTRAS\n" if uses_broker else "",
"DOCKER_EXTRA_FLAGS": (
' $(for e in ${BROKER_EXTRAS}; do printf -- "--extra %s " "$e"; done)'
if uses_broker
else ""
),
"RUNTIME_EXTRA": "" if is_worker else HTTP_RUNTIME_EXTRA,
"EVENT": args.event or "",
"TOPIC": args.topic or "",
Expand Down
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Default settings for `docker compose` (read automatically from this folder).
# Committed on purpose: it holds no secrets, only which broker the local stack uses.
# To switch broker, pass another file instead: docker compose --env-file brokers/rabbitmq.env up
COMPOSE_PROFILES=redis
BROKER_URL=redis://redis:6379/0
49 changes: 48 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,59 @@ jobs:
# Exit code 5 means "no tests collected"; don't fail a package for that.
uv run pytest "$PKG_PATH" || [ $? -eq 5 ]

brokers:
# The contract tests in packages/messaging, run against real Redis and RabbitMQ
# servers (the unit test job covers memory, fakeredis and TCP only).
name: Broker integration (Redis + RabbitMQ)
needs: changes
if: contains(needs.changes.outputs.packages, '"messaging"')
runs-on: ubuntu-latest
services:
redis:
image: redis:7-alpine
ports: ["6379:6379"]
options: >-
--health-cmd "redis-cli ping" --health-interval 5s --health-retries 10
rabbitmq:
image: rabbitmq:4-alpine
ports: ["5672:5672"]
options: >-
--health-cmd "rabbitmq-diagnostics -q ping" --health-interval 5s
--health-timeout 10s --health-retries 20
steps:
- uses: actions/checkout@v5
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
cache-dependency-glob: uv.lock
- run: uv sync --all-packages
- name: pytest packages/messaging (real brokers)
env:
MESSAGING_TEST_REDIS_URL: redis://localhost:6379/0
MESSAGING_TEST_AMQP_URL: amqp://guest:guest@localhost:5672/
run: uv run pytest packages/messaging

compose:
# Build the real images and send an order from orders-api to notifier-worker
# through each broker, exactly as `docker compose --env-file brokers/<x>.env up` does.
name: End-to-end over ${{ matrix.broker }}
needs: changes
if: contains(needs.changes.outputs.packages, '"notifier-worker"')
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
broker: [redis, rabbitmq, tcp]
steps:
- uses: actions/checkout@v5
- run: scripts/ci/compose_smoke.sh "${{ matrix.broker }}"

ci-ok:
# Single required check for branch protection; passes when tests were
# skipped because nothing relevant changed.
name: CI OK
if: always()
needs: [changes, lint, test]
needs: [changes, lint, test, brokers, compose]
runs-on: ubuntu-latest
steps:
- name: Check results
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ htmlcov/

# Env
.env
# ...except the root one, which only holds non-secret compose defaults.
!/.env
Loading
Loading