From 69f02096daab93a06a7a13ff489022ad4cb47f39 Mon Sep 17 00:00:00 2001 From: Pavel Lopatin Date: Tue, 11 Feb 2025 20:43:52 +0300 Subject: [PATCH] init django-rq --- .dockerignore | 2 +- .env.example | 10 +++- README.md | 13 ++++- docker-compose.yaml | 56 +++++++++++++------ Dockerfile => docker/Dockerfile | 10 ++-- docker/DockerfileWorker | 19 +++++++ nginx/nginx.conf | 24 +++++--- .../management/commands/run_task.py | 13 +++++ src/application/routers/__init__.py | 3 - src/application/routers/api.py | 14 ----- src/application/tasks/__init__.py | 0 src/application/tasks/example.py | 6 ++ src/core/asgi.py | 16 ------ src/core/settings.py | 17 +++++- src/core/urls.py | 3 +- src/requirements.txt | 6 +- src/supervisord.ini | 16 ++++++ 17 files changed, 159 insertions(+), 69 deletions(-) rename Dockerfile => docker/Dockerfile (63%) create mode 100644 docker/DockerfileWorker create mode 100644 src/application/management/commands/run_task.py delete mode 100644 src/application/routers/__init__.py delete mode 100644 src/application/routers/api.py create mode 100644 src/application/tasks/__init__.py create mode 100644 src/application/tasks/example.py create mode 100644 src/supervisord.ini diff --git a/.dockerignore b/.dockerignore index 5f752b1..9955ee3 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,7 +11,7 @@ # Docker docker-compose.yml -Dockerfile +docker/Dockerfile .docker .dockerignore diff --git a/.env.example b/.env.example index d6dec62..41777d0 100644 --- a/.env.example +++ b/.env.example @@ -11,4 +11,12 @@ DB_PORT=5432 REDIS_HOST=redis REDIS_PORT=6379 -REDIS_PASSWORD=redis \ No newline at end of file +REDIS_PASSWORD=redis + +BROKER_HOST=redis +BROKER_PORT=6379 +BROKER_PASSWORD=redis +BROKER_DB=1 + +AUTH_HTTP_USER=admin +AUTH_HTTP_PASSWORD=adminpass \ No newline at end of file diff --git a/README.md b/README.md index 397c8a4..2bb77a4 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,26 @@ -# simpliest django(uvicorn)+postgresql+fastapi+redis+nginx docker-compose (ready for production and dev) +# simplest Django and django-rq + ## How to use + To run: `docker-compose up -d` Site available on 8000 port. You can make any changes in code, they will appear automatically. If you want to execute something with manage.py use: + ```sh docker-compose exec app python3 manage.py migrate docker-compose exec app python3 manage.py makemigrations docker-compose exec app python3 manage.py update_admin admin adminpass # create superuser ``` + and so on. ## Install formatting + **Features** + - check for unsolved merge conflicts - black formatting - sort imports @@ -22,15 +28,20 @@ and so on. - flake8 verification It executes on **every** commit + ```sh pip install pre-commit flake8 black pre-commit install ``` + Apply for all files in current directory: + ```sh pre-commit run --all-files ``` + If there is PEP8 errors, commit will be forbidden. To force commit use flag --no-verify: + ```sh git commit --no-verify ... ``` \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index f53ea85..7874477 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -14,7 +14,8 @@ services: command: nginx -g "daemon off;" depends_on: - app - - api + - redis_commander + - rq_dashboard image: nginx:alpine restart: on-failure volumes: @@ -26,7 +27,7 @@ services: app: build: context: . - dockerfile: Dockerfile + dockerfile: docker/Dockerfile command: bash -c 'while ! float: + return a / b diff --git a/src/core/asgi.py b/src/core/asgi.py index 68ff548..d3f2e66 100644 --- a/src/core/asgi.py +++ b/src/core/asgi.py @@ -10,22 +10,6 @@ https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ import os from django.core.asgi import get_asgi_application -from fastapi import FastAPI -from fastapi.middleware.cors import CORSMiddleware os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") application = get_asgi_application() - -from application.routers.api import router - - -fastapp = FastAPI() - -fastapp.include_router(router) -fastapp.add_middleware( - CORSMiddleware, - allow_origins=["*"], - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) diff --git a/src/core/settings.py b/src/core/settings.py index b0bc313..32c3e14 100644 --- a/src/core/settings.py +++ b/src/core/settings.py @@ -16,6 +16,11 @@ REDIS_HOST = os.getenv("REDIS_HOST") REDIS_PORT = int(os.getenv("REDIS_PORT")) REDIS_PASSWORD = os.getenv("REDIS_PASSWORD") +BROKER_HOST = os.getenv("BROKER_HOST") +BROKER_PORT = int(os.getenv("BROKER_PORT")) +BROKER_PASSWORD = os.getenv("BROKER_PASSWORD") +BROKER_DB = os.getenv("BROKER_DB") + INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', @@ -23,7 +28,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'application.apps.ApplicationConfig' + 'django_rq', + 'application.apps.ApplicationConfig', ] MIDDLEWARE = [ @@ -98,3 +104,12 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static') DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" + +RQ_QUEUES = { + 'default': { + 'HOST': BROKER_HOST, + 'PORT': BROKER_PORT, + 'DB': BROKER_DB, + 'PASSWORD': BROKER_PASSWORD, + }, +} diff --git a/src/core/urls.py b/src/core/urls.py index 22cbb40..c6c0930 100644 --- a/src/core/urls.py +++ b/src/core/urls.py @@ -14,8 +14,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path urlpatterns = [ path('django/admin/', admin.site.urls), + path('django-rq/', include('django_rq.urls')) ] diff --git a/src/requirements.txt b/src/requirements.txt index 8bdb446..36516b2 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -1,6 +1,6 @@ uvicorn==0.27.1 -fastapi==0.109.0 Django==5.0.2 psycopg2 -redis==4.6.0 -djangoql==0.18.1 \ No newline at end of file +redis==5.2.1 +djangoql==0.18.1 +django-rq \ No newline at end of file diff --git a/src/supervisord.ini b/src/supervisord.ini new file mode 100644 index 0000000..dae2fdb --- /dev/null +++ b/src/supervisord.ini @@ -0,0 +1,16 @@ +[supervisord] +nodaemon = true +logfile = /var/log/supervisor/supervisord.log +pidfile = /var/run/supervisord.pid +stdout_logfile = /dev/stdout +stdout_logfile_maxbytes = 0 + +[program:myworker] +command = python3 /app/manage.py rqworker default +process_name = %(program_name)s-%(process_num)s +numprocs = 10 +directory = /app +stopsignal = TERM +autostart = true +autorestart = true +user = root \ No newline at end of file