-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListFailedCommand.php
More file actions
207 lines (163 loc) · 6.34 KB
/
ListFailedCommand.php
File metadata and controls
207 lines (163 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Queuewatch\Laravel\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
class ListFailedCommand extends Command
{
protected $name = 'queue:failed';
protected $description = 'List all of the failed queue jobs';
public function handle(): int
{
$failer = $this->laravel['queue.failer'];
if (! method_exists($failer, 'all')) {
$this->error('Queue failer does not support listing all failed jobs.');
return 1;
}
$jobs = collect($failer->all());
if ($jobs->isEmpty()) {
if ($this->option('json')) {
$this->line(json_encode([
'failed_jobs' => [],
'count' => 0,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
return 0;
}
$this->info('No failed jobs!');
return 0;
}
$jobs = $this->applyFilters($jobs);
if ($jobs->isEmpty()) {
if ($this->option('json')) {
$this->line(json_encode([
'failed_jobs' => [],
'count' => 0,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
return 0;
}
$this->info('No failed jobs match the given criteria.');
return 0;
}
if ($limit = $this->option('limit')) {
$jobs = $jobs->take((int) $limit);
}
if ($this->option('json')) {
return $this->outputJson($jobs);
}
return $this->displayTable($jobs);
}
protected function applyFilters($jobs)
{
if ($queue = $this->option('queue')) {
$jobs = $jobs->filter(fn ($job) => $job->queue === $queue);
}
if ($connection = $this->option('connection')) {
$jobs = $jobs->filter(fn ($job) => $job->connection === $connection);
}
if ($after = $this->option('after')) {
try {
$afterDate = Carbon::parse($after);
$jobs = $jobs->filter(function ($job) use ($afterDate) {
$failedAt = $job->failed_at instanceof Carbon
? $job->failed_at
: Carbon::parse($job->failed_at);
return $failedAt->gte($afterDate);
});
} catch (\Exception $e) {
$this->error("Invalid date format for --after: {$after}");
return collect();
}
}
if ($before = $this->option('before')) {
try {
$beforeDate = Carbon::parse($before);
$jobs = $jobs->filter(function ($job) use ($beforeDate) {
$failedAt = $job->failed_at instanceof Carbon
? $job->failed_at
: Carbon::parse($job->failed_at);
return $failedAt->lte($beforeDate);
});
} catch (\Exception $e) {
$this->error("Invalid date format for --before: {$before}");
return collect();
}
}
if ($class = $this->option('class')) {
$jobs = $jobs->filter(function ($job) use ($class) {
$payload = json_decode($job->payload ?? '{}', true);
$jobClass = $payload['displayName'] ?? $payload['job'] ?? null;
return $jobClass && str_contains($jobClass, $class);
});
}
return $jobs;
}
protected function outputJson($jobs): int
{
$output = [
'failed_jobs' => $jobs->map(function ($job) {
return [
'id' => $job->id,
'uuid' => $job->uuid ?? null,
'connection' => $job->connection,
'queue' => $job->queue,
'payload' => json_decode($job->payload ?? '{}', true),
'exception' => $job->exception,
'failed_at' => $this->formatFailedAt($job->failed_at),
];
})->values()->all(),
'count' => $jobs->count(),
];
$this->line(json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
return 0;
}
protected function displayTable($jobs): int
{
$this->table(
['ID', 'Connection', 'Queue', 'Class', 'Failed At'],
$jobs->map(function ($job) {
$payload = json_decode($job->payload ?? '{}', true);
return [
'id' => $job->id,
'connection' => $job->connection,
'queue' => $job->queue,
'class' => $this->formatJobClass($payload['displayName'] ?? $payload['job'] ?? 'N/A'),
'failed_at' => $this->formatFailedAt($job->failed_at),
];
})->all()
);
$this->newLine();
$this->info("Total failed jobs: {$jobs->count()}");
return 0;
}
protected function formatJobClass($class): string
{
if (empty($class) || $class === 'N/A') {
return 'N/A';
}
$parts = explode('\\', $class);
return end($parts);
}
protected function formatFailedAt($failedAt): string
{
if ($failedAt instanceof Carbon) {
return $failedAt->toDateTimeString();
}
try {
return Carbon::parse($failedAt)->toDateTimeString();
} catch (\Exception $e) {
return (string) $failedAt;
}
}
protected function getOptions(): array
{
return [
['json', null, InputOption::VALUE_NONE, 'Output the failed jobs as JSON'],
['queue', null, InputOption::VALUE_OPTIONAL, 'Filter by queue name'],
['connection', null, InputOption::VALUE_OPTIONAL, 'Filter by connection name'],
['after', null, InputOption::VALUE_OPTIONAL, 'Show jobs failed after date (e.g., "2025-11-20" or "yesterday")'],
['before', null, InputOption::VALUE_OPTIONAL, 'Show jobs failed before date (e.g., "2025-11-21" or "today")'],
['class', null, InputOption::VALUE_OPTIONAL, 'Filter by job class name (partial match supported)'],
['limit', null, InputOption::VALUE_OPTIONAL, 'Limit the number of results'],
];
}
}