2024-01-19 03:15:48 +03:00
|
|
|
|
import asyncio
|
2024-02-11 03:14:17 +03:00
|
|
|
|
import multiprocessing
|
2024-01-18 21:15:56 +03:00
|
|
|
|
import sys
|
2024-02-11 03:14:17 +03:00
|
|
|
|
from subprocess import Popen
|
2024-01-18 21:15:56 +03:00
|
|
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
2024-02-02 23:38:12 +03:00
|
|
|
|
from fastfood.repository import create_db_and_tables
|
2024-01-19 03:15:48 +03:00
|
|
|
|
|
2024-02-11 03:14:17 +03:00
|
|
|
|
loop = asyncio.get_event_loop()
|
2024-01-18 21:15:56 +03:00
|
|
|
|
|
2024-02-11 03:14:17 +03:00
|
|
|
|
|
2024-02-11 20:10:25 +03:00
|
|
|
|
def start_celery_worker() -> None:
|
2024-02-11 03:14:17 +03:00
|
|
|
|
Popen(['celery', '-A', 'bg_tasks.bg_task.celery_app', 'worker', '--loglevel=info'])
|
|
|
|
|
|
|
|
|
|
|
2024-02-11 20:10:25 +03:00
|
|
|
|
def start_celery_beat() -> None:
|
2024-02-11 03:14:17 +03:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2024-02-11 20:10:25 +03:00
|
|
|
|
async def run_app() -> None:
|
2024-01-18 21:15:56 +03:00
|
|
|
|
"""
|
2024-01-22 01:28:32 +03:00
|
|
|
|
Запуск FastAPI
|
2024-01-18 21:15:56 +03:00
|
|
|
|
"""
|
|
|
|
|
uvicorn.run(
|
2024-02-03 01:08:04 +03:00
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-02-11 20:10:25 +03:00
|
|
|
|
async def recreate() -> None:
|
2024-01-22 01:28:32 +03:00
|
|
|
|
"""Удаление и создание таблиц в базе данных для тестирования"""
|
|
|
|
|
await create_db_and_tables()
|
|
|
|
|
|
2024-01-18 21:15:56 +03:00
|
|
|
|
|
2024-02-03 01:08:04 +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-01-22 01:28:32 +03:00
|
|
|
|
|
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 в отдельных процессах"""
|
2024-02-11 03:14:17 +03:00
|
|
|
|
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()
|