remove auto-flush to reduce average lock duration

pull/195/head
Aevann 2023-08-20 05:00:35 +03:00
parent e0a6023c9a
commit 4993c3a889
6 changed files with 23 additions and 20 deletions

View File

@ -1156,4 +1156,4 @@ from sqlalchemy.engine.create import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(environ.get("DATABASE_URL").strip(), connect_args={"options": "-c statement_timeout=5000 -c idle_in_transaction_session_timeout=40000"})
db_session = scoped_session(sessionmaker(bind=engine))
db_session = scoped_session(sessionmaker(bind=engine, autoflush=False))

View File

@ -154,30 +154,30 @@ def approve_emoji(v, name):
if emoji.kind == "Marsey":
all_by_author = g.db.query(Emoji).filter_by(kind="Marsey", author_id=author.id).count()
if all_by_author >= 100:
if all_by_author >= 99:
badge_grant(badge_id=143, user=author)
elif all_by_author >= 10:
elif all_by_author >= 9:
badge_grant(badge_id=16, user=author)
else:
badge_grant(badge_id=17, user=author)
elif emoji.kind == "Capy":
all_by_author = g.db.query(Emoji).filter_by(kind="Capy", author_id=author.id).count()
if all_by_author >= 10:
if all_by_author >= 9:
badge_grant(badge_id=115, user=author)
badge_grant(badge_id=114, user=author)
elif emoji.kind == "Carp":
all_by_author = g.db.query(Emoji).filter_by(kind="Carp", author_id=author.id).count()
if all_by_author >= 10:
if all_by_author >= 9:
badge_grant(badge_id=288, user=author)
badge_grant(badge_id=287, user=author)
elif emoji.kind == "Wolf":
all_by_author = g.db.query(Emoji).filter_by(kind="Wolf", author_id=author.id).count()
if all_by_author >= 10:
if all_by_author >= 9:
badge_grant(badge_id=111, user=author)
badge_grant(badge_id=110, user=author)
elif emoji.kind == "Platy":
all_by_author = g.db.query(Emoji).filter_by(kind="Platy", author_id=author.id).count()
if all_by_author >= 10:
if all_by_author >= 9:
badge_grant(badge_id=113, user=author)
badge_grant(badge_id=112, user=author)
@ -353,13 +353,13 @@ def approve_hat(v, name):
all_by_author = g.db.query(HatDef).filter_by(author_id=author.id).count()
if all_by_author >= 250:
if all_by_author >= 249:
badge_grant(badge_id=166, user=author)
elif all_by_author >= 100:
elif all_by_author >= 99:
badge_grant(badge_id=165, user=author)
elif all_by_author >= 50:
elif all_by_author >= 49:
badge_grant(badge_id=164, user=author)
elif all_by_author >= 10:
elif all_by_author >= 9:
badge_grant(badge_id=163, user=author)
hat_copy = Hat(

View File

@ -106,11 +106,11 @@ def buy_hat(v, hat_id):
f":marseycapitalistmanlet: @{v.username} has just bought `{hat.name}`, you have received your 10% cut ({int(hat.price * 0.1)} coins) :!marseycapitalistmanlet:"
)
if v.num_of_owned_hats >= 250:
if v.num_of_owned_hats >= 249:
badge_grant(user=v, badge_id=154)
elif v.num_of_owned_hats >= 100:
elif v.num_of_owned_hats >= 99:
badge_grant(user=v, badge_id=153)
elif v.num_of_owned_hats >= 25:
elif v.num_of_owned_hats >= 24:
badge_grant(user=v, badge_id=152)
return {"message": f"'{hat.name}' bought!"}

View File

@ -657,7 +657,7 @@ def submit_post(v, sub=None):
autojanny.comment_count += 1
g.db.add(autojanny)
v.post_count = g.db.query(Post).filter_by(author_id=v.id, deleted_utc=0).count()
v.post_count += 1
g.db.add(v)
execute_lawlz_actions(v, p)
@ -708,7 +708,7 @@ def delete_post_pid(pid, v):
cache.delete_memoized(frontlist)
cache.delete_memoized(userpagelisting)
v.post_count = g.db.query(Post).filter_by(author_id=v.id, deleted_utc=0).count()
v.post_count -= 1
g.db.add(v)
for sort in COMMENT_SORTS:
@ -733,7 +733,7 @@ def undelete_post_pid(pid, v):
cache.delete_memoized(frontlist)
cache.delete_memoized(userpagelisting)
v.post_count = g.db.query(Post).filter_by(author_id=v.id, deleted_utc=0).count()
v.post_count += 1
g.db.add(v)
for sort in COMMENT_SORTS:

View File

@ -42,6 +42,7 @@ def claim_rewards_all_users():
emails = [x[0] for x in g.db.query(Transaction.email).filter_by(claimed=None)]
users = g.db.query(User).filter(User.email.in_(emails)).order_by(User.truescore.desc()).all()
for user in users:
g.db.flush()
transactions = g.db.query(Transaction).filter_by(email=user.email, claimed=None).all()
highest_tier = 0
@ -81,6 +82,7 @@ def claim_rewards_all_users():
for x in range(22, badge_id+1):
badge_grant(badge_id=x, user=user)
g.db.flush()
user.lifetimedonated = g.db.query(func.sum(Transaction.amount)).filter_by(email=user.email).scalar()
if user.lifetimedonated >= 100:
@ -1205,7 +1207,7 @@ def follow_user(username, v):
new_follow = Follow(user_id=v.id, target_id=target.id)
g.db.add(new_follow)
target.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=target.id).count()
target.stored_subscriber_count += 1
g.db.add(target)
if not v.shadowbanned:
@ -1229,7 +1231,7 @@ def unfollow_user(username, v):
if follow:
g.db.delete(follow)
target.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=target.id).count()
target.stored_subscriber_count -= 1
g.db.add(target)
if not v.shadowbanned:
@ -1256,7 +1258,7 @@ def remove_follow(username, v):
g.db.delete(follow)
v.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=v.id).count()
v.stored_subscriber_count -= 1
g.db.add(v)
send_repeatable_notification(target.id, f"@{v.username} has removed your follow!")

View File

@ -96,6 +96,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls):
# this is hacky but it works, we should probably do better later
def get_vote_count(dir, real_instead_of_dir):
g.db.flush()
votes = g.db.query(vote_cls)
if real_instead_of_dir:
votes = votes.filter(vote_cls.real == True)