This commit is contained in:
parent
d5ba8d1cde
commit
173c865a69
3
cache/precache.py
vendored
3
cache/precache.py
vendored
|
@ -10,6 +10,7 @@ from orm.topic import Topic, TopicFollower
|
||||||
from resolvers.stat import get_with_stat
|
from resolvers.stat import get_with_stat
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.redis import redis
|
from services.redis import redis
|
||||||
|
from settings import REDIS_URL
|
||||||
from utils.encoders import CustomJSONEncoder
|
from utils.encoders import CustomJSONEncoder
|
||||||
from utils.logger import root_logger as logger
|
from utils.logger import root_logger as logger
|
||||||
|
|
||||||
|
@ -81,7 +82,7 @@ async def precache_data():
|
||||||
# cache reset
|
# cache reset
|
||||||
value = await redis.get(key)
|
value = await redis.get(key)
|
||||||
await redis.execute("FLUSHDB")
|
await redis.execute("FLUSHDB")
|
||||||
logger.info("redis flushed")
|
logger.info(f"FLUSHDB {REDIS_URL}")
|
||||||
if value is not None:
|
if value is not None:
|
||||||
await redis.execute("HSET", key, value)
|
await redis.execute("HSET", key, value)
|
||||||
logger.info(f"Значение ключа '{key}' сохранено")
|
logger.info(f"Значение ключа '{key}' сохранено")
|
||||||
|
|
15
main.py
15
main.py
|
@ -1,3 +1,4 @@
|
||||||
|
import asyncio
|
||||||
import os
|
import os
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from os.path import exists
|
from os.path import exists
|
||||||
|
@ -6,6 +7,7 @@ from ariadne import load_schema_from_path, make_executable_schema
|
||||||
from ariadne.asgi import GraphQL
|
from ariadne.asgi import GraphQL
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Route
|
from starlette.routing import Route
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
from cache.precache import precache_data
|
from cache.precache import precache_data
|
||||||
from cache.revalidator import revalidation_manager
|
from cache.revalidator import revalidation_manager
|
||||||
|
@ -45,10 +47,21 @@ def create_all_tables():
|
||||||
create_table_if_not_exists(engine, model)
|
create_table_if_not_exists(engine, model)
|
||||||
|
|
||||||
|
|
||||||
|
# Оборачиваем GraphQL-обработчик для лучшей обработки ошибок
|
||||||
|
async def graphql_handler(request):
|
||||||
|
try:
|
||||||
|
graphql_app = GraphQL(schema, debug=True)
|
||||||
|
return await graphql_app(request)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
return JSONResponse({"error": "Request cancelled"}, status_code=499)
|
||||||
|
except Exception as e:
|
||||||
|
return JSONResponse({"error": str(e)}, status_code=500)
|
||||||
|
|
||||||
|
|
||||||
# main starlette app object with ariadne mounted in root
|
# main starlette app object with ariadne mounted in root
|
||||||
app = Starlette(
|
app = Starlette(
|
||||||
routes=[
|
routes=[
|
||||||
Route("/", GraphQL(schema, debug=True)),
|
Route("/", graphql_handler),
|
||||||
Route("/new-author", WebhookEndpoint),
|
Route("/new-author", WebhookEndpoint),
|
||||||
],
|
],
|
||||||
on_startup=[
|
on_startup=[
|
||||||
|
|
|
@ -12,7 +12,7 @@ SQLAlchemy = "^2.0.29"
|
||||||
psycopg2-binary = "^2.9.9"
|
psycopg2-binary = "^2.9.9"
|
||||||
redis = {extras = ["hiredis"], version = "^5.0.1"}
|
redis = {extras = ["hiredis"], version = "^5.0.1"}
|
||||||
sentry-sdk = {version = "^1.44.1", extras = ["starlette", "ariadne", "sqlalchemy"]}
|
sentry-sdk = {version = "^1.44.1", extras = ["starlette", "ariadne", "sqlalchemy"]}
|
||||||
starlette = "^0.37.2"
|
starlette = "^0.39.2"
|
||||||
gql = "^3.5.0"
|
gql = "^3.5.0"
|
||||||
ariadne = "^0.23.0"
|
ariadne = "^0.23.0"
|
||||||
pre-commit = "^3.7.0"
|
pre-commit = "^3.7.0"
|
||||||
|
|
|
@ -55,7 +55,7 @@ class ViewedStorage:
|
||||||
# Запуск фоновой задачи
|
# Запуск фоновой задачи
|
||||||
_task = asyncio.create_task(self.worker())
|
_task = asyncio.create_task(self.worker())
|
||||||
else:
|
else:
|
||||||
logger.info(" * Пожалуйста, добавьте ключевой файл Google Analytics")
|
logger.warning(" * Пожалуйста, добавьте ключевой файл Google Analytics")
|
||||||
self.disabled = True
|
self.disabled = True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -79,7 +79,7 @@ class ViewedStorage:
|
||||||
self.views_by_shout.update(precounted_views)
|
self.views_by_shout.update(precounted_views)
|
||||||
logger.info(f" * {len(precounted_views)} публикаций с просмотрами успешно загружены.")
|
logger.info(f" * {len(precounted_views)} публикаций с просмотрами успешно загружены.")
|
||||||
else:
|
else:
|
||||||
logger.info(" * Файл просмотров не найден.")
|
logger.warning(" * Файл просмотров не найден.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Ошибка загрузки предварительно подсчитанных просмотров: {e}")
|
logger.error(f"Ошибка загрузки предварительно подсчитанных просмотров: {e}")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user