session-close-fix

This commit is contained in:
Untone 2024-05-26 02:17:45 +03:00
parent 3742528e3a
commit da89b20e5c
3 changed files with 136 additions and 125 deletions

View File

@ -63,29 +63,30 @@ async def get_author(_, _info, slug="", author_id=0):
author_query = author_query.filter(Author.id == author_id) author_query = author_query.filter(Author.id == author_id)
else: else:
raise ValueError("Author not found") raise ValueError("Author not found")
lookup_result = local_session().execute(author_query).first() with local_session() as session:
if lookup_result: lookup_result = session.execute(author_query).first()
[found_author] = lookup_result if lookup_result:
# logger.debug(found_author) [found_author] = lookup_result
if found_author: # logger.debug(found_author)
logger.debug(f"found author id: {found_author.id}") if found_author:
author_id = found_author.id if found_author.id else author_id logger.debug(f"found author id: {found_author.id}")
if author_id: author_id = found_author.id if found_author.id else author_id
cached_result = await redis.execute("GET", f"author:{author_id}") if author_id:
if isinstance(cached_result, str): cached_result = await redis.execute("GET", f"author:{author_id}")
author_dict = json.loads(cached_result) if isinstance(cached_result, str):
author_dict = json.loads(cached_result)
# update stat from db # update stat from db
if not author_dict or not author_dict.get("stat"): if not author_dict or not author_dict.get("stat"):
result = get_with_stat(author_query) result = get_with_stat(author_query)
if not result: if not result:
raise ValueError("Author not found") raise ValueError("Author not found")
[author] = result [author] = result
# use found author # use found author
if isinstance(author, Author): if isinstance(author, Author):
logger.debug(f"update @{author.slug} with id {author.id}") logger.debug(f"update @{author.slug} with id {author.id}")
author_dict = author.dict() author_dict = author.dict()
await cache_author(author_dict) await cache_author(author_dict)
except ValueError: except ValueError:
pass pass
except Exception as exc: except Exception as exc:
@ -150,19 +151,20 @@ async def load_authors_by(_, _info, by, limit, offset):
before = int(time.time()) - by["created_at"] before = int(time.time()) - by["created_at"]
authors_query = authors_query.filter(Author.created_at > before) authors_query = authors_query.filter(Author.created_at > before)
authors_query = authors_query.limit(limit).offset(offset) authors_query = authors_query.limit(limit).offset(offset)
authors_nostat = local_session().execute(authors_query) with local_session() as session:
authors = [] authors_nostat = session.execute(authors_query)
if authors_nostat: authors = []
for [a] in authors_nostat: if authors_nostat:
author_dict = None for [a] in authors_nostat:
if isinstance(a, Author): author_dict = None
author_id = a.id if isinstance(a, Author):
if bool(author_id): author_id = a.id
cached_result = await redis.execute("GET", f"author:{author_id}") if bool(author_id):
if isinstance(cached_result, str): cached_result = await redis.execute("GET", f"author:{author_id}")
author_dict = json.loads(cached_result) if isinstance(cached_result, str):
if not author_dict or not isinstance(author_dict.get("shouts"), int): author_dict = json.loads(cached_result)
break if not author_dict or not isinstance(author_dict.get("shouts"), int):
break
# order # order
order = by.get("order") order = by.get("order")
@ -185,46 +187,47 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0):
author_query = author_query.filter(Author.id == author_id) author_query = author_query.filter(Author.id == author_id)
else: else:
return {"error": "One of slug, user, or author_id must be provided"} return {"error": "One of slug, user, or author_id must be provided"}
result = local_session().execute(author_query) with local_session() as session:
if result: result = session.execute(author_query)
# logger.debug(result) if result:
[author] = result # logger.debug(result)
# logger.debug(author) [author] = result
if author and isinstance(author, Author): # logger.debug(author)
# logger.debug(author.dict()) if author and isinstance(author, Author):
author_id = author.id if not author_id else author_id # logger.debug(author.dict())
topics = [] author_id = author.id if not author_id else author_id
authors = [] topics = []
if bool(author_id): authors = []
rkey = f"author:{author_id}:follows-authors" if bool(author_id):
logger.debug(f"getting {author_id} follows authors") rkey = f"author:{author_id}:follows-authors"
cached = await redis.execute("GET", rkey) logger.debug(f"getting {author_id} follows authors")
if not cached: cached = await redis.execute("GET", rkey)
authors = author_follows_authors(author_id) # type: ignore if not cached:
prepared = [author.dict() for author in authors] authors = author_follows_authors(author_id) # type: ignore
await redis.execute( prepared = [author.dict() for author in authors]
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder) await redis.execute(
) "SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
elif isinstance(cached, str): )
authors = json.loads(cached) elif isinstance(cached, str):
authors = json.loads(cached)
rkey = f"author:{author_id}:follows-topics" rkey = f"author:{author_id}:follows-topics"
cached = await redis.execute("GET", rkey) cached = await redis.execute("GET", rkey)
if cached and isinstance(cached, str): if cached and isinstance(cached, str):
topics = json.loads(cached) topics = json.loads(cached)
if not cached: if not cached:
topics = author_follows_topics(author_id) # type: ignore topics = author_follows_topics(author_id) # type: ignore
prepared = [topic.dict() for topic in topics] prepared = [topic.dict() for topic in topics]
await redis.execute( await redis.execute(
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder) "SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
) )
return { return {
"topics": topics, "topics": topics,
"authors": authors, "authors": authors,
"communities": [ "communities": [
{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""} {"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}
], ],
} }
except Exception: except Exception:
import traceback import traceback
@ -310,44 +313,45 @@ async def get_author_followers(_, _info, slug: str):
try: try:
author_alias = aliased(Author) author_alias = aliased(Author)
author_query = select(author_alias).filter(author_alias.slug == slug) author_query = select(author_alias).filter(author_alias.slug == slug)
result = local_session().execute(author_query).first() with local_session() as session:
followers = [] result = session.execute(author_query).first()
if result: followers = []
[author] = result if result:
author_id = author.id [author] = result
cached = await redis.execute("GET", f"author:{author_id}:followers") author_id = author.id
if cached: cached = await redis.execute("GET", f"author:{author_id}:followers")
followers_ids = [] if cached:
followers = [] followers_ids = []
if isinstance(cached, str): followers = []
followers_cached = json.loads(cached) if isinstance(cached, str):
if isinstance(followers_cached, list): followers_cached = json.loads(cached)
logger.debug( if isinstance(followers_cached, list):
f"@{slug} got {len(followers_cached)} followers cached" logger.debug(
) f"@{slug} got {len(followers_cached)} followers cached"
for fc in followers_cached: )
if fc["id"] not in followers_ids and fc["id"] != author_id: for fc in followers_cached:
followers.append(fc) if fc["id"] not in followers_ids and fc["id"] != author_id:
followers_ids.append(fc["id"]) followers.append(fc)
return followers followers_ids.append(fc["id"])
return followers
author_follower_alias = aliased(AuthorFollower, name="af") author_follower_alias = aliased(AuthorFollower, name="af")
followers_query = select(Author).join( followers_query = select(Author).join(
author_follower_alias, author_follower_alias,
and_( and_(
author_follower_alias.author == author_id, author_follower_alias.author == author_id,
author_follower_alias.follower == Author.id, author_follower_alias.follower == Author.id,
Author.id != author_id, # exclude the author from the followers Author.id != author_id, # exclude the author from the followers
), ),
) )
followers = get_with_stat(followers_query) followers = get_with_stat(followers_query)
if isinstance(followers, list): if isinstance(followers, list):
followers_ids = [r.id for r in followers] followers_ids = [r.id for r in followers]
for follower in followers: for follower in followers:
if follower.id not in followers_ids: if follower.id not in followers_ids:
await cache_follow_author_change(follower.dict(), author.dict()) await cache_follow_author_change(follower.dict(), author.dict())
followers_ids.append(follower.id) followers_ids.append(follower.id)
logger.debug(f"@{slug} cache updated with {len(followers)} followers") logger.debug(f"@{slug} cache updated with {len(followers)} followers")
return followers return followers
except Exception as exc: except Exception as exc:
import traceback import traceback

View File

@ -73,8 +73,8 @@ async def follow(_, info, what, slug):
_topic_dict = await cache_by_slug(what, slug) _topic_dict = await cache_by_slug(what, slug)
elif what == "COMMUNITY": elif what == "COMMUNITY":
# FIXME: when more communities with local_session() as session:
follows = local_session().execute(select(Community)) follows = session.execute(select(Community))
elif what == "SHOUT": elif what == "SHOUT":
error = reactions_follow(follower_id, slug) error = reactions_follow(follower_id, slug)
@ -122,7 +122,8 @@ async def unfollow(_, info, what, slug):
_topic_dict = await cache_by_slug(what, slug) _topic_dict = await cache_by_slug(what, slug)
elif what == "COMMUNITY": elif what == "COMMUNITY":
follows = local_session().execute(select(Community)) with local_session() as session:
follows = session.execute(select(Community))
elif what == "SHOUT": elif what == "SHOUT":
error = reactions_unfollow(follower_id, slug) error = reactions_unfollow(follower_id, slug)

View File

@ -52,13 +52,13 @@ def get_topic_shouts_stat(topic_id: int):
) )
) )
) )
result = local_session().execute(q).first() with local_session() as session:
result = session.execute(q).first()
return result[0] if result else 0 return result[0] if result else 0
def get_topic_authors_stat(topic_id: int): def get_topic_authors_stat(topic_id: int):
# authors count_query = (
q = (
select(func.count(distinct(ShoutAuthor.author))) select(func.count(distinct(ShoutAuthor.author)))
.select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id)) .select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id))
.join(ShoutAuthor, ShoutAuthor.shout == Shout.id) .join(ShoutAuthor, ShoutAuthor.shout == Shout.id)
@ -70,7 +70,10 @@ def get_topic_authors_stat(topic_id: int):
) )
) )
) )
result = local_session().execute(q).first()
# Выполняем запрос и получаем результат
with local_session() as session:
result = session.execute(count_query).first()
return result[0] if result else 0 return result[0] if result else 0
@ -79,7 +82,8 @@ def get_topic_followers_stat(topic_id: int):
q = select(func.count(distinct(aliased_followers.follower))).filter( q = select(func.count(distinct(aliased_followers.follower))).filter(
aliased_followers.topic == topic_id aliased_followers.topic == topic_id
) )
result = local_session().execute(q).first() with local_session() as session:
result = session.execute(q).first()
return result[0] if result else 0 return result[0] if result else 0
@ -106,8 +110,8 @@ def get_topic_comments_stat(topic_id: int):
ShoutTopic.topic == topic_id ShoutTopic.topic == topic_id
) )
q = q.outerjoin(sub_comments, ShoutTopic.shout == sub_comments.c.shout_id) q = q.outerjoin(sub_comments, ShoutTopic.shout == sub_comments.c.shout_id)
with local_session() as session:
result = local_session().execute(q).first() result = session.execute(q).first()
return result[0] if result else 0 return result[0] if result else 0
@ -141,7 +145,8 @@ def get_author_authors_stat(author_id: int):
aliased_authors.author != author_id, aliased_authors.author != author_id,
) )
) )
result = local_session().execute(q).first() with local_session() as session:
result = session.execute(q).first()
return result[0] if result else 0 return result[0] if result else 0
@ -150,7 +155,8 @@ def get_author_followers_stat(author_id: int):
q = select(func.count(distinct(aliased_followers.follower))).filter( q = select(func.count(distinct(aliased_followers.follower))).filter(
aliased_followers.author == author_id aliased_followers.author == author_id
) )
result = local_session().execute(q).first() with local_session() as session:
result = session.execute(q).first()
return result[0] if result else 0 return result[0] if result else 0
@ -172,8 +178,8 @@ def get_author_comments_stat(author_id: int):
.subquery() .subquery()
) )
q = select(sub_comments.c.comments_count).filter(sub_comments.c.id == author_id) q = select(sub_comments.c.comments_count).filter(sub_comments.c.id == author_id)
with local_session() as session:
result = local_session().execute(q).first() result = session.execute(q).first()
return result[0] if result else 0 return result[0] if result else 0