fastfood/manage.py

60 lines
1.6 KiB
Python
Raw Permalink Normal View History

import asyncio
import multiprocessing
2024-01-18 21:15:56 +03:00
import sys
from subprocess import Popen
2024-01-18 21:15:56 +03:00
import uvicorn
from fastfood.repository import create_db_and_tables
loop = asyncio.get_event_loop()
2024-01-18 21:15:56 +03:00
def start_celery_worker() -> None:
Popen(['celery', '-A', 'bg_tasks.bg_task.celery_app', 'worker', '--loglevel=info'])
def start_celery_beat() -> None:
Popen(['celery', '-A', 'bg_tasks.bg_task.celery_app', 'beat', '--loglevel=info'])
celery_worker_process = multiprocessing.Process(target=start_celery_worker)
celery_beat_process = multiprocessing.Process(target=start_celery_beat)
async def run_app() -> None:
2024-01-18 21:15:56 +03:00
"""
Запуск FastAPI
2024-01-18 21:15:56 +03:00
"""
uvicorn.run(
app='fastfood.app:create_app',
host='0.0.0.0',
2024-01-28 16:22:24 +03:00
port=8000,
2024-01-18 21:15:56 +03:00
reload=True,
factory=True,
2024-01-28 16:22:24 +03:00
workers=1,
2024-01-18 21:15:56 +03:00
)
async def recreate() -> None:
"""Удаление и создание таблиц в базе данных для тестирования"""
await create_db_and_tables()
2024-01-18 21:15:56 +03:00
if __name__ == '__main__':
2024-02-12 01:06:45 +03:00
if '--run-docker-server' in sys.argv:
2024-02-12 03:03:24 +03:00
"""Запуск FastAPI в докере. Celery запускается в отдельном контейнере"""
2024-02-12 01:06:45 +03:00
loop.run_until_complete(recreate())
loop.run_until_complete(run_app())
2024-02-12 01:06:45 +03:00
if '--run-local-server' in sys.argv:
2024-02-12 03:03:24 +03:00
"""Локальный запуск FastAPI с запуском Celery в отдельных процессах"""
celery_worker_process.start()
celery_beat_process.start()
loop.run_until_complete(recreate())
loop.run_until_complete(run_app())
celery_beat_process.kill()
celery_worker_process.kill()