How to kill the laravel queue:listen --queue=notification? - laravel

For a cron job I am using following code in laravel 5.1 and run the command in every 1 min. But even though after stopping cronjob from crontab the laravel code still executes. ?
$this->call('queue:listen', [
'--queue' => 'notification-emails','--timeout'=>'30'
]);
what could be the problem ? How can I stop this queue listen ?

You probably looking for queue:work which will stop, when no more jobs left, meanwhile queue:listen will persist.
If You want to kill existing process - You have to do it manually, because there is no command in laravel to kill all queue:listen processes.
Keep in mind, that You will not find process like artisan queue:listen, You have to look for artisan schedule:run because queue:listen, when called internally, will not create separate process.

Related

Running Artisan Horizon on Shared Hosting

im tried to create cronjob on shared hosting with artisan horizon like this
/usr/local/bin/ea-php74 /home/example/example.com/artisan schedule:run 1>> /dev/null 2>&1
/usr/local/bin/ea-php74 /home/example/example.com/artisan horizon>> /dev/null 2>&1
but after a view hours our server goes down. any solutions for us?
Horizon is not supposed to be on a cronjob, as every time the cron triggers that line, it is a new Horizon process that has to run, so probably that's why your server is going down.
The right solution is to set up Horizon using Supervisor: https://laravel.com/docs/8.x/horizon#supervisors

How to run queue worker on shared hosting

My Laravel application has a queued event listener and I have also set up the cronjob to run schedule:run every minute.
But I don't know how I can run the php artisan queue:worker command persistently in the background. I found this thread where it was the most voted approach:
$schedule->command('queue:work --daemon')->everyMinute()->withoutOverlapping();
However, on a different thread some people complained that the above-mentioned command creates multiple queue worker.
How can I safely run a queue worker?
Since Laravel 5.7, there's a new queue command to stop working when empty:
php artisan queue:work --stop-when-empty
As this is mostly just for emails or few small jobs, I put it on a cronjob to run every minute. This isn't really a solution for more than 100 jobs per minute I'd say, but works for my emails. This will run about 5 seconds every minute just to send emails, depending on how many emails or how big the job.
Steps
Create new command: php artisan make:command SendContactEmails
In SendContactEmails.php, change: protected $signature = 'emails:work';
In the handle() method, add:
return $this->call('queue:work', [
'--queue' => 'emails', // remove this if queue is default
'--stop-when-empty' => null,
]);
Schedule your command every minute:
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:work')->everyMinute();
// you can add ->withoutOverlapping(); if you think it won't finish in 1 minute
}
Update your cronjobs:
* * * * * /usr/local/bin/php /home/username/project/artisan schedule:run > /dev/null 2>&1
Source
Processing All Queued Jobs & Then Exiting
The --stop-when-empty option may be used to instruct the worker to process all jobs and then exit gracefully. This option can be useful when working Laravel queues within a Docker container if you wish to shutdown the container after the queue is empty:
php artisan queue:work --stop-when-empty
are you using cpanel?
you can set in the Scheduler or Cron Jobs menu.
and set the command in there
You can set a schedule task like this
$schedule->command('queue:work --stop-when-empty')->everyMinute()->withoutOverlapping();

Laravel run the queue job every second

I have created a queue job which need to run every second.How can I do that ? So i have created a job using artisan command,but the job is not run every second. I think I need to reconfigure some config files of supervisor.
Laravel docs have examples of exactly that. check https://laravel.com/docs/5.6/queues#supervisor-configuration
The default examples is
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/yourproject/artisan queue:work sqs --sleep=3 --tries=3
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile= /var/www/html/yourproject/storage/logs/worker.log
Note that you need to have a worker connection set in config/queues.php, and then on superviser, in the command artisan queue:work you can specify the connection. in the example i sent is using sqs but you can configure other stuff like redis
I'm successfully using Spatie's Short Schedule package for this.
You may use crontab for your queueable job. But Cron only allows for a minimum of one minute. Use crontab -e to set your schedule with the help of https://crontab.guru/ add this 2 * * * * php /var/www/html/your-project-folder/artisan queue:work >> /dev/null 2>&1 to your crontab -e which runs every 2 minutes.
What you could, you need to write a shell script with an infinite loop that runs your task, and then sleeps for every second.

Setting up a Laravel cron job in cPanel

I have the following function:
protected function schedule(Schedule $schedule)
{
$schedule->command('email:users')->everyMinute();
}
when I run the command
artisan schedule:run
it sends an email but when I add the following command to the cpanel as a cron job it doesn't send any email. Cpanel suppose to email me a notification when the cron job is run but I haven't receive a single email.
php /home/rain/artisan schedule:run 1>> /dev/null 2>&1
Where am I doing wrong?
Also when I run the command artisan schedule:run it runs it only once. I am very curious why do I have to add ->everyMinute(); if it is not going to run every minute? If I want to send it weekly I can setup the cron job. Why do I have to write to add ->weekly(); in the function if cron job is sending it weekly?
The Laravel scheduler assumes you have a cronjob every minutes. The scheduler is only useful if you want to have multiple tasks.
Normally you have one single cronjob configured in cPanel and you can set the scheduler to everyWeek() and have another task that would be everyDay() without having to add of change the cronjobs in your cPanel.
Laravel will automagically know if the task has already been run.
https://laravel.com/docs/5.4/scheduling
This Cron will call the Laravel command scheduler every minute. When
the schedule:run command is executed, Laravel will evaluate your
scheduled tasks and runs the tasks that are due.
It worked for me:
First try your command without waiting:
/usr/local/bin/php /home/hosting-username/laravel-folder/artisan schedule:run
Then, once you checked if it worked, add this:
/usr/local/bin/php /home/hosting-username/laravel-folder/artisan schedule:run >> /dev/null 2>&1
This Works for me
/usr/local/bin/php /home/hosting-username/laravel-folder/artisan schedule:run >> /dev/null
Just Make Sure you use exact version of php to execute schedule
e.g
if your php v is 7.3 then the code will be
/usr/local/bin/ea-php73 /home/hosting-username/laravel-folder/artisan schedule:run >> /dev/null
This worked for me
/usr/local/bin/php /home2/maildoll/demo.maildoll.com/artisan queue:work --stop-when-empty >> /dev/null

Laravel queues run forever

I have a page which supports some kind of mail notificiation. When user inserts some data, I want to send mail to another. I know, Mail::send() works perfectly, but it is slow. So I want to push this mail to queue. I use iron.io as provider. Everything works perfectly until I close console.
So is it possible to run php artisan queue:listen forever after I close console on Win and Linux?
You can run every process in the background in linux by using nohup
nohup php artisan queue:listen
This will keep the process running even if you close your terminal, nohup will force to ignore hangup signals.
nohup creates a logfile. If you want to suppress this, you can add
>/dev/null 2>&1 &
after your command

Resources