|
15 | 15 | use Illuminate\Support\InteractsWithTime; |
16 | 16 | use Symfony\Component\Console\Attribute\AsCommand; |
17 | 17 | use Symfony\Component\Console\Terminal; |
| 18 | +use Throwable; |
18 | 19 |
|
19 | 20 | use function Termwind\terminal; |
20 | 21 |
|
@@ -44,7 +45,8 @@ class WorkCommand extends Command |
44 | 45 | {--sleep=3 : Number of seconds to sleep when no job is available} |
45 | 46 | {--rest=0 : Number of seconds to rest between jobs} |
46 | 47 | {--timeout=60 : The number of seconds a child process can run} |
47 | | - {--tries=1 : Number of times to attempt a job before logging it failed}'; |
| 48 | + {--tries=1 : Number of times to attempt a job before logging it failed} |
| 49 | + {--json : Output the queue worker information as JSON}'; |
48 | 50 |
|
49 | 51 | /** |
50 | 52 | * The console command description. |
@@ -113,7 +115,7 @@ public function handle() |
113 | 115 | // connection being run for the queue operation currently being executed. |
114 | 116 | $queue = $this->getQueue($connection); |
115 | 117 |
|
116 | | - if (Terminal::hasSttyAvailable()) { |
| 118 | + if (! $this->option('json') && Terminal::hasSttyAvailable()) { |
117 | 119 | $this->components->info( |
118 | 120 | sprintf('Processing jobs from the [%s] %s.', $queue, str('queue')->plural(explode(',', $queue))) |
119 | 121 | ); |
@@ -183,20 +185,35 @@ protected function listenForEvents() |
183 | 185 | }); |
184 | 186 |
|
185 | 187 | $this->laravel['events']->listen(JobFailed::class, function ($event) { |
186 | | - $this->writeOutput($event->job, 'failed'); |
| 188 | + $this->writeOutput($event->job, 'failed', $event->exception); |
187 | 189 |
|
188 | 190 | $this->logFailedJob($event); |
189 | 191 | }); |
190 | 192 | } |
191 | 193 |
|
| 194 | + /** |
| 195 | + * Write the status output for the queue worker for JSON or TTY. |
| 196 | + * |
| 197 | + * @param Job $job |
| 198 | + * @param string $status |
| 199 | + * @param Throwable|null $exception |
| 200 | + * @return void |
| 201 | + */ |
| 202 | + protected function writeOutput(Job $job, $status, Throwable $exception = null) |
| 203 | + { |
| 204 | + $this->option('json') |
| 205 | + ? $this->writeOutputAsJson($job, $status, $exception) |
| 206 | + : $this->writeOutputForCli($job, $status); |
| 207 | + } |
| 208 | + |
192 | 209 | /** |
193 | 210 | * Write the status output for the queue worker. |
194 | 211 | * |
195 | 212 | * @param \Illuminate\Contracts\Queue\Job $job |
196 | 213 | * @param string $status |
197 | 214 | * @return void |
198 | 215 | */ |
199 | | - protected function writeOutput(Job $job, $status) |
| 216 | + protected function writeOutputForCli(Job $job, $status) |
200 | 217 | { |
201 | 218 | $this->output->write(sprintf( |
202 | 219 | ' <fg=gray>%s</> %s%s', |
@@ -235,6 +252,45 @@ protected function writeOutput(Job $job, $status) |
235 | 252 | }); |
236 | 253 | } |
237 | 254 |
|
| 255 | + /** |
| 256 | + * Write the status output for the queue worker in JSON format. |
| 257 | + * |
| 258 | + * @param \Illuminate\Contracts\Queue\Job $job |
| 259 | + * @param string $status |
| 260 | + * @param Throwable|null $exception |
| 261 | + * @return void |
| 262 | + */ |
| 263 | + protected function writeOutputAsJson(Job $job, $status, Throwable $exception = null) |
| 264 | + { |
| 265 | + $log = array_filter([ |
| 266 | + 'level' => $status === 'starting' || $status === 'success' ? 'info' : 'warning', |
| 267 | + 'id' => $job->getJobId(), |
| 268 | + 'uuid' => $job->uuid(), |
| 269 | + 'connection' => $job->getConnectionName(), |
| 270 | + 'queue' => $job->getQueue(), |
| 271 | + 'job' => $job->resolveName(), |
| 272 | + 'status' => $status, |
| 273 | + 'result' => match (true) { |
| 274 | + $job->isDeleted() => 'deleted', |
| 275 | + $job->isReleased() => 'released', |
| 276 | + $job->hasFailed() => 'failed', |
| 277 | + default => '', |
| 278 | + }, |
| 279 | + 'attempts' => $job->attempts(), |
| 280 | + 'exception' => $exception ? $exception::class : '', |
| 281 | + 'message' => $exception?->getMessage(), |
| 282 | + 'timestamp' => $this->now()->format('Y-m-d\TH:i:s.uP'), |
| 283 | + ]); |
| 284 | + |
| 285 | + if ($status === 'starting') { |
| 286 | + $this->latestStartedAt = microtime(true); |
| 287 | + } else { |
| 288 | + $log['duration'] = round(microtime(true) - $this->latestStartedAt, 6); |
| 289 | + } |
| 290 | + |
| 291 | + $this->output->writeln(json_encode($log)); |
| 292 | + } |
| 293 | + |
238 | 294 | /** |
239 | 295 | * Get the current date / time. |
240 | 296 | * |
|
0 commit comments