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\Contracts\Events\Dispatcher;
7
use Illuminate\Contracts\Queue\Factory;
8
use Illuminate\Queue\Events\QueueBusy;
9
use Illuminate\Support\Collection;
10
use Symfony\Component\Console\Attribute\AsCommand;
11
 
12
#[AsCommand(name: 'queue:monitor')]
13
class MonitorCommand extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'queue:monitor
21
                       {queues : The names of the queues to monitor}
22
                       {--max=1000 : The maximum number of jobs that can be on the queue before an event is dispatched}';
23
 
24
    /**
25
     * The name of the console command.
26
     *
27
     * This name is used to identify the command during lazy loading.
28
     *
29
     * @var string|null
30
     *
31
     * @deprecated
32
     */
33
    protected static $defaultName = 'queue:monitor';
34
 
35
    /**
36
     * The console command description.
37
     *
38
     * @var string
39
     */
40
    protected $description = 'Monitor the size of the specified queues';
41
 
42
    /**
43
     * The queue manager instance.
44
     *
45
     * @var \Illuminate\Contracts\Queue\Factory
46
     */
47
    protected $manager;
48
 
49
    /**
50
     * The events dispatcher instance.
51
     *
52
     * @var \Illuminate\Contracts\Events\Dispatcher
53
     */
54
    protected $events;
55
 
56
    /**
57
     * The table headers for the command.
58
     *
59
     * @var string[]
60
     */
61
    protected $headers = ['Connection', 'Queue', 'Size', 'Status'];
62
 
63
    /**
64
     * Create a new queue monitor command.
65
     *
66
     * @param  \Illuminate\Contracts\Queue\Factory  $manager
67
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
68
     * @return void
69
     */
70
    public function __construct(Factory $manager, Dispatcher $events)
71
    {
72
        parent::__construct();
73
 
74
        $this->manager = $manager;
75
        $this->events = $events;
76
    }
77
 
78
    /**
79
     * Execute the console command.
80
     *
81
     * @return void
82
     */
83
    public function handle()
84
    {
85
        $queues = $this->parseQueues($this->argument('queues'));
86
 
87
        $this->displaySizes($queues);
88
 
89
        $this->dispatchEvents($queues);
90
    }
91
 
92
    /**
93
     * Parse the queues into an array of the connections and queues.
94
     *
95
     * @param  string  $queues
96
     * @return \Illuminate\Support\Collection
97
     */
98
    protected function parseQueues($queues)
99
    {
100
        return collect(explode(',', $queues))->map(function ($queue) {
101
            [$connection, $queue] = array_pad(explode(':', $queue, 2), 2, null);
102
 
103
            if (! isset($queue)) {
104
                $queue = $connection;
105
                $connection = $this->laravel['config']['queue.default'];
106
            }
107
 
108
            return [
109
                'connection' => $connection,
110
                'queue' => $queue,
111
                'size' => $size = $this->manager->connection($connection)->size($queue),
112
                'status' => $size >= $this->option('max') ? '<fg=yellow;options=bold>ALERT</>' : '<fg=green;options=bold>OK</>',
113
            ];
114
        });
115
    }
116
 
117
    /**
118
     * Display the queue sizes in the console.
119
     *
120
     * @param  \Illuminate\Support\Collection  $queues
121
     * @return void
122
     */
123
    protected function displaySizes(Collection $queues)
124
    {
125
        $this->newLine();
126
 
127
        $this->components->twoColumnDetail('<fg=gray>Queue name</>', '<fg=gray>Size / Status</>');
128
 
129
        $queues->each(function ($queue) {
130
            $status = '['.$queue['size'].'] '.$queue['status'];
131
 
132
            $this->components->twoColumnDetail($queue['queue'], $status);
133
        });
134
 
135
        $this->newLine();
136
    }
137
 
138
    /**
139
     * Fire the monitoring events.
140
     *
141
     * @param  \Illuminate\Support\Collection  $queues
142
     * @return void
143
     */
144
    protected function dispatchEvents(Collection $queues)
145
    {
146
        foreach ($queues as $queue) {
147
            if ($queue['status'] == '<fg=green;options=bold>OK</>') {
148
                continue;
149
            }
150
 
151
            $this->events->dispatch(
152
                new QueueBusy(
153
                    $queue['connection'],
154
                    $queue['queue'],
155
                    $queue['size'],
156
                )
157
            );
158
        }
159
    }
160
}