3

I have a CustomCommand_1 and a CustomCommand_2.

Any way to create a pipeline of commands and executing CustomCommand_2 right after CustomCommand_1 execution? (without call a command inside the other one).

shA.t
  • 16,580
  • 5
  • 54
  • 111
Christopher
  • 3,379
  • 7
  • 25
  • 36

3 Answers3

1

You can use a callback to decide when something will or won't run, using when() or skip():

$schedule
    ->call('Mailer@BusinessDayMailer')
    ->weekdays()
    ->skip(function(TypeHintedDeciderClass $decider)
    {
        return $decider->isHoliday();
    }
);

Referred: Event Scheduling and Commands & Handlers

You can also read how to add commands in queue here. See, if that helps.

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
0

I could not find any way to do this, so I came up workaround (tested on laravel sync driver).

First, you have to create/adjust base command:

namespace App\Commands;

use Illuminate\Foundation\Bus\DispatchesCommands;

abstract class Command {
    use DispatchesCommands;
    /**
     * @var Command[]
     */
    protected $commands = [];

    /**
     * @param Command|Command[] $command
     */
    public function addNextCommand($command) {
        if (is_array($command)) {
            foreach ($command as $item) {
                $this->commands[] = $item;
            }
        } else {
            $this->commands[] = $command;
        }
    }

    public function handlingCommandFinished() {
        if (!$this->commands)
            return;
        $command = array_shift($this->commands);
        $command->addNextCommand($this->commands);
        $this->dispatch($command);
    }
}

Every command has to call $this->handlingCommandFinished(); when they finish execution.

With this, you can chain your commands:

$command = new FirstCommand();
$command->addNextCommand(new SecondCommand());
$command->addNextCommand(new ThirdCommand());
$this->dispatch($command);

Pipeline

Instead of calling handlingCommandFinished in each command, you can use command pipeline!

In App\Providers\BusServiceProvider::boot add:

$dispatcher->pipeThrough([
    'App\Commands\Pipeline\ChainCommands'
]);

Add create App\Commands\Pipeline\ChainCommands:

class ChainCommands {
    public function handle(Command $command, $next) {
        $result = $next($command);
        $command->handlingCommandFinished();
        return $result;
    }
}
Daniel Antos
  • 1,206
  • 1
  • 8
  • 9
0

What is stopping you from doing the following?

$this->dispatch(new CustomCommand_1);
$this->dispatch(new CustomCommand_2);
// And so on
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Because each custom command calls more commands. And i want to execute `CustomCommand_2` after the execution of the `CustomCommand_1` and its subcommands. – Christopher Apr 28 '15 at 17:12
  • @idknow Surely by adding them to the queue, they’re _queued_ and run in turn? – Martin Bean Apr 28 '15 at 19:23