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 !!?