How to execute more than one background process in laravel?

First of all, I know about queues and now I have good experience working with queues. The problem with the queue is the queue. I want to execute several functions or commands together in the background. The queues store the second command or function in the queue and will be executed after the first execution!

For example, I have a table with ~ 3,000,000 records, and I want to process them faster. What I can do is split them into 5 equal pieces and execute 5 commands in total so that I can use my processor, as well as the process data, 5 times faster.

So how can I do this with Laravel? Queues are not going to work because they do things one by one. If your idea is to create several 5 queues and supervisors to execute, this is not the standard way to do this, I think.

Any idea what can be done in this case?

+6
source share
2 answers

Just to add something from my personal experience.

First install and configure a supervisor for your OS, following the examples below for Linux with a database, for example. Ubuntu

Supervisor confs: (/etc/supervisor/conf.d)

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
user=username
numprocs=25
redirect_stderr=true
stderr_events_enabled=true
stderr_logfile=/var/www/app/storage/logs/worker.error.log
stdout_logfile=/var/www/app/storage/logs/worker.log

jobs dispatch.

, 25 .

0

, . . , .

, (, 5 . 3 5 , 600 . )

, , , --once. , , .

$chunk_id = 0;
foreach($chunks as $chunk){
    // Adding chunk to queue
    Artisan::queue('process:items',[
        'items' => $chunk,
    ])->onQueue('processChunk'.$chunk_id);

    // Executing queue worker only once
    exec('php artisan queue:work --queue=processChunk'.$chunk_id.' --once > storage/logs/process.log &');

    $chunk_id++;
}

exec , . , & , .

. , ! ?

+2

All Articles