Laravel : Task Scheduling [ In Parallel ]

2019-05-17 22:29发布

问题:

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

回答1:

The Laravel scheduler can only run one command at a time because of the limitations of PHP.

You could, however, add the cronjobs directly in your crontab file, this way, they will be executed in parallel in separate processes.

15 * * * * * php /path/to/artisan email:notifications1
15 * * * * * php /path/to/artisan email:notifications2
15 * * * * * php /path/to/artisan email:notifications3


Another way to fix this is to let the jobs start at another time. Because a new php process is started every minute by the cron job, these do not affect each other.

For example:

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