John Mccandles
John Mccandles

Reputation: 1231

Laravel - How to schedule the same artisan task to run in parallel?

I am trying to schedule the same artisan task to run in parallel in Laravel 5.2. At first, I put $schedule->command in a for loop in Kernel.php:

    for ($j = 0; $j < 10; $j++) {
        $schedule->command('foo:bar --option=' . $j)
            ->dailyAt("1:00");
    }

And I found that foo:bar actually run one by one, in a serial way. The next command started when the previous one was finished.

Then I changed the code as follows,

    $schedule->call(function(){
        for ($j = 0; $j < 10; $j++) {
            Artisan::queue('foo:bar', [
                '--option' => $j
            ]);
        }
    })->dailyAt(1:00);

When I checked in Ubuntu with ps aux , I saw two tasks instead of 10 tasks. Am I missing something? And what's the best way to schedule the same Artisan task to run in parallel, starting at the same time? Thanks.

Upvotes: 0

Views: 2861

Answers (1)

Justin Origin Broadband
Justin Origin Broadband

Reputation: 1520

Using Supervisor to manage your queued tasks, you can run more than one task at once. I learned this after a painful afternoon of watching emails tick by one per second after queueing them up!

My conf file (in /etc/supervisor/conf.d/{appName}.conf)

[program:mylaravelappnamequeue]
command=php /var/www/html/mylaravelappname/artisan queue:listen --tries=1
directory=/var/www/html/mylaravelappname
stdout_logfile=/var/www/html/mylaravelappname/storage/logs/supervisord.log
redirect_stderr=true
autostart=true
autorestart=true
numprocs = 10
process_name = %(program_name)s%(process_num)s

Credit where it's due: https://laracasts.com/discuss/channels/general-discussion/how-to-speed-up-queues

Yes, this isn't "true" parallel, but it does run a queue a lot faster! Also, check how much RAM you have spare, you might want to turn the numprocs down to something like 4.

EDIT: some formatting

Upvotes: 3

Related Questions