Skip to content

Instantly share code, notes, and snippets.

@crnkovic
Created July 30, 2024 12:03
Show Gist options
  • Save crnkovic/3cbde9fd224de2257891d60bd42fef21 to your computer and use it in GitHub Desktop.
Save crnkovic/3cbde9fd224de2257891d60bd42fef21 to your computer and use it in GitHub Desktop.
Scheduling in chunk
<?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