Laravel : Task Scheduling [ In Parallel ]

Viewed 3449

I have multiple tasks that need to be done every hour or two. All of them have been scheduled via Laravel using below comamnds as cron jobs

$schedule->command('email:notifications1')
            ->cron('15 * * * * *');
$schedule->command('email:notifications2')
            ->cron('15 * * * * *');
$schedule->command('email:notifications3')
            ->cron('15 * * * * *');

Issue: All of the above tasks are pretty time consuming & it seems from the results that these tasks are not running in parallel. And each tasks runs after the previous has ended.

Requirment How can i run them in parallel? I want all tasks to be executed (in parallel) as soon as the clock tick the specified time.

Laravel Version 5

2 Answers

You can easily have multiple parallel commands running if you add runInBackground() to the chain. Like this:

$schedule->command('email:notifications1')
            ->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications2')
            ->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications3')
            ->cron('15 * * * * *')->runInBackground();

This creates a new process in the background so the Scheduler doesn't have to wait until the command executes. This doesn't even interfere with the withoutOverlapping() method because that works with mutex files.

Now you also have the benefit of having your commands in version control.

Related