This question already has an answer here:
symfony 4 process running a command, not running
(1 answer)
Closed 1 year ago.
I'm trying to start a process with Symfony in my Laravel app on my shared hosting server that will call an Artisan command with parameters like so:
$process = new Process(['/usr/local/bin/php', base_path('artisan'), 'queue:work --stop-when-empty']);
$process->setTimeout(null);
$process->start();
This does not work as I get:
ERROR: Command "queue:work --stop-when-empty" is not defined.
Did you mean one of these?
queue:batches-table
queue:clear
queue:failed
queue:failed-table
queue:flush
queue:forget
queue:listen
queue:monitor
queue:prune-batches
queue:prune-failed
queue:restart
queue:retry
queue:retry-batch
queue:table
queue:work {"exception":"[object] (Symfony\\Component\\Console\\Exception\\CommandNotFoundException(code: 0): Command \"queue:work --stop-when-empty\" is not defined.
The problem is with the parameter --stop-when-empty as the command runs succesfully wthout it. How can I pass this parameter to the command ?
The process adds quotes around each array value that you pass in. Right now, it's attempting to run
"exec '/usr/local/bin/php' '/your/path/to/artisan' 'queue:work --stop-when-empty'"
So by adding quotes around the whole string, it's treating 'queue:work --stop-when-empty' as a single command, instead of a command and option. Separate out the command so that it will quote it properly
$process = new Process(['/usr/local/bin/php', base_path('artisan'), 'queue:work', '--stop-when-empty']);
On my Linux server I have the following cron:
* * * * * php /var/www/core/v1/general-api/artisan schedule:run >> /dev/null 2>&1
The CRON works correctly. I have a scheduled command defined in my Kernel.php as such:
protected function schedule(Schedule $schedule)
{
$schedule->command('pickup:save')
->dailyAt('01:00');
$schedule->command('queue:restart')->hourly();
}
The scheduled task at 1AM runs my custom command php artisan pickup:save. The only thing this command does is dispatch a Job I have defined:
public function handle()
{
$job = (new SaveDailyPropertyPickup());
dispatch($job);
}
So this job is dispatched and since I am using the database driver for my Queues, a new row is inserted into the jobs table.
Everything works perfectly up to here.
Since I need a queue listener to process the queue and since this queue listener has to run basically forever, I start the queue listener like this:
nohup php artisan queue:listen --tries=3 &
This will write all the logs from nohup to a file called nohup.out in my /home directory
What happens is this: The first time, queue is processed and the code defined in the handle function of my SaveDailyPropertyPickup job is executed.
AFTER it is executed once, my queue listener just exits. When I check the logs nohup.out, I can see the following error:
In Process.php line 1335:
The process "'/usr/bin/php7.1' 'artisan' queue:work '' --once --queue='default'
--delay=0 --memory=128 --sleep=3 --tries=3" exceeded the timeout of 60 seconds.
I checked this answer and it says to specify timeout as 0 when I start the queue listener but there are also answers not recommending this approach. I haven't tried it so I dont know if it will work in my situation.
Any recommendations for my current situation?
The Laravel version is 5.4
Thanks
Call it with timeout parameter, figure out how long your job takes and scale from there.
nohup php artisan queue:listen --tries=3 --timeout=600
In your config you need to update retry after, it has to be larger than timeout, to avoid the same job running at the same time. Assuming you use beanstalkd.
'beanstalkd' => [
...
'retry_after' => 630,
...
],
In more professional settings, i often end up doing a queue for short running jobs and one for long running operations.
I have a problem with laravel jobs.
I configured laravel jobs to work with the database and it is working.
When I execute a job, the entry is created in database and the constructor is well executed.
However, the handle function is never executed ... and the jobs stay in the jobs table.
Someone already had this problem?
(I use Laravel 5.7).
I found the problem...
I'm using a different queue name that the default and in config/queue.php, in the database array you have the default queue name set to "default".
So when i execute : php artisan queue:work , he is waiting for default queue.
When i execute the command line : php artisan queue:work --queue QUEUENAME it is working !
Thanks everybody.
You should listen to the queue for default
php artisan queue:work
or
php artisan queue:work --sleep=1 --tries=5 --timeout=60
If you are not using the default queue then mention the custom queue
php artisan queue:work --sleep=1 --tries=5 --timeout=60 --queue customQueue
I am creating a web application in laravel in which bidding is being done by users in multiple games. Bidding is being performed by front end users and by cron job as well. Cron job do bid on each game after each second. Therefore some collision was happening between bids when same row was accessed at same time. To resolve concurrency issue I decided to use laravel queues for bidding. But I am having multiple games and therefore I want simultaneously bids of each game. I don't want bids of same game to be process at same time because then concurrency issue can be occur. I want to know about multiple queues system in laravel. After having search on internet I got to know about multiple queues like
php artisan queue:work --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7
But I am not sure how it works. Please someone explain me in detail that all 7 queues work simultaneously or one by one.
php artisan queue:work --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7 sets the priority in which queues will be executed. So with this all jobs on myJobQueue will be executed before moving to execute jobs on myJobQueue1 then to myJobQueue2 in that order.
However if you want jobs on these queues to be executed simultaneously, you could run each queue in the background.
php artisan queue:work --queue=myJobQueue & php artisan queue:work --queue=myJobQueue1 & php artisan queue:work --queue=myJobQueue2 &
This will run each queue as single processes in the background.
Are looking for the queue:listen command?
queue:work will process all pending jobs that are stored by the queue driver, whereas queue:listen will be waiting for jobs to be thrown at it to execute them as they come.
If you do php artisan queue:listen --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7, 7 queues are being created and listening to new tasks on their own.
In your code, you can dispatch jobs like the following:
dispatch((new MyJob)->onQueue('myJobQueue'));
You might want to use a tool like Supervisor to make sure queue:listen is always running in the background.
Hope this helps!
Like Ben V said, it is highly recommended to use Supervisor to keep the workers active at all times, especially if you want to run one or more workers per queue, or if you want the queues to be processed simultaneously.
Here is an example Supervisor configuration file:
[program:laravel-worker-myJobQueue]
process_name=%(program_name)s_%(process_num)s
command=php artisan queue:work --queue=myJobQueue
numprocs=8
autostart=true
autorestart=true
[program:laravel-worker-myJobQueue1]
process_name=%(program_name)s_%(process_num)s
command=php artisan queue:work --queue=myJobQueue1
numprocs=1
autostart=true
autorestart=true
The above configuration creates 8 workers for myJobQueue, and one worker for myJobQueue1, since multiple workers can help speed things up, but can cause trouble for jobs that try to access the same row in the database, in which case you want to limit things to 1 worker only.
You then simply dispatch jobs to the correct queue using
dispatch((new MyJob)->onQueue('myJobQueue'));
or
dispatch((new MyJob)->onQueue('myJobQueue1'));
This might be old but just in case, all of the answers are on point, but you must set the .env variable QUEUE_CONNECTION to something else than sync,if your configuration is set to sync it will take every job in order of entry to the queue (thus finishing one and then starting the next one), if it's set to database or redis it will be taking jobs in parallel if needed (which is the idea of setting priorities) you should check this article (it helped me) https://medium.com/hexavara-tech/optimize-laravel-with-redis-caching-made-easy-bf486bf4c58 also you will need to configure your queues in config/queue.php like such for example in the 'connections' array:
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => ['default','another_queue'], //this is just 'default' by default
'retry_after' => 90,
],
this applies for all the other configurations in this file.
If you are testing on a local machine you can create a .bat file inside your project and enter these lines in that bat file
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
one line represents one queue at a time 10 means 10 queues will run at once.
also, I included --queue=low cause I have a low queue.
this is for local machines only for the online production checkout Supervisor.
In addition to the other answers here, you can also dispatch a job on a specified queue in Laravel like so:
MyJob::dispatch()->onQueue('myJobQueue');
Or, within the Job constructor:
public function __construct()
{
$this->onQueue('myJobQueue');
}
Remember to start your queue from the terminal:
php artisan queue:listen --queue=myJobQueue
I have an external PHP script, that is processing an XML array to insert, update or delete rows in a database. This script lies in the root of the project in a folder called scripts and I can run and execute it via terminal with no problems whatsoever and it updates the database accordingly:
php index.php
I have also set up a schedule in Laravel (using October CMS syntax)
public function registerSchedule($schedule)
{
$schedule->exec(public_path() . '/script/index.php')->everyMinute();
}
This however is doing nothing. I tried manually running the schedule with artisan in command line by:
php artisan schedule:run
And the output is
Running scheduled command: /Users/x/x/x/x/scripts/index.php > '/dev/null' 2>&1 &
Nothing happens in the database tho.
Did you try to generate a new key?
php artisan generate:key