upd: Контейнеры для celery & rabbitmq

develop
Сергей Ванюшкин 2024-02-12 00:39:51 +03:00
parent 550a058b6f
commit fc9577c538
6 changed files with 78 additions and 20 deletions

View File

@ -8,7 +8,11 @@ RUN mkdir -p /usr/src/fastfood
WORKDIR /usr/src/fastfood WORKDIR /usr/src/fastfood
COPY . . COPY ./example.env .
COPY ./poetry.lock .
COPY ./pyproject.toml .
RUN touch /usr/src/RUN_IN_DOCKER RUN touch /usr/src/RUN_IN_DOCKER

View File

@ -12,7 +12,9 @@ from fastfood.models import Dish, Menu, SubMenu
file = os.path.join(os.path.curdir, 'admin', 'Menu.xlsx') file = os.path.join(os.path.curdir, 'admin', 'Menu.xlsx')
redis = redis.Redis(host='127.0.0.1', port=6379, db=0) redis = redis.Redis(
host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB
)
async_engine = create_async_engine(settings.DATABASE_URL_asyncpg) async_engine = create_async_engine(settings.DATABASE_URL_asyncpg)
async_session_maker = async_sessionmaker( async_session_maker = async_sessionmaker(
@ -22,7 +24,10 @@ async_session_maker = async_sessionmaker(
) )
async def refresh_cache(disconts: dict): async def refresh_cache(disconts: dict) -> None:
"""Очищает кэш при обновлении БД и ставит отметку времени обновления
и сохраняет данные скидок на товар
"""
await redis.flushall() await redis.flushall()
for key in disconts.keys(): for key in disconts.keys():
@ -50,7 +55,6 @@ async def is_changed_xls() -> bool:
async def xlsx_to_dict() -> dict: async def xlsx_to_dict() -> dict:
"""Парсит Menu.xlsx в словарь""" """Парсит Menu.xlsx в словарь"""
print('run dumping xlsx_to_dict')
wb = openpyxl.load_workbook(file).worksheets[0] wb = openpyxl.load_workbook(file).worksheets[0]
data = {} data = {}
@ -89,7 +93,9 @@ async def xlsx_to_dict() -> dict:
async def refresh_all_data(data: dict) -> dict[UUID, int | float]: async def refresh_all_data(data: dict) -> dict[UUID, int | float]:
"""Чистит кэш удаляем старые данные и пушим новые""" """Удаляет старые данные и сохраняет новые.
Создает и возвращает список со скидками с привязкой по UUID товара
"""
disconts = {} disconts = {}
@ -133,10 +139,10 @@ async def refresh_all_data(data: dict) -> dict[UUID, int | float]:
return disconts return disconts
async def main(): async def main() -> None:
"""Главная функция фоновой задачи"""
changed = await is_changed_xls() changed = await is_changed_xls()
if changed: if changed:
print('Файл изменен, обновляю базу')
menu_data = await xlsx_to_dict() menu_data = await xlsx_to_dict()
discont_data = await refresh_all_data(menu_data) discont_data = await refresh_all_data(menu_data)
await refresh_cache(discont_data) await refresh_cache(discont_data)

View File

@ -57,4 +57,35 @@ services:
restart: always restart: always
volumes:
- .:/usr/src/fastfood
command: /bin/bash -c 'poetry run python /usr/src/fastfood/manage.py --run-test-server' command: /bin/bash -c 'poetry run python /usr/src/fastfood/manage.py --run-test-server'
celery:
container_name: celery
build:
context: .
env_file:
- .env
depends_on:
- rabbitmq
- db
- app
- redis
volumes:
- .:/usr/src/fastfood
command: /bin/bash -c 'poetry run python /usr/src/fastfood/manage.py --run-selery'
rabbitmq:
container_name: rabbit
image: "rabbitmq:management"
ports:
- 5672:5672

View File

@ -1,7 +1,14 @@
DB_HOST=db # PosgreSQL адрес сервера
DB_PORT=5432 POSTGRES_HOST=127.0.0.1
POSTGRES_PORT=5432
# Пользователь БД Postgres
POSTGRES_USER=testuser POSTGRES_USER=testuser
POSTGRES_PASSWORD=test POSTGRES_PASSWORD=test
# БД рабочая и тестовая
POSTGRES_DB=fastfood_db POSTGRES_DB=fastfood_db
POSTGRES_DB_TEST=testdb POSTGRES_DB_TEST=fastfood_db_test
REDIS_DB=redis://localhost
# Redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_DB=0

View File

@ -4,13 +4,17 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings): class Settings(BaseSettings):
DB_HOST: str = '' # Конфиг PostgreSql
DB_PORT: int = 5432 POSTGRES_HOST: str = ''
POSTGRES_PORT: int = 5432
POSTGRES_DB: str = '' POSTGRES_DB: str = ''
POSTGRES_PASSWORD: str = '' POSTGRES_PASSWORD: str = ''
POSTGRES_USER: str = '' POSTGRES_USER: str = ''
POSTGRES_DB_TEST: str = '' POSTGRES_DB_TEST: str = ''
REDIS_DB: str = '' # Конфиг Redis
REDIS_HOST: str = ''
REDIS_PORT: int = 6379
REDIS_DB: int = 0
@property @property
def DATABASE_URL_asyncpg(self) -> str: def DATABASE_URL_asyncpg(self) -> str:
@ -18,19 +22,18 @@ class Settings(BaseSettings):
Возвращает строку подключения к БД необходимую для SQLAlchemy Возвращает строку подключения к БД необходимую для SQLAlchemy
""" """
# Проверяем, в DOCKER или нет # Проверяем, в DOCKER или нет
file_path = '/usr/src/RUN_IN_DOCKER' file_path = '/usr/src/RUN_IN_DOCKER'
if os.path.exists(file_path): if os.path.exists(file_path):
return ( return (
'postgresql+asyncpg://' 'postgresql+asyncpg://'
f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}'
f'@db:{self.DB_PORT}/{self.POSTGRES_DB}' f'@db:5432/{self.POSTGRES_DB}'
) )
return ( return (
'postgresql+asyncpg://' 'postgresql+asyncpg://'
f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}'
f'@{self.DB_HOST}:{self.DB_PORT}/{self.POSTGRES_DB}' f'@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}'
) )
@property @property
@ -43,22 +46,25 @@ class Settings(BaseSettings):
return ( return (
'postgresql+asyncpg://' 'postgresql+asyncpg://'
f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}'
f'@db:{self.DB_PORT}/{self.POSTGRES_DB_TEST}' f'@db:5432/{self.POSTGRES_DB_TEST}'
) )
return ( return (
'postgresql+asyncpg://' 'postgresql+asyncpg://'
f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}' f'{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}'
f'@{self.DB_HOST}:{self.DB_PORT}/{self.POSTGRES_DB_TEST}' f'@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB_TEST}'
) )
@property @property
def REDIS_URL(self): def REDIS_URL(self):
"""
Возвращает строку подключения к REDIS
"""
file_path = '/usr/src/RUN_IN_DOCKER' file_path = '/usr/src/RUN_IN_DOCKER'
if os.path.exists(file_path): if os.path.exists(file_path):
return 'redis://redis:6379/0' return 'redis://redis:6379/0'
return self.REDIS_DB return f'redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}'
model_config = SettingsConfigDict(env_file='.env') model_config = SettingsConfigDict(env_file='.env')

View File

@ -42,6 +42,10 @@ async def recreate() -> None:
if __name__ == '__main__': if __name__ == '__main__':
if '--run-celery' in sys.argv:
celery_worker_process.start()
celery_beat_process.start()
if '--run-server' in sys.argv: if '--run-server' in sys.argv:
pass pass