Skip to content

Commit

Permalink
prevent out of memory exception backfilling awards
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed Jan 30, 2025
1 parent 3a3026a commit f9da01f
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions app/Platform/Actions/UpdateDeveloperContributionYieldAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,37 @@ private function backfillMissingBadges(User $user, int $type, int $lastAwardedTi
$tier = 0;
$nextThreshold = PlayerBadge::getBadgeThreshold($type, $tier);

foreach ($unlocks->get() as $unlock) {
if ($type == AwardType::AchievementPointsYield) {
$total += $unlock->Points;
} else {
$total++;
}

while ($total >= $nextThreshold) {
if ($tier > $lastAwardedTier) {
PlayerBadge::create([
'user_id' => $user->id,
'AwardType' => $type,
'AwardData' => $tier,
'AwardDate' => $unlock->unlocked_at,
'DisplayOrder' => $displayOrder,
]);
$unlocks->chunk(10000, function ($rows) use ($user, $type, $lastAwardedTier, $newTier, $displayOrder, $total, $tier, $nextThreshold) {
foreach ($rows as $unlock) {
if ($type == AwardType::AchievementPointsYield) {
$total += $unlock->Points;
} else {
$total++;
}

$tier++;
if ($tier == $newTier) {
return;
}

$nextThreshold = PlayerBadge::getBadgeThreshold($type, $tier);
if ($nextThreshold < $total) {
// unexpected. bail
return;
while ($total >= $nextThreshold) {
if ($tier > $lastAwardedTier) {
PlayerBadge::create([
'user_id' => $user->id,
'AwardType' => $type,
'AwardData' => $tier,
'AwardDate' => $unlock->unlocked_at,
'DisplayOrder' => $displayOrder,
]);
}

$tier++;
if ($tier == $newTier) {
return;
}

$nextThreshold = PlayerBadge::getBadgeThreshold($type, $tier);
if ($nextThreshold < $total) {
// unexpected. bail
return;
}
}
}
}
});
}
}

0 comments on commit f9da01f

Please sign in to comment.