如何监控 Laravel 队列是否为 运行?

How can I monitor if the Laravel queue is running?

我可以这样启动队列:

php artisan queue:listen

这工作正常,但我想监视队列是否 运行ning,这一点尤其重要,因为如果不是,似乎没有回退。

明确地说,如果我通过控制器对电子邮件进行排队,如下所示:

$this->mailer->queue($view, $data, function ($message) use ($toEmail, $toName, $subject) {
    $message
        ->to($toEmail, $toName)
        ->subject($subject);
    });

这将成功 运行,但如果队列不是 'listening',作业将被永远推送到作业 table。

我正在寻找类似 \Queue::isListening();

的内容

实际上,当队列失败时会触发 failing 事件,例如,您可以在 AppServiceProvider class 中注册 failing 事件 boot 方法使用这样的东西:

public function boot()
{
    Queue::failing(function (JobFailed $event) {
        // $event->connectionName
        // $event->job
        // $event->data
    });
}

或者,您可以在 handler class 中声明一个 failed 方法,例如:

class SendEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    public function handle(Mailer $mailer)
    {
        //...
    }


    public function failed()
    {
        //...
    }
}

The Updated link.

关于后台监控,可以使用Supervisord:

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

在这种情况下,您必须在您的计算机上安装它并使用至少一个 program 部分对其进行配置,例如:

[program:queue]
command=/usr/local/bin/php artisan queue:listen --tries=3

这是 program 部分的示例,我曾使用 Supervisord 监控我的 queue。在这种情况下,您需要阅读 supervisord 的文档以了解如何使用它,我只是给了您一个想法。一旦你启动它,supervisord 将在后台 运行 并且即使在服务器重新启动后它也会重新开始观察(如果它由于某种原因出现故障)所以你不必担心关于那个。

一个简单的(最小的)配置文件可能看起来像这样:

[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/home/someDirName/www/example.com/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/tmp/supervisord.pid
nodaemon=false
loglevel=warn

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock

[program:queue]
command=/usr/local/bin/php artisan queue:listen --tries=3
directory=/home/someDirName/www/example.com
autostart=true
autorestart=true
redirect_stderr=true

好吧,您可以阅读文档以真正了解它。这可能会帮助您开始。

没有你说的方法

\Queue::isListening();

但您可以配置 Supervisor:一个过程控制系统来管理您的队列。这是主管配置的 Laravel 文档 https://laravel.com/docs/5.1/queues#supervisor-configuration

这是较低级别,但同样地,您可以 运行 诸如 ps -aux | grep queue 之类的命令,从字面上查看您 [=13= 的任何服务器上的 运行ning 队列进程] 工作人员 运行 正在工作。

Laravel 8.53 引入了一个新的 artisan 命令来监控队列:

php artisan queue:monitor redis:default

https://laravel.com/docs/8.x/queues#monitoring-your-queues