Created
July 30, 2024 12:03
-
-
Save crnkovic/3cbde9fd224de2257891d60bd42fef21 to your computer and use it in GitHub Desktop.
Scheduling in chunk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
private function scheduleAppHomeUpdates(Schedule $schedule): void | |
{ | |
$ttl = now()->addMinutes(30); | |
$users = (int) Cache::remember('total-users', $ttl, function () { | |
return User::whereHas( | |
'workspace', fn ($q) => $q->whereNull('locked_at')->whereNotNull('onboarded_at') | |
)->count(); | |
}); | |
$howOften = 10; // run every X minutes | |
$offsetMinute = 5; | |
$chunks = 60 / $howOften; | |
$perJob = ceil(($users + 1) / $chunks); | |
foreach (range(0, $chunks-1) as $chunk) { | |
$at = (int) (round($chunk*$howOften) + $offsetMinute); | |
if ($at >= 60) { | |
$at = $at - 60; | |
} | |
$schedule->job(new SyncAppHomeForChunk( | |
limit: (int) $perJob, | |
skip: (int) ($chunk * $perJob) | |
))->name(sprintf('Syncing, limit: %s, skip: %s', $perJob, $chunk*$perJob))->hourlyAt($at); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment