Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Queue\Console;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Arr;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
 
9
#[AsCommand(name: 'queue:failed')]
10
class ListFailedCommand extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'queue:failed';
18
 
19
    /**
20
     * The name of the console command.
21
     *
22
     * This name is used to identify the command during lazy loading.
23
     *
24
     * @var string|null
25
     *
26
     * @deprecated
27
     */
28
    protected static $defaultName = 'queue:failed';
29
 
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'List all of the failed queue jobs';
36
 
37
    /**
38
     * The table headers for the command.
39
     *
40
     * @var string[]
41
     */
42
    protected $headers = ['ID', 'Connection', 'Queue', 'Class', 'Failed At'];
43
 
44
    /**
45
     * Execute the console command.
46
     *
47
     * @return void
48
     */
49
    public function handle()
50
    {
51
        if (count($jobs = $this->getFailedJobs()) === 0) {
52
            return $this->components->info('No failed jobs found.');
53
        }
54
 
55
        $this->newLine();
56
        $this->displayFailedJobs($jobs);
57
        $this->newLine();
58
    }
59
 
60
    /**
61
     * Compile the failed jobs into a displayable format.
62
     *
63
     * @return array
64
     */
65
    protected function getFailedJobs()
66
    {
67
        $failed = $this->laravel['queue.failer']->all();
68
 
69
        return collect($failed)->map(function ($failed) {
70
            return $this->parseFailedJob((array) $failed);
71
        })->filter()->all();
72
    }
73
 
74
    /**
75
     * Parse the failed job row.
76
     *
77
     * @param  array  $failed
78
     * @return array
79
     */
80
    protected function parseFailedJob(array $failed)
81
    {
82
        $row = array_values(Arr::except($failed, ['payload', 'exception']));
83
 
84
        array_splice($row, 3, 0, $this->extractJobName($failed['payload']) ?: '');
85
 
86
        return $row;
87
    }
88
 
89
    /**
90
     * Extract the failed job name from payload.
91
     *
92
     * @param  string  $payload
93
     * @return string|null
94
     */
95
    private function extractJobName($payload)
96
    {
97
        $payload = json_decode($payload, true);
98
 
99
        if ($payload && (! isset($payload['data']['command']))) {
100
            return $payload['job'] ?? null;
101
        } elseif ($payload && isset($payload['data']['command'])) {
102
            return $this->matchJobName($payload);
103
        }
104
    }
105
 
106
    /**
107
     * Match the job name from the payload.
108
     *
109
     * @param  array  $payload
110
     * @return string|null
111
     */
112
    protected function matchJobName($payload)
113
    {
114
        preg_match('/"([^"]+)"/', $payload['data']['command'], $matches);
115
 
116
        return $matches[1] ?? $payload['job'] ?? null;
117
    }
118
 
119
    /**
120
     * Display the failed jobs in the console.
121
     *
122
     * @param  array  $jobs
123
     * @return void
124
     */
125
    protected function displayFailedJobs(array $jobs)
126
    {
127
        collect($jobs)->each(
128
            fn ($job) => $this->components->twoColumnDetail(
129
                sprintf('<fg=gray>%s</> %s</>', $job[4], $job[0]),
130
                sprintf('<fg=gray>%s@%s</> %s', $job[1], $job[2], $job[3])
131
            ),
132
        );
133
    }
134
}