Skip to content

Commit

Permalink
🐛 fix: reduce concurrency to address issues with updating ContestReco…
Browse files Browse the repository at this point in the history
…rd collection
  • Loading branch information
baoliay2008 committed Mar 11, 2024
1 parent bde9ac3 commit f889c23
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions app/handler/contest_record.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
from datetime import datetime

from beanie.odm.operators.update.general import Set
Expand All @@ -8,7 +7,7 @@
from app.db.models import DATA_REGION, ContestRecordArchive, ContestRecordPredict, User
from app.handler.submission import save_submission
from app.handler.user import save_users_of_contest
from app.utils import exception_logger_reraise
from app.utils import exception_logger_reraise, gather_with_limited_concurrency


@exception_logger_reraise
Expand Down Expand Up @@ -49,19 +48,19 @@ async def _fill_old_rating_and_count(_contest_record: ContestRecordPredict):
contest_record_dict.update({"contest_name": contest_name})
contest_record = ContestRecordPredict.model_validate(contest_record_dict)
contest_records.append(contest_record)
insert_tasks = (
insert_tasks = [
ContestRecordPredict.insert_one(contest_record)
for contest_record in contest_records
)
await asyncio.gather(*insert_tasks)
]
await gather_with_limited_concurrency(insert_tasks, max_con_num=50)
await save_users_of_contest(contest_name=contest_name, predict=True)
# fill rating and attended count, must be called after save_users_of_contest and before predict_contest,
fill_tasks = (
fill_tasks = [
_fill_old_rating_and_count(contest_record)
for contest_record in contest_records
if contest_record.score != 0
)
await asyncio.gather(*fill_tasks)
]
await gather_with_limited_concurrency(fill_tasks, max_con_num=50)


@exception_logger_reraise
Expand All @@ -86,7 +85,7 @@ async def save_archive_contest_records(
contest_record_dict.update({"contest_name": contest_name})
contest_record = ContestRecordArchive.model_validate(contest_record_dict)
contest_records.append(contest_record)
tasks = (
tasks = [
ContestRecordArchive.find_one(
ContestRecordArchive.contest_name == contest_record.contest_name,
ContestRecordArchive.username == contest_record.username,
Expand All @@ -103,8 +102,8 @@ async def save_archive_contest_records(
on_insert=contest_record,
)
for contest_record in contest_records
)
await asyncio.gather(*tasks)
]
await gather_with_limited_concurrency(tasks, max_con_num=50)
# remove old records
await ContestRecordArchive.find(
ContestRecordArchive.contest_name == contest_name,
Expand Down

0 comments on commit f889c23

Please sign in to comment.