4

This question is similar to this one except that:

  • I need to send the output of the chained (2nd, 3rd or 4th) commands in an emailOutputTo method

Single Command Scheduler Code
This is working fine - both command and the email output:

protected function schedule(Schedule $schedule)
{
    $schedule   ->command('commandA:myoption')
                ->emailOutputTo('myemail@gmail.com');
}

Chained Command / Call - Using Previous SO Answer
The aforementioned accepted answer runs commandB correctly but because it uses ->call() it does not include the output for commandB in the email:

protected function schedule(Schedule $schedule) {
    $schedule   ->command('commandA:myoption')
                ->then(function() {
                    return $this->call('commandB:myoption');
                })
                ->emailOutputTo('myemail@gmail.com');
}

...as noted here it's not possible to email from a Scheduler ->call() method, only when using the ->command() method :

The emailOutputTo, sendOutputTo and appendOutputTo methods are exclusive to the command method and are not supported for call.

I tried a modification of the aforementioned accepted answer to use ->command()
instead of ->call() :

protected function schedule(Schedule $schedule) {
    $schedule   ->command('commandA:myoption')
                ->then(function() {
                    $this->command('commandB:myoption');
                            ->emailOutputTo('myemail@gmail.com');
                })
                ->emailOutputTo('myemail@gmail.com');
}

...gives the following error:

[Symfony\Component\Debug\Exception\FatalThrowableError]
Type error: Too few arguments to function Illuminate\Foundation\Console\Kernel::command(), 1 passed in /app/Console/Kernel.php on line 33 and exactly 2 expected


Other Attempts at Chained / Series Commands

Tried various methods to chain commands using a then function - none seem to work (either running the command or output):

Option 1 - try to include output into a common email
Result = commandB not performed, only emails the commandA output:

protected function schedule(Schedule $schedule) {
    $schedule   ->command('commandA:myoption')
                ->then(function(Schedule $schedule) {
                    return $schedule->command('commandB:myoption');
                })
                ->emailOutputTo('myemail@gmail.com');
}

Option 2 - try to include output into a separate email
(by sending separate email outputs in the primary and 2nd commands)

Result = commandB not performed, only emails the commandA output:

protected function schedule(Schedule $schedule) {
    $schedule   ->command('commandA:myoption')
                ->then(function(Schedule $schedule) {
                    $schedule->command('commandB:myoption')
                            ->emailOutputTo('myemail@gmail.com');
                })
                ->emailOutputTo('myemail@gmail.com');
}

Option 3 - another variation injecting $schedule - with no success (same result)

protected function schedule(Schedule $schedule) {
    $schedule   ->command('commandA:myoption')
                ->then(function() use($schedule) {
                    return $schedule->command('commandB:myoption')
                                ->emailOutputTo('myemail@gmail.com');
                })
                ->emailOutputTo('myemail@gmail.com');
}

Questions
How do you chain commands in the scheduler and send the output from all commands to either

  • a common email ?
  • separate emails (if common isn't possible) ?

...as an aside, why is everyone so excited by Laravel when the documentation is so pathetically incomplete !!?

Community
  • 1
  • 1
goredwards
  • 2,486
  • 2
  • 30
  • 40
  • "why is everyone so excited by Laravel when the documentation is so pathetically incomplete !!?"; good question. But apparently the documentation is maintained on GitHub, so you can update it yourself if you're so inclined. – cartbeforehorse Feb 07 '18 at 09:23
  • @cartbeforehorse the frequent cry of "maintain the documentation yourself" to people who are still learning the framework doesn't seem like a winning strategy... – goredwards Feb 08 '18 at 16:25
  • I have the same question as the OP and can't find anything on google or SO about this...does no one else have this need? – smenzer Dec 11 '19 at 10:10

1 Answers1

0

In the documentation it says:

By default, multiple tasks scheduled at the same time will execute sequentially based on the order they are defined in your schedule method.

In my use case where I only have three tasks which needs to run only once each day (read: morning), I just created three scheduler to run at the same time. In your case, that might not work as one of your tasks may run over 5 minutes.

Mine ended up looking like this:

// $schedule->command('inspire')->hourly();
        $schedule->command('command:first')
            ->dailyAt('12:34')
        ->timezone('Europe/Oslo');
        $schedule->command('command:second')
            ->dailyAt('12:34')
            ->timezone('Europe/Oslo');
        $schedule->command('command:third')
            ->dailyAt('12:34')
            ->timezone('Europe/Oslo');

This solution works only if you have multiple tasks that must run in a specific order, and where it is supposed to run only once a day (or week).

I tried using the then closure, but it only ran one of the commands inside the function.

Trond
  • 363
  • 1
  • 4
  • 14
  • 2
    I recommend against rhetoric questions in answers. They risk being misunderstood as not an answer at all. You are trying to answer the question at the top of this page, aren't you? Otherwise please delete this post. – Yunnosch May 09 '21 at 12:48