Laravel queue doesn't fail automatically despite using --tries - php

I'm want to queue some jobs using Redis,so I made an Event and its Listener.
The Listener implements ShouldQueue. I deliberately throw exception to test whether it fails. By entering the the command php artisan queue:work --tries=3 it should try the job 3 times and if it can't be done, add it to failed-jobs right? but it doesn't do so. I tried using database driver but nothing changed.
One time I used php artisan queue:work --tries=3 --daemon despite the fact that --daemon is deprecated, and after 3 tries,the job was marked as failed - but when I stopped the command and ran it again, it didn't fail again.
why is this happening? Thanks in advance :)

I think you should deal with the failed jobs and check for the reasons ,
I suggest Adding Failed_jobs Table and check for failure reasons run theses commands to use that
php artisan queue:failed-table
php artisan migrate
Then try
php artisan queue:work redis --tries=3 --delay=3
If the job failed check the failed jobs table

Related

Laravel : Email Jobs are not attempted

i have my Laravel project setup on Ubuntu Server where emails are being sent using Jobs.
Below is my laravel-worker file in /etc/supervisor/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log
stopwaitsecs=3600
My jobs are being dispatched in Controller as this
dispatch(new UserRegistrationEmail($data, $token));
in env file i have
QUEUE_CONNECTION=database
This was all working fine and emails were being sent in background but for some reason emails stopped going automatically and queues are not being attempted . My Jobs table
if i am logged in through Putty and run command php artisan queue:work it executes the jobs and emails are sent, Now until dont ctrl+c this command it keeps processing upcoming job requests as well , as seen in screenshot .
I have cleared config cache, restarted queue, cleared cache but nothing helped. Can someone please help with this , This is quite frustrating
When you dispatch a job through the code (like App\Jobs\YourJob::dispatch(); ) It will create a record in the jobs table, with all the information needed to run this job.
Once you run php artisan queue:work it starting to fetch these jobs from the database, as a queue (the latest one first) and run the commands.
In order to monitor and fetch and run these jobs when they are presented in the database I use the command php artisan queue:work --stop-when-empty and run this command every minute in my webserver as a cron job.
In this case everyminute it checks the datebase and if there is any job to handle it does.

Clear Laravel Queue Cache without restarting

In my application, every customer has a kind of complex class in which we do some search and replaces for that specific customer. I run Queue workers to run a daily sync with eBay for every single customer to do some kind of search and replaces.
The problem is Laravel queues caches the code for a good deal of time and if I want to go and change any customer class file (Which happens frequently), I will have to restart queue workers (Which may stop a running job that I don't intend to stop).
So my question is, how to force Laravel Queue to reread the new code without restarting workers?
you will have not cache if you use code:
php artisan queue:listen
Also, interest command:
php artisan queue:work --queue=system,hot,default --tries=10 --timeout=10 --stop-when-empty
Open an extra terminal and use the artisan command
php artisan queue:restart
This command will instruct all queue workers to gracefully "die" after they finish processing their current job so that no existing jobs are lost.
You may gracefully restart all of the workers by issuing the queue:restart command:
php artisan queue:restart
source: https://laravel.com/docs/5.8/queues#running-the-queue-worker
so the full chain of commands should be (maybe) this:
composer dump-autoload
php artisan optimize
php artisan clear-compiled
php artisan cache:clear
php artisan view:clear
php artisan route:cache
php artisan queue:restart
I left out the config cache command on purpose because I don't want to run it initially. Running it, will actually start to cache your config file afaik so I dont want that.
I'm answering because I want to store my own answer to evernote, to never ever have to deal with that bs again

Laravel queue only run one job

In my table "jobs" have 5 jobs, i used the process "php artisan queue:listen" on localhost then it ran all jobs and finished. But when i used this process on server then it only once ran one jobs (same "php artisan queue:work"). I use queue_driver is "database".
Update - Laravel 5.5+
Just use:
php artisan queue:work --once
TL;DR
php artisan queue:work = run one queued job and stop
php artisan queue:listen = run all queued jobs and listen for more until stopped
Further detail:
How are you running the commands? Are you just opening a terminal and running it or do you have a cron job to run at intervals?
If you're just opening a terminal and running php artisan queue:work will complete one task on the queue then stop, whereas if you run php artisan queue:listen it will process all the jobs in the queue and continue to listen for any further jobs until it is stopped.
Read further in the docs regarding queue:work:
Starting The Queue Listener
Laravel includes an Artisan command that will run new jobs as they are
pushed onto the queue. You may run the listener using the queue:listen
command:
php artisan queue:listen
Processing The First Job On The Queue
To process only the first job on the queue, you may use the queue:work command:
php artisan queue:work
What you should be doing?
I am guessing what you ideally want to do on your server is set up a cron job to run continuously at intervals and have it run queue:work. Better yet, acquaint yourself with the docs and make a decision after that.
See similar question answered here: What is the difference between queue:work --daemon and queue:listen

How to execute laravel job (queue)?

developers, I have a problem. My queue not working or i just not understand how it's works. I create a command which should add a new queue job. Driver for Queue is - database. After executing my command i see a new row in table 'jobs'. After that I try to do "php artisan queue:work" - but nothing happens.
Help me please, how can I execute this job?
From the documentation : [Daemon Queue Listener] The queue:work artisan command includes a --daemon option for forcing the queue worker to continue processing jobs without ever re-booting the framework. This results in a significant reduction of CPU usage when compared to the queue:listen command:
To start a queue worker in daemon mode, use the --daemon flag:
php artisan queue:work connection --daemon
However if you don't have multiple connections remove connection and execute it without connection :
php artisan queue:work --daemon
It worked for me.
Try
php artisan queue:listen
instead.
Yes there are times when your queue jobs won't run. For deployment if you are using redis queue driver, if not you can follow this here to install and configure redis and after which you should create a table for failed jobs using
php artisan queue:failed-table
php artisan migrate and then use php artisan queue:work redis --tries=3 --backoff=3 to retry every failed jobs 3 times after 3 seconds of failure.
To delay the next retry just add --delay=[NUM_OF_SECONDS] to your command.
For example, to wait 30 seconds to retry after failing just
run: php artisan queue:work tries=3 --delay=30
OR
php artisan queue:work --daemon --tries=3 --sleep=5 --delay=10

How does Laravel queue work and what if php artisan queue:listen stops

I have installed beanstaled and its working fine with laravel. The point where I am puzzled is that we have to do
php artisan queue:listen
to start listening queue. Right now, I am using it on amazone ec2 instance remotely through putty. but what is i close terminal? Will the jobs created through the code will work? Is it manually calling php artisan queue:listen or php artisan queue:work all time. Which does not seems fair.
If once php artisan queue:listen done, will it keep on running even if we close terminal?
Actually I dont know.
you need to install supervisor also. Here is a tutorial on using beanstalkd with laravel:
http://fideloper.com/ubuntu-beanstalkd-and-laravel4
Here are details on supervisor also:
http://supervisord.org/installing.html
I personally use a redis instance and run my queue with supervisor from there.
I find its a bit more memory effective then beanstalkd personally but each to there own.
Supervisor will execute the queue:listen command from artisan and this will run a job, if you have multiple supervisor processes then you can run multiple in line items.
depending on what you are doing i would almost look into python and multithereading also as i have used this for a few things i used to use a queue for and it has provided even better results.
example config file for supervisor:
[program:myqueue]
command=php artisan queue:listen --env=your_environment
directory=/path/to/laravel
stdout_logfile=/path/to/laravel/app/storage/logs/myqueue_supervisord.log
redirect_stderr=true
autostart=true
autorestart=true
You can also make use of Laravel's Task Scheduler i.e add the php artisan queue:listen command to the scheduler and sets its frequency to whatever you wants.
So that will make sure to call queue listen process automatically.
Hope it will make sense.

Categories