Laravel multiple apps same server do queues conflict if same name? - php

We're currently running two laravel applications on the same dedicated server. Each application utilizes laravel's queueing system for background jobs and notifications. Each uses redis for the driver. Neither define any specific queues, they are both using the default. Our supervisor .conf is as follows:
[program:site-one-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/siteone.com/artisan queue:work --sleep=5 --tries=1
autostart=true
autorestart=true
user=www-data
numprocs=4
redirect_stderr=true
stdout_logfile=/var/www/siteone.com/storage/logs/worker.log
[program:site-two-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/sitetwo.com/artisan queue:work --sleep=5 --tries=1
autostart=true
autorestart=true
user=www-data
numprocs=4
redirect_stderr=true
stdout_logfile=/var/www/sitetwo.com/storage/logs/worker.log
Everything worked as expected before adding the configuration for the second site. After adding it, we were testing and noticed that when invoking an event on sitetwo.com that triggered a notification to be sent to the queue, that the email addresses which should have received the notifications did not, and instead they were sent to two email addresses that only exist within the database for siteone.com!
Everything seems to function as expected as long as only one of the above supervisor jobs is running.
Is there somehow a conflict between the two different applications using the same queue name for processing? Did I botch the supervisor config? Is there something else that I'm missing here?

The name of the class is all Laravel cares about when reading the queue. So if you have 2 sites dispatching the job App\Jobs\CoolEmailSender, then whichever application picks it up first is going to process it, regardless of which invoked it.
I can think of 2 things here:
Multiple redis-instances
or
unique queue names passed to --queue

I just changed APP_ENV and APP_NAME into .env file and it worked for me.
For Example:
First .env: APP_ENV=local APP_NAME = localapp
Second .env: APP_ENV=staging APP_NAME=stagingapp

Maybe late but did you try to modify the queue config at config/queue.php
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'project1', // 'project2'...
'retry_after' => 90,
'block_for' => null,
],
run queue with --queue=project1

Note: This answer is for those who are having multiple domains, multiple apps, and one database.
You can dispatch & listen to your job from multiple servers by specifying the queue name.
App 1
dispatch((new Job($payload))->onQueue('app1'));
php artisan queue:listen --queue=app1
App 2
dispatch((new Job($payload))->onQueue('app2'));
php artisan queue:listen --queue=app2

Related

Laravel Job Timeouts and retry_after setting

I'm using supervisor to run laravel jobs, and I was having a long-running job that was timing out, so I changed the rety_after variable from 90 to 3600 in config/queue.php.
It seems like now when I run php artisan queue:listen locally, it's trying to wait for 3600 (seconds?) before it attempts to run the job.
I just pushed some changes out to the prod environment and it seems like it's doing it there as well. Wondering if someone can give some insights on how to set up my jobs so they run quickly, but also don't timeout.
Here's my job definition in my supervisor conf file:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/my_project/artisan queue:work database --sleep=3 --tries=3 --timeout=2400
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=ec2-user
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/my_project/storage/logs/supervisor.log
stopwaitsecs=3600
startsecs=0
And in my config/queue.php file I have:
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 3600,
],
To me the documentation is not clear on exactly what all the various flags do.

Running two Laravel projects queues with supervisor

Hi below is the config file of supervisor of one project
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /livesites/siteA.example.com/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=8
redirect_stderr=true
stdout_logfile=/livesites/siteA.example.com/storage/logs/worker.log
Its running fine . I have another project (siteB.example.com) with the redis as QUEUE_CONNECTION in the .env. What should the the config file for that. Will there be any issue running two projects queues on same server ?
First If two projects are on different connections (Redis and Database) shouldn't be any problem.
But if connections are the same (both on Database or Redis), One solution might be using a different queue for each project.
for example in the siteA project push your jobs on siteA queue and in the siteB project push your jobs on siteB queue. Then create two separate supervisor config files and in each of them put --queue=siteA or --queue=siteB in the artisan command argument.
siteA.conf:
command=php /livesites/siteA.example.com/artisan queue:work database --queue=siteA --sleep=3 --tries=3
siteB.conf:
command=php /livesites/siteB.example.com/artisan queue:work database --queue=siteB --sleep=3 --tries=3
and finally, in your Laravel code dispatch each job to appropriate queue:
dispatch((new Job)->onQueue('siteA'));
in siteB project
dispatch((new Job)->onQueue('siteB'));
or you can globally change the default queue for each project in config/queue.php as below:
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'siteA' // or siteB,
'retry_after' => 90,
]

Laravel 5.2 queue - Delay not working

I need your help.
I'm working with Laravel queue and with Linux supervisor tool (Exactly like it the documentation)
Now I have a very weird issue.
When I use this command without delay
$job = (new SendAutoresponderEmail($poptin,$autoresponder,$data));
It's working fine.
But when I use the delay option
$job = (new SendAutoresponderEmail($poptin,$autoresponder,$data))->delay(60);
The job failed and not continue anymore
I can see the job on my failed-job table.
Now... When I'm not working with the supervisor tool and just run the command in my terminal:
php artisan queue:listen
The command with the delay option and other queue task working fine.
This is my larave-worker content look like:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/Poptin/artisan queue:work database --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=ubuntu
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/html/<project>/worker.log
What you think I need to do in order to fix it?
Also ... How can I use a different queue for a different job? like that
$job = (new SendAutoresponderEmail($poptin,$autoresponder,$data))->onQueue('autoresponder')->delay(60);
?
currently, I have only the default queue. Where I declare others queues in my config/queue.php file?
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
dispatch your job
$job = (new SendAutoresponderEmail($poptin,$autoresponder,$data))->delay(60);
$this->dispatch($job);
So... Eventually, I solve the issue by creating a new supervisor worker in a different connection and queue, like this:
[program:autoresponder-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/<Project>/artisan queue:listen autoresponder --sleep=5 -
-tries=3
autostart=true
autorestart=true
user=ubuntu
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/<Project>/worker.log

Laravel 5.5 Queue: No connector for []

I have two websites running identical laravel 5.5 project. In fact both websites hosted on the same server. One of them works, another one has troubles with queues. I have double-checked everything.
.env:
...
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=database
QUEUE_DRIVER=database
...
conf/queue.php
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
I do have empty jobs table as well.
Whenever I do try to run queues I'm getting an error
php artisan queue:work --daemon
In QueueManager.php line 172:
No connector for []
Please check the config queue drive setting. If use Redis, you can use php artisan tinker check
current the config.
Check command queue:
php artisan queue:work redis --queue=QUEUE_NAME --tries=3 --memory=128 --timeout=300
Check job listen by command line:
ps aux | grep php
I just had to specify a "connection" (database for my case) after queue:work command like below:
php artisan queue:work database --queue=Q_NAME
and the error disappeared!
Hi I encountered the same problem and couldn't find the solution but I followed laravel deploy optimizations, like cache config, view and routes which result into this. (I am not sure why).
I followed these and it worked like magic. (I don't know why it worked)
php artisan config:clear
php artisan route:clear
php artisan view:clear

Running beanstalkd worker on a remote server

My stack set-up consists of the following
Machine1 - Main Server (Running laravel)
Machine2 - MySql Server for the laravel codebase
Machine3 - Beanstalkd worker
I have setup Supervisord on Machine1 and added the following queue listener
[program:queue1]
command=php artisan queue:listen --queue=queue1 --tries=2
...
My laravel queue config file(app/config/queue.php) reads the following
'beanstalkd' => array(
'driver' => 'beanstalkd',
'host' => '--- Machine3 IP ---',
'queue' => 'queue1',
'ttr' => 60,
),
And I have installed beanstalkd on Machine3 along with Beanstalk console and can see my tasks being pushed to the queue and executing successfully. However I am not sure if Machine3 is actually executing them, and the reason for my suspicion is the High CPU usage on the main server as compared to no spikes in CPU usage on Machine3
I completely shutdown my beanstalkd Server to check if the queue still processes and the outcome was an error reported by laravel indicating it could not connect to the beanstalkd server.
I read somewhere that you need to have your laravel codebase on the beanstalkd server(Machine3) too, was that really the way to go?
Whichever machine you run queue:listen on is the machine that does the actual processing of the queue.
At the moment all you are doing is storing the queues on machine3, but processing them on machine1.
So you need to have machine3 run the queue:listen command if you want it to process the queue.

Categories