diff --git a/backend/Dockerfile b/backend/Dockerfile index 18c7e478..a92b92b4 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -42,6 +42,10 @@ COPY backend/app ./app # In-repo these live at `backend/templates/`; runtime path is `/app/templates`. COPY backend/templates ./templates +# Copy worker scripts. +# In-repo these live at `scripts/`; runtime path is `/app/scripts`. +COPY scripts ./scripts + # Default API port EXPOSE 8000 diff --git a/compose.yml b/compose.yml index e852dc49..d55a4428 100644 --- a/compose.yml +++ b/compose.yml @@ -75,7 +75,7 @@ services: build: context: . dockerfile: backend/Dockerfile - command: ["rq", "worker", "--with-scheduler", "-u", "redis://redis:6379/0"] + command: ["python", "scripts/rq-docker", "worker"] env_file: - ./backend/.env depends_on: diff --git a/scripts/rq-docker b/scripts/rq-docker new file mode 100644 index 00000000..9ec53742 --- /dev/null +++ b/scripts/rq-docker @@ -0,0 +1,46 @@ +#!/usr/bin/env python +"""RQ worker entrypoint for Docker containers.""" + +from __future__ import annotations + +import argparse +import sys +from pathlib import Path + +# In Docker, the working directory is /app and app code is at /app/app/ +# Add /app to sys.path so we can import app.services.queue_worker +WORKDIR = Path.cwd() +sys.path.insert(0, str(WORKDIR)) + +from app.services.queue_worker import run_worker + + +def cmd_worker(args: argparse.Namespace) -> int: + try: + run_worker() + except KeyboardInterrupt: + return 0 + return 0 + + +def build_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description="RQ background worker helpers.") + subparsers = parser.add_subparsers(dest="command", required=True) + + worker_parser = subparsers.add_parser( + "worker", + help="Continuously process queued background work.", + ) + worker_parser.set_defaults(func=cmd_worker) + + return parser + + +def main() -> None: + parser = build_parser() + args = parser.parse_args() + sys.exit(args.func(args)) + + +if __name__ == "__main__": + main()