|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Doppar\Queue\Commands; |
| 4 | + |
| 5 | +use Phaseolies\Console\Schedule\Command; |
| 6 | +use Doppar\Queue\QueueManager; |
| 7 | +use Doppar\Queue\Models\FailedJob; |
| 8 | + |
| 9 | +class QueueFailedCommand extends Command |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $name = 'queue:failed'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $description = 'List all failed jobs'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Execute the console command |
| 27 | + * Example: php pool queue:failed |
| 28 | + * |
| 29 | + * @return int |
| 30 | + */ |
| 31 | + protected function handle(): int |
| 32 | + { |
| 33 | + $failedJobs = FailedJob::orderBy('failed_at', 'desc')->get(); |
| 34 | + |
| 35 | + if ($failedJobs->isEmpty()) { |
| 36 | + $this->info("No failed jobs found."); |
| 37 | + return Command::SUCCESS; |
| 38 | + } |
| 39 | + |
| 40 | + // Create table |
| 41 | + $table = $this->createTable(); |
| 42 | + $table->setHeaders(['ID', 'Job', 'Queue', 'Failed At']); |
| 43 | + |
| 44 | + foreach ($failedJobs as $job) { |
| 45 | + $payload = $job->payload; |
| 46 | + $data = unserialize($payload); |
| 47 | + |
| 48 | + $jobClass = null; |
| 49 | + if ($data && isset($data['job']) && is_object($data['job'])) { |
| 50 | + $jobClass = get_class($data['job']); |
| 51 | + } |
| 52 | + |
| 53 | + $failedAt = date('Y-m-d H:i:s', $job->failed_at); |
| 54 | + $table->addRow([ |
| 55 | + $job->id, |
| 56 | + $jobClass, |
| 57 | + $job->queue, |
| 58 | + $failedAt |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + // Render table |
| 63 | + $table->render(); |
| 64 | + |
| 65 | + return Command::SUCCESS; |
| 66 | + } |
| 67 | +} |
0 commit comments