From f596a9bf2ce539fdbc9e8249bdc47e31a47af3ec Mon Sep 17 00:00:00 2001 From: Untone Date: Sun, 25 Feb 2024 21:16:34 +0300 Subject: [PATCH] update-author_cache --- Dockerfile | 23 ++++++++++++++++++----- resolvers/author.py | 4 ++-- services/db.py | 8 +++++--- services/event_listeners.py | 16 ++++++++-------- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 981c3404..5753338a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,25 @@ FROM python:alpine + +# Update package lists and install necessary dependencies +RUN apk update && \ + apk add --no-cache build-base icu-data-full curl python3-dev musl-dev postgresql-dev postgresql-client && \ + curl -sSL https://install.python-poetry.org | python + +# Set working directory WORKDIR /app + +# Copy only the pyproject.toml file initially +COPY pyproject.toml /app/ + +# Install poetry and dependencies +RUN pip install poetry && \ + poetry config virtualenvs.create false && \ + poetry install --no-root --only main + +# Copy the rest of the application COPY . /app -RUN apk update && apk add --no-cache build-base icu-data-full curl python3-dev musl-dev postgresql-dev postgresql-client -RUN curl -sSL https://install.python-poetry.org | python -ENV PATH="${PATH}:/root/.local/bin" -RUN poetry config virtualenvs.create false && poetry install --only main - +# Expose the port EXPOSE 8000 CMD ["python", "server.py"] diff --git a/resolvers/author.py b/resolvers/author.py index 27a6ed86..22d3fef4 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -55,7 +55,7 @@ async def get_author(_, _info, slug='', author_id=None): q = select(aliased_author).where(aliased_author.id == author_id) [author] = get_with_stat(q) if author: - await update_author_cache(author) + await update_author_cache(author.dict()) else: author = json.loads(cache) except Exception as exc: @@ -79,7 +79,7 @@ async def get_author_by_user_id(user_id: str): [author] = get_with_stat(q) if author: - await update_author_cache(author) + await update_author_cache(author.dict()) except Exception as exc: logger.error(exc) return author diff --git a/services/db.py b/services/db.py index 4c2254d5..68d11d2f 100644 --- a/services/db.py +++ b/services/db.py @@ -20,6 +20,10 @@ inspector = inspect(engine) configure_mappers() T = TypeVar('T') REGISTRY: Dict[str, type] = {} +FILTERED_FIELDS = [ + '_sa_instance_state', + 'search_vector' +] # noinspection PyUnusedLocal @@ -42,9 +46,7 @@ class Base(declarative_base()): REGISTRY[cls.__name__] = cls def dict(self) -> Dict[str, Any]: - column_names = self.__table__.columns.keys() - if '_sa_instance_state' in column_names: - column_names.remove('_sa_instance_state') + column_names = filter(lambda x: x not in FILTERED_FIELDS, self.__table__.columns.keys()) try: return {c: getattr(self, c) for c in column_names} except Exception as e: diff --git a/services/event_listeners.py b/services/event_listeners.py index 5318076b..0c562d5b 100644 --- a/services/event_listeners.py +++ b/services/event_listeners.py @@ -18,8 +18,8 @@ DEFAULT_FOLLOWS = { } -async def update_author_cache(author: Author, ttl=25 * 60 * 60): - payload = json.dumps(author.dict()) +async def update_author_cache(author: dict, ttl=25 * 60 * 60): + payload = json.dumps(author) await redis.execute('SETEX', f'user:{author.user}:author', ttl, payload) await redis.execute('SETEX', f'id:{author.id}:author', ttl, payload) @@ -48,7 +48,7 @@ def after_shouts_update(mapper, connection, shout: Shout): ) authors = get_with_stat(authors_query) for author in authors: - asyncio.create_task(update_author_cache(author)) + asyncio.create_task(update_author_cache(author.dict())) @event.listens_for(Reaction, 'after_insert') @@ -64,7 +64,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction): authors = get_with_stat(author_query) for author in authors: - asyncio.create_task(update_author_cache(author)) + asyncio.create_task(update_author_cache(author.author())) shout = connection.execute(select(Shout).where(Shout.id == reaction.shout)).first() if shout: @@ -74,7 +74,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction): @event.listens_for(Author, 'after_insert') @event.listens_for(Author, 'after_update') def after_author_update(mapper, connection, author: Author): - asyncio.create_task(update_author_cache(author)) + asyncio.create_task(update_author_cache(author.dict())) @event.listens_for(TopicFollower, 'after_insert') @@ -132,8 +132,8 @@ async def handle_author_follower_change( follower_query = select(Author).filter(Author.id == follower_id) follower = get_with_stat(follower_query) if follower and author: - _ = asyncio.create_task(update_author_cache(author)) - _ = asyncio.create_task(update_author_cache(follower)) + _ = asyncio.create_task(update_author_cache(author.dict())) + _ = asyncio.create_task(update_author_cache(follower.dict())) await update_follows_for_user( connection, follower.user, @@ -159,7 +159,7 @@ async def handle_topic_follower_change( follower_query = select(Author).filter(Author.id == follower_id) follower = get_with_stat(follower_query) if follower and topic: - _ = asyncio.create_task(update_author_cache(follower)) + _ = asyncio.create_task(update_author_cache(follower.dict())) await update_follows_for_user( connection, follower.user,