18

Suppose I have three commands I want to schedule: 'commandA', 'commandB', and 'commandC'

But I don't want to run 'commandB' until 'commandA' is complete and I don't want to run 'commandC' until 'commandB' is complete.

I know I can schedule each to run every five minutes:

$schedule->command('commandA')->everyFiveMinutes();
$schedule->command('commandB')->everyFiveMinutes();
$schedule->command('commandC')->everyFiveMinutes();

But is it possible to chain them one after the other?

hogan
  • 1,434
  • 1
  • 15
  • 32
wkm
  • 1,764
  • 6
  • 24
  • 40

1 Answers1

38

Use then(Closure $callback) to chain commands:

$schedule->command('commandA')->everyFiveMinutes()->then(function() {
    $this->call('commandB');
    $this->call('commandC');
});
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
  • 1
    Actually that didn't seem to work. First issue was that the $schedule variable was not known within the Closure. Fixed that by using 'use'. I would see commandA being executed but commandB and commandC were not executed. One way I ended up getting them to run was using $this->call('commandB'); within the Closure. – wkm Apr 28 '15 at 09:23
  • @wkm thank you for reply! Please make changes on my answer co I can apply these changes to it. – Limon Monte Apr 28 '15 at 09:28
  • 1
    @wkm just tested this in my sandbox and amended my answer. 100% working and tested now :) – Limon Monte Apr 28 '15 at 11:29
  • Here's the official [docs](https://laravel.com/api/8.x/Illuminate/Console/Scheduling/Event.html#method_then) for `->then` – om-ha Mar 10 '21 at 01:08