Created
November 13, 2025 08:38
-
-
Save kura1420/d6c3fd036786151e79a7a3022bdbb1f8 to your computer and use it in GitHub Desktop.
Laravel Queue Bus
This file contains hidden or 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 | |
| namespace App\Console\Commands; | |
| use App\Jobs\SendMessageJob; | |
| use App\Models\User; | |
| use Illuminate\Console\Command; | |
| use Illuminate\Support\Facades\Bus; | |
| class BusQueueCommand extends Command | |
| { | |
| /** | |
| * The name and signature of the console command. | |
| * | |
| * @var string | |
| */ | |
| protected $signature = 'busqueue:cmd'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'Command description'; | |
| /** | |
| * Execute the console command. | |
| */ | |
| public function handle() | |
| { | |
| // Konfigurasi delay | |
| $delayBetweenBatches = 2; // Delay antar batch dalam menit | |
| $delayBetweenJobs = 30; // Delay antar job dalam batch dalam detik | |
| $chunkSize = 2; // Jumlah job per batch | |
| $users = User::where('id', '!=', 1)->limit(10)->get(); | |
| $chunks = $users->chunk($chunkSize); | |
| $this->info("=== KONFIGURASI DELAY ==="); | |
| $this->info("Total users: " . $users->count()); | |
| $this->info("Total batches: " . $chunks->count()); | |
| $this->info("Delay antar batch: {$delayBetweenBatches} menit (" . ($delayBetweenBatches * 60) . " detik)"); | |
| $this->info("Delay antar job: {$delayBetweenJobs} detik"); | |
| $this->info("Waktu mulai: " . now()->format('Y-m-d H:i:s')); | |
| $this->info("=== MEMULAI PENJADWALAN JOB ==="); | |
| $baseTime = now(); // Waktu referensi yang tetap untuk semua perhitungan | |
| foreach ($chunks as $batchIndex => $chunk) { | |
| $jobs = []; | |
| foreach ($chunk as $jobIndex => $user) { | |
| $job = new SendMessageJob("517233778", "Test message to user {$user->id}"); | |
| // Delay bertingkat calculation dengan waktu referensi tetap | |
| $batchDelaySeconds = $batchIndex * $delayBetweenBatches * 60; // Konversi menit ke detik | |
| $jobDelaySeconds = $jobIndex * $delayBetweenJobs; | |
| $totalDelaySeconds = $batchDelaySeconds + $jobDelaySeconds; | |
| // Gunakan waktu referensi yang sama untuk semua job | |
| $scheduledTime = $baseTime->copy()->addSeconds($totalDelaySeconds); | |
| $job->delay($scheduledTime); | |
| $jobs[] = $job; | |
| $executeTime = $scheduledTime->format('Y-m-d H:i:s'); | |
| $this->info("Batch {$batchIndex}, Job {$jobIndex} (User ID: {$user->id}): dijadwalkan pada {$executeTime} (delay: {$totalDelaySeconds}s)"); | |
| } | |
| Bus::batch($jobs)->dispatch(); | |
| $this->info("--- Batch {$batchIndex} berhasil dijadwalkan ---"); | |
| } | |
| $this->info('All batches have been dispatched.'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment