fix
Some checks failed
Deploy to core / deploy (push) Failing after 1m10s

This commit is contained in:
2024-02-21 20:38:12 +03:00
parent 1f0d5ae8e8
commit 9b2d1c96ba
2 changed files with 20 additions and 42 deletions

View File

@@ -1,4 +1,5 @@
import asyncio
from sqlalchemy import select, event
import json
@@ -122,7 +123,9 @@ async def handle_topic_follower_change(connection, topic_id, follower_id, is_ins
q = select(Topic).filter(Topic.id == topic_id)
q = add_topic_stat_columns(q)
async with connection.begin() as conn:
[topic, shouts_stat, authors_stat, followers_stat] = await conn.execute(q).first()
[topic, shouts_stat, authors_stat, followers_stat] = await conn.execute(
q
).first()
topic.stat = {
'shouts': shouts_stat,
'authors': authors_stat,
@@ -160,13 +163,16 @@ class FollowsCached:
q = select(Author)
q = add_author_stat_columns(q)
authors = session.execute(q)
redis_updates = [] # Store Redis update tasks
while True:
batch = authors.fetchmany(BATCH_SIZE)
if not batch:
break
else:
for [author, shouts_stat, followers_stat, followings_stat] in batch:
await redis.execute('SET', f'user:{author.user}:author', json.dumps({
redis_key = f'user:{author.user}:author'
redis_data = {
'id': author.id,
'name': author.name,
'slug': author.slug,
@@ -175,9 +181,16 @@ class FollowsCached:
'stat': {
'followings': followings_stat,
'shouts': shouts_stat,
'followers': followers_stat
'followers': followers_stat,
},
}))
}
# Add Redis update task to the list
redis_updates.append(
redis.execute('SET', redis_key, json.dumps(redis_data))
)
# Execute Redis update tasks concurrently
await asyncio.gather(*redis_updates)
@staticmethod
async def update_author_cache(author: Author):