Compare commits
No commits in common. "4890df987672647c73461503b41f7ac42b0f0c59" and "b7e066b5257bd0930f8d9eda83fe0cfa5ee79215" have entirely different histories.
4890df9876
...
b7e066b525
|
@ -5,7 +5,8 @@ from misc.models import Setting
|
|||
|
||||
|
||||
@app.task(autoretry_for=(Exception,), retry_backoff=True)
|
||||
def deliver_setting(setting):
|
||||
def deliver_setting(setting_name):
|
||||
setting = Setting.objects.get(name=setting_name)
|
||||
rabbitmq_conf = settings.RABBITMQ
|
||||
dsn = f'{rabbitmq_conf["PROTOCOL"]}://{rabbitmq_conf["USER"]}:{rabbitmq_conf["PASSWORD"]}@{rabbitmq_conf["HOST"]}:{rabbitmq_conf["PORT"]}/'
|
||||
queue = Queue(settings.SETTINGS_QUEUE_NAME, exchange='', routing_key=settings.SETTINGS_QUEUE_NAME, durable=True)
|
||||
|
@ -13,7 +14,7 @@ def deliver_setting(setting):
|
|||
with conn.channel() as channel:
|
||||
producer = Producer(channel)
|
||||
producer.publish(
|
||||
setting,
|
||||
{setting.name: setting.value['value']},
|
||||
exchange='',
|
||||
routing_key=settings.SETTINGS_QUEUE_NAME,
|
||||
declare=[queue],
|
||||
|
|
|
@ -64,27 +64,5 @@
|
|||
"value": 30
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "misc.setting",
|
||||
"pk": 7,
|
||||
"fields": {
|
||||
"name": "DEFAULT_TOP_LIMIT",
|
||||
"description": "Количество пользователей в топе",
|
||||
"value": {
|
||||
"value": 10
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "misc.setting",
|
||||
"pk": 8,
|
||||
"fields": {
|
||||
"name": "DEFAULT_NEIGHBOUR_LIMIT",
|
||||
"description": "Количество соседей",
|
||||
"value": {
|
||||
"value": 3
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -7,4 +7,4 @@ class Command(BaseCommand):
|
|||
|
||||
def handle(self, *args, **options):
|
||||
for setting in Setting.objects.all():
|
||||
deliver_setting.delay({setting.name: setting.value['value']})
|
||||
deliver_setting.delay(setting.name)
|
||||
|
|
|
@ -6,4 +6,4 @@ from misc.celery import deliver_setting as deliver_setting_celery
|
|||
|
||||
@receiver(post_save, sender=Setting, dispatch_uid='deliver_setting')
|
||||
def deliver_setting(sender, instance, **kwargs):
|
||||
deliver_setting_celery.delay({instance.name: instance.value['value']})
|
||||
deliver_setting_celery.delay(instance.name)
|
||||
|
|
|
@ -3,7 +3,6 @@ from fastapi import Depends, FastAPI, Request, Response
|
|||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from functools import partial
|
||||
from starlette.exceptions import HTTPException
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from app.src.routers.api import router as router_api
|
||||
from app.src.routers.handlers import http_error_handler
|
||||
|
@ -11,23 +10,8 @@ from app.src.domain.setting import launch_consumer
|
|||
from app.src.db import connect_pg, get_connection, get_channel, get_rmq, get_pg
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
launch_consumer(connect_pg, get_connection)
|
||||
|
||||
app.state.pg_pool = await connect_pg()
|
||||
|
||||
rmq_conn_pool = aio_pika.pool.Pool(get_connection, max_size=2)
|
||||
rmq_chan_pool = aio_pika.pool.Pool(partial(get_channel, conn_pool=rmq_conn_pool), max_size=10)
|
||||
app.state.rmq_chan_pool = rmq_chan_pool
|
||||
|
||||
yield
|
||||
|
||||
await app.state.pg_pool.close()
|
||||
|
||||
|
||||
def get_application() -> FastAPI:
|
||||
application = FastAPI(lifespan=lifespan)
|
||||
application = FastAPI()
|
||||
|
||||
application.include_router(router_api, prefix='/api')
|
||||
|
||||
|
@ -44,3 +28,19 @@ def get_application() -> FastAPI:
|
|||
return application
|
||||
|
||||
app = get_application()
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
launch_consumer(connect_pg, get_connection)
|
||||
|
||||
app.state.pg_pool = await connect_pg()
|
||||
|
||||
rmq_conn_pool = aio_pika.pool.Pool(get_connection, max_size=2)
|
||||
rmq_chan_pool = aio_pika.pool.Pool(partial(get_channel, conn_pool=rmq_conn_pool), max_size=10)
|
||||
app.state.rmq_chan_pool = rmq_chan_pool
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
await app.state.pg_pool.close()
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
CREATE TABLE users(
|
||||
id BIGINT PRIMARY KEY,
|
||||
energy INTEGER NOT NULL CONSTRAINT non_negative_energy CHECK (energy >= 0),
|
||||
session TEXT NOT NULL
|
||||
session VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE clicks(
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import asyncio
|
||||
|
||||
from app.src.config import PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DB
|
||||
from pathlib import Path
|
||||
from starlette.requests import Request
|
||||
import asyncpg
|
||||
import logging
|
||||
from asyncpg_trek import plan, execute, Direction
|
||||
from asyncpg_trek.asyncpg import AsyncpgBackend
|
||||
|
||||
|
@ -9,6 +12,8 @@ from asyncpg_trek.asyncpg import AsyncpgBackend
|
|||
DB_URL = f'postgresql://{PG_USER}:{str(PG_PASSWORD)}@{PG_HOST}:{PG_PORT}/{PG_DB}'
|
||||
MIGRATIONS_DIR = Path(__file__).parent.resolve() / "migrations"
|
||||
|
||||
logger = logging.getLogger("uvicorn")
|
||||
|
||||
async def connect_pg() -> asyncpg.Pool:
|
||||
return await asyncpg.create_pool(DB_URL)
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import asyncio
|
||||
import aio_pika
|
||||
import logging
|
||||
from starlette.requests import Request
|
||||
from aio_pika.abc import AbstractRobustConnection
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import aio_pika
|
|||
from typing import Callable
|
||||
import asyncpg
|
||||
|
||||
|
||||
SETTING_QUEUE_NAME = "settings"
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import logging
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from create_bot import bot, token, WEBHOOK_URL
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
set -o nounset
|
||||
set -o xtrace
|
||||
|
||||
uvicorn main_api:app --host 0.0.0.0 --port "${HTTP_PORT}" --log-config /app/log.ini
|
7
bot/scripts/gunicorn.sh
Executable file
7
bot/scripts/gunicorn.sh
Executable file
|
@ -0,0 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
set -o nounset
|
||||
|
||||
gunicorn main_api:app -w 17 -b 0.0.0.0:7313 -k uvicorn.workers.UvicornWorker --timeout 600
|
|
@ -1,7 +0,0 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
set -o nounset
|
||||
|
||||
gunicorn main_api:app -w "${WORKER_COUNT}" -b "0.0.0.0:${HTTP_PORT}" -k uvicorn.workers.UvicornWorker --timeout 600
|
8
bot/scripts/start.sh
Executable file
8
bot/scripts/start.sh
Executable file
|
@ -0,0 +1,8 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
set -o nounset
|
||||
set -o xtrace
|
||||
|
||||
uvicorn main_api:app --host 0.0.0.0 --port 7313 --log-config /app/log.ini
|
|
@ -11,7 +11,7 @@ services:
|
|||
volumes:
|
||||
- backend_media:/app/media
|
||||
- backend_static:/app/static
|
||||
command: /app/scripts/prod.sh
|
||||
command: /app/scripts/gunicorn.sh
|
||||
restart: on-failure
|
||||
depends_on:
|
||||
postgres: &healthy-dependency
|
||||
|
@ -21,7 +21,7 @@ services:
|
|||
- .env/prod/pg
|
||||
- .env/prod/back
|
||||
- .env/prod/rmq
|
||||
- .env/prod/bot_token
|
||||
- .env/prod/bot
|
||||
- .env/prod/web
|
||||
|
||||
bot:
|
||||
|
@ -32,9 +32,8 @@ services:
|
|||
batcher: *started-dependency
|
||||
env_file:
|
||||
- .env/prod/bot
|
||||
- .env/prod/bot_token
|
||||
- .env/prod/web
|
||||
command: /app/scripts/prod.sh
|
||||
command: /app/scripts/gunicorn.sh
|
||||
restart: on-failure
|
||||
|
||||
postgres: &pg-conf
|
||||
|
@ -93,7 +92,7 @@ services:
|
|||
- .env/prod/rmq
|
||||
- .env/prod/batcher-pg
|
||||
- .env/prod/batcher
|
||||
- .env/prod/bot_token
|
||||
- .env/prod/bot
|
||||
|
||||
batcher-postgres:
|
||||
<<: *pg-conf
|
||||
|
|
|
@ -14,13 +14,13 @@ services:
|
|||
volumes:
|
||||
- backend_media:/app/media
|
||||
- backend_static:/app/static
|
||||
command: /app/scripts/dev.sh
|
||||
command: /app/scripts/start.sh
|
||||
restart: on-failure
|
||||
env_file: &backend-env-files
|
||||
- .env/dev/pg
|
||||
- .env/dev/back
|
||||
- .env/dev/rmq
|
||||
- .env/dev/bot_token
|
||||
- .env/dev/bot
|
||||
- .env/dev/web
|
||||
ports:
|
||||
- '8000:8000'
|
||||
|
@ -74,7 +74,7 @@ services:
|
|||
- .env/dev/rmq
|
||||
- .env/dev/batcher-pg
|
||||
- .env/dev/batcher
|
||||
- .env/dev/bot_token
|
||||
- .env/dev/bot
|
||||
- .env/dev/web
|
||||
ports:
|
||||
- '8080:8080'
|
||||
|
|
Loading…
Reference in New Issue
Block a user