Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I monitor if the Laravel queue is running?

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();

like image 732
Chris Avatar asked Sep 26 '15 07:09

Chris


People also ask

How do I know if my laravel queue is working?

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

What is laravel queue sync?

Sync, or synchronous, is the default queue driver which runs a queued job within your existing process. With this driver enabled, you effectively have no queue as the queued job runs immediately.


2 Answers

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

like image 181
jmcgrory Avatar answered Sep 19 '22 14:09

jmcgrory


Actually, when a queue fails a failing event fires, so for example, you may register the failing event in your AppServiceProvider class in the boot method using something like this:

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

Alternatively, you can declare a failed method in the handler class for example:

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:

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

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

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

This is an example of program section that I've used to monitor my queue using Supervisord. In this case, you need to read the documentation for the supervisord to understand how to use it, I've just gave you an idea. The supervisord will 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.

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.

like image 31
The Alpha Avatar answered Sep 22 '22 14:09

The Alpha