circular-fix
Some checks failed
Deploy on push / deploy (push) Failing after 6s

This commit is contained in:
Untone 2024-02-24 19:53:47 +03:00
parent f1444cbe10
commit a3244fc74b
2 changed files with 40 additions and 49 deletions

View File

@ -1,5 +1,3 @@
import asyncio
from sqlalchemy import func, distinct, select, join, and_ from sqlalchemy import func, distinct, select, join, and_
from sqlalchemy.orm import aliased from sqlalchemy.orm import aliased
@ -8,7 +6,6 @@ from orm.topic import TopicFollower, Topic
from services.db import local_session from services.db import local_session
from orm.author import AuthorFollower, Author, AuthorRating from orm.author import AuthorFollower, Author, AuthorRating
from orm.shout import ShoutTopic, ShoutAuthor, Shout from orm.shout import ShoutTopic, ShoutAuthor, Shout
from services.follows import update_author_cache
from services.logger import root_logger as logger from services.logger import root_logger as logger
@ -169,7 +166,6 @@ def get_authors_with_stat(q, ratings=False):
authors_with_ratings = [] authors_with_ratings = []
for author in authors: for author in authors:
authors_with_ratings.append(load_author_ratings(author)) authors_with_ratings.append(load_author_ratings(author))
_ = asyncio.create_task(update_author_cache(author))
return authors_with_ratings return authors_with_ratings
return authors return authors

View File

@ -56,9 +56,7 @@ def after_author_follower_delete(mapper, connection, target: AuthorFollower):
) )
async def update_follows_for_user( async def update_follows_for_user(connection, user_id, entity_type, entity: dict, is_insert):
connection, user_id, entity_type, entity: dict, is_insert
):
redis_key = f'user:{user_id}:follows' redis_key = f'user:{user_id}:follows'
follows_str = await redis.get(redis_key) follows_str = await redis.get(redis_key)
if follows_str: if follows_str:
@ -69,55 +67,52 @@ async def update_follows_for_user(
follows[f'{entity_type}s'].append(entity) follows[f'{entity_type}s'].append(entity)
else: else:
# Remove the entity from follows # Remove the entity from follows
follows[f'{entity_type}s'] = [ follows[f'{entity_type}s'] = [e for e in follows[f'{entity_type}s'] if e['id'] != entity['id']]
e for e in follows[f'{entity_type}s'] if e['id'] != entity['id']
]
await redis.execute('SET', redis_key, json.dumps(follows)) await redis.execute('SET', redis_key, json.dumps(follows))
async def handle_author_follower_change(connection, author_id: int, follower_id: int, is_insert: bool): async def handle_author_follower_change(connection, author_id: int, follower_id: int, is_insert: bool):
q = select(Author).filter(Author.id == author_id) author_query = select(Author).filter(Author.id == author_id)
authors = get_authors_with_stat(q) [author, ] = get_authors_with_stat(author_query, ratings=True)
author = authors[0] follower_query = select(Author).filter(Author.id == follower_id)
async with connection.begin() as conn: follower = get_authors_with_stat(follower_query, ratings=True)
follower = await conn.execute( if follower and author:
select(Author).filter(Author.id == follower_id) _ = asyncio.create_task(update_author_cache(author))
).first() _ = asyncio.create_task(update_author_cache(follower))
if follower and author: await update_follows_for_user(
await update_follows_for_user( connection,
conn, follower.user,
follower.user, 'author',
'author', {
{ 'id': author.id,
'id': author.id, 'name': author.name,
'name': author.name, 'slug': author.slug,
'slug': author.slug, 'pic': author.pic,
'pic': author.pic, 'bio': author.bio,
'bio': author.bio, 'stat': author.stat,
'stat': author.stat, },
}, is_insert,
is_insert, )
)
async def handle_topic_follower_change(connection, topic_id: int, follower_id: int, is_insert: bool): async def handle_topic_follower_change(connection, topic_id: int, follower_id: int, is_insert: bool):
q = select(Topic).filter(Topic.id == topic_id) q = select(Topic).filter(Topic.id == topic_id)
topics = get_topics_with_stat(q) topics = get_topics_with_stat(q)
topic = topics[0] topic = topics[0]
follower_query = select(Author).filter(Author.id == follower_id)
async with connection.begin() as conn: follower = get_authors_with_stat(follower_query, ratings=True)
follower = conn.execute(select(Author).filter(Author.id == follower_id)).first() if follower and topic:
if follower and topic: _ = asyncio.create_task(update_author_cache(follower))
await update_follows_for_user( await update_follows_for_user(
conn, connection,
follower.user, follower.user,
'topic', 'topic',
{ {
'id': topic.id, 'id': topic.id,
'title': topic.title, 'title': topic.title,
'slug': topic.slug, 'slug': topic.slug,
'body': topic.body, 'body': topic.body,
'stat': topic.stat, 'stat': topic.stat,
}, },
is_insert, is_insert,
) )