Skip to content

Commit

Permalink
clear cache if update_active_users hits IntegrityErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
rawxfish committed Dec 21, 2024
1 parent ebe8c4f commit 9695834
Showing 1 changed file with 50 additions and 41 deletions.
91 changes: 50 additions & 41 deletions google_api_lib/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.db import transaction
from django.db import transaction, IntegrityError
from django.db.models import CharField, F, Value
from django.db.models.functions import Concat
from googleapiclient import _auth
Expand Down Expand Up @@ -620,51 +620,60 @@ def update_active_users(self, hunt_id):
else:
body["pageToken"] = response["nextPageToken"]

with transaction.atomic():
user_puzzle_pairs = [
f"{user_pk}-{puzzle_pk}" for (user_pk, puzzle_pk) in latest.keys()
]
old_activities = (
PuzzleActivity.objects.annotate(
user_puzzle_pair=Concat(
F("user_id"), Value("-"), F("puzzle_id"), output_field=CharField()
try:
with transaction.atomic():
user_puzzle_pairs = [
f"{user_pk}-{puzzle_pk}" for (user_pk, puzzle_pk) in latest.keys()
]
old_activities = (
PuzzleActivity.objects.annotate(
user_puzzle_pair=Concat(
F("user_id"),
Value("-"),
F("puzzle_id"),
output_field=CharField(),
)
)
.filter(user_puzzle_pair__in=user_puzzle_pairs)
.select_for_update()
)
old_keys = [(e.user_id, e.puzzle_id) for e in old_activities]

PuzzleActivity.objects.bulk_create(
[
PuzzleActivity(
puzzle_id=puzzle_pk,
user_id=user_pk,
last_edit_time=last_edit_time,
num_edits=num_edits,
)
for (
(user_pk, puzzle_pk),
(last_edit_time, num_edits),
) in latest.items()
if (user_pk, puzzle_pk) not in old_keys
],
)
.filter(user_puzzle_pair__in=user_puzzle_pairs)
.select_for_update()
)
old_keys = [(e.user_id, e.puzzle_id) for e in old_activities]

PuzzleActivity.objects.bulk_create(
[
PuzzleActivity(
puzzle_id=puzzle_pk,
user_id=user_pk,
last_edit_time=last_edit_time,
num_edits=num_edits,
)
for (
(user_pk, puzzle_pk),
(last_edit_time, num_edits),
) in latest.items()
if (user_pk, puzzle_pk) not in old_keys
],
)

updates = []
for activity in old_activities:
(last_edit_time, num_edits) = latest[(activity.user_id, activity.puzzle_id)]
activity.last_edit_time = last_edit_time
activity.num_edits += num_edits
updates.append(activity)
updates = []
for activity in old_activities:
(last_edit_time, num_edits) = latest[
(activity.user_id, activity.puzzle_id)
]
activity.last_edit_time = last_edit_time
activity.num_edits += num_edits
updates.append(activity)

if updates:
PuzzleActivity.objects.bulk_update(
updates, fields=["last_edit_time", "num_edits"]
)
if updates:
PuzzleActivity.objects.bulk_update(
updates, fields=["last_edit_time", "num_edits"]
)

hunt.last_active_users_update_time = end
hunt.save()
hunt.last_active_users_update_time = end
hunt.save()
except IntegrityError:
# attempt to avoid the error next time by clearing cache
cache.clear()


@shared_task(base=GoogleApiClientTask, bind=True)
Expand Down

0 comments on commit 9695834

Please sign in to comment.