Skip to content

Commit 5ae4260

Browse files
authored
Merge pull request #5 from doppar/commands
queue:faild command to list all failed jobs
2 parents 435d785 + b4400de commit 5ae4260

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

src/QueueServiceProvider.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Doppar\Queue;
44

5-
use Doppar\Queue\Commands\MakeJobCommand;
6-
use Doppar\Queue\Commands\QueueFlushCommand;
75
use Phaseolies\Providers\ServiceProvider;
86
use Doppar\Queue\QueueManager;
97
use Doppar\Queue\Commands\QueueRunCommand;
108
use Doppar\Queue\Commands\QueueRetryCommand;
9+
use Doppar\Queue\Commands\QueueFlushCommand;
10+
use Doppar\Queue\Commands\QueueFailedCommand;
11+
use Doppar\Queue\Commands\MakeJobCommand;
1112

1213
class QueueServiceProvider extends ServiceProvider
1314
{
@@ -38,7 +39,8 @@ public function boot(): void
3839
QueueRunCommand::class,
3940
QueueRetryCommand::class,
4041
MakeJobCommand::class,
41-
QueueFlushCommand::class
42+
QueueFlushCommand::class,
43+
QueueFailedCommand::class
4244
]);
4345
}
4446
}

0 commit comments

Comments
 (0)