Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#48] Add Celery and Celery beat options #51

Merged
merged 5 commits into from
Sep 23, 2024
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cookiecutter https://github.com/nickatnight/cookiecutter-fastapi-backend.git
* :lock: **Let's Encrypt** A free, automated, and open [certificate authority](https://letsencrypt.org/) (CA), provided by the Internet Security Research Group (ISRG)...with automatic cert renewal
* :floppy_disk: **postgresql** Powerful open source [object-relational](https://www.postgresql.org/) database
* :convenience_store: **Redis** In-memory data structure [store](https://redis.io/), used as a distributed, in-memory key–value database, cache and message broker
* :seedling: **Celery** [Asynchronous](https://docs.celeryq.dev/en/stable/getting-started/introduction.html) task or job queue
* :inbox_tray: **Continuous Integration/Deployment** Modular [GitHub Actions](https://github.com/features/actions) to lint, build, test, and deploy to DigitalOcean cloud
* :leftwards_arrow_with_hook: **pre-commit** [Git hooks](https://pre-commit.com/) to maintain code quality using modern tooling (ruff, black, isort)

Expand All @@ -46,6 +47,7 @@ The input variables, with their default values (some auto generated) are:
* `py_version`: The version of Python to install. Options are `3.9`, `3.10`, and `3.11`
* `db_container_name`: The name of the database container. Default `db`
* `backend_container_name`: The name of the backend container. Default `backend`
* `use_celery`: Whether to use Celery/Beat for asynchronous/scheduled tasks.
* `nginx_container_name`: The name of the nginx web server container. Default `nginx`
* `doctl_version`: The version name of [DigitalOcean Command Line Interface](https://docs.digitalocean.com/reference/doctl/) to use. Default `1.92.0`
* `github_username`: The username of the GitHub user. Used for badge display in generated project `README.md`
Expand Down
1 change: 1 addition & 0 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

"db_container_name": "db",
"backend_container_name": "backend",
"use_celery": ["no", "yes"],
"nginx_container_name": "nginx",

"doctl_version": "1.92.0",
Expand Down
12 changes: 6 additions & 6 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)
BASE_BACKEND_SRC_PATH = "{{ cookiecutter.backend_container_name }}/src/"
MEME_FILE_PATHS = [
"%smodels/meme.py" % BASE_BACKEND_SRC_PATH,
"%sschemas/meme.py" % BASE_BACKEND_SRC_PATH,
"%sapi/v1/meme.py" % BASE_BACKEND_SRC_PATH,
"%srepositories/meme.py" % BASE_BACKEND_SRC_PATH,
"%sdb/init_db.py" % BASE_BACKEND_SRC_PATH,
CELERY_FILE_PATHS = [
"%sworker.py" % BASE_BACKEND_SRC_PATH,
]
DEPLOYMENT_FILES = [
"ops/docker-compose.prod.yml",
Expand All @@ -31,5 +27,9 @@ def rename_file(old: str, new: str):
for p in DEPLOYMENT_FILES:
remove_file(p)

if "{{ cookiecutter.use_celery }}" == "no":
print("Removing celery files...")
for p in CELERY_FILE_PATHS:
remove_file(p)

rename_file(".env_example", ".env")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cookiecutter-fastapi-backend"
version = "0.3.0"
version = "0.4.0"
description = "Cookiecutter template to build and deploy fastapi backends..batteries included"
authors = ["nickatnight <nickkelly.858@gmail.com>"]
readme = "README.md"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_bake_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
SUPPORTED_COMBINATIONS = [
{"deployments": "no"},
{"deployments": "yes"},
{"use_celery": "no"},
{"use_celery": "yes"},
{"py_version": "3.9"},
{"py_version": "3.10"},
{"py_version": "3.11"},
Expand Down
21 changes: 21 additions & 0 deletions {{ cookiecutter.project_slug }}/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,24 @@ services:
- "6479:6379"
volumes:
- redis-data:/data
{%- if cookiecutter.use_celery == "yes" %}
celery:
restart: always
build: ./{{ cookiecutter.backend_container_name }}
command: celery --app=src worker --loglevel=info
env_file:
- .env
depends_on:
- {{ cookiecutter.backend_container_name }}
- redis

beat:
restart: always
build: ./{{ cookiecutter.backend_container_name }}
command: celery --app=src beat --loglevel=info
env_file:
- .env
depends_on:
- {{ cookiecutter.backend_container_name }}
- redis
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:{{ cookiecutter.py_version}} as base
FROM python:{{ cookiecutter.py_version}} AS base

ARG env

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ PyYAML = "6.0.1"
httpx = "0.25.0"
gunicorn = "23.0.0"
openai = "1.44.0"
{%- if cookiecutter.use_celery == "yes" %}celery = {extras = ["beat"], version = "5.2.7"}{%- endif %}

[tool.poetry.dev-dependencies]
black = "24.8.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{%- if cookiecutter.use_celery == "yes" %}from .celery import app as celery_app


__all__ = ("celery_app",){%- endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from celery import Celery


celery_app = Celery(__name__)
# add more tasks roots here
celery_app.autodiscover_tasks(["src.core.tasks"], related_name="tasks", force=True)
Loading