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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32794547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:24:34  来源:igfitidea点击:

How can I monitor if the Laravel queue is running?

laravellaravel-5.1

提问by Chris

I can start the queue as such:

我可以这样启动队列:

php artisan queue:listen

This works fine, but I would like to monitor if the queue is running, especially important as there doesn't seem to a fallback if it's not.

这工作正常,但我想监视队列是否正在运行,这尤其重要,因为如果不是,则似乎没有后备。

To be clear, if I queue an email through a controller, like so:

需要明确的是,如果我通过控制器将电子邮件排队,如下所示:

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

This will successfully run, but if the queue is not 'listening', the job gets pushed on to the job table, forever.

这将成功运行,但如果队列不是“侦听”,则作业将永远推送到作业表中。

I am looking for something like \Queue::isListening();

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

回答by The Alpha

Actually, when a queue fails a failingevent fires, so for example, you may register the failingevent in your AppServiceProviderclass in the bootmethod using something like this:

实际上,当队列失败时failing会触发一个事件,例如,您可以使用以下方法failingAppServiceProvider类中的boot方法中注册该事件:

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

Alternatively, you can declare a failedmethod in the handlerclass for example:

或者,您可以failedhandler类中声明一个方法,例如:

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

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


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

The Updated link.

更新的链接

Regarding background monitoring, you may use Supervisord:

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

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

Supervisor 是一个客户端/服务器系统,允许其用户监视和控制类 UNIX 操作系统上的许多进程。

In this case, you have to install it on your machine and configure it using at least one programsection, for example:

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

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

This is an example of programsection that I've used to monitor my queueusing Supervisord. In this case, you need to read the documentation for the supervisordto understand how to use it, I've just gave you an idea. The supervisordwill run in the background once you start it and it'll also restart the observation even after the server is restarted (if it goes down for some reason) so you don't need to worry about that.

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

A simple (minimal) config file may look something like this:

一个简单(最小)的配置文件可能如下所示:

[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

Well, you may read the documentation to really get the clear idea about it. This may help you to start.

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

回答by jmcgrory

This is lower level but in the same vein you could run a command such as ps -aux | grep queueto literally view the running queue processes on whatever server your application/queue workers are running on.

这是较低级别的,但同样,您可以运行命令,例如ps -aux | grep queue在您的应用程序/队列工作人员运行的任何服务器上查看正在运行的队列进程。

回答by Abu Sayem

There is no method like you say

没有你说的方法

\Queue::isListening();

\Queue::isListening();

But you can configure Supervisor: A process control system to look after your queue. here is a the Laravel documentation for supervisor configuation https://laravel.com/docs/5.1/queues#supervisor-configuration

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