When in Laravel version 8.5.0 a Scheduled Task fails it is not providing me with an output in the onFailure
hook
->onFailure(function (Stringable $error) {
\ To something with output
}
Link to documentation: https://laravel.com/docs/8.x/scheduling#task-hooks
This is my current set up.
kernel.php
use IlluminateSupportStringable;
$cronId = 33;
$cronUrls = $healthChecks->getHealthCheckUrls('cron');
$schedule->command(DoSomethingThatThrowsAnError::class)
->when(function () use ($crons, $cronId) {
return $crons->isActive($cronId);
})
->before(function () {
Log::info('cron "do something that throws an error" has started.');
})
->after(function () {
Log::info('cron "do something that throws an error" has finished.');
})
->onFailure(function (Stringable $error) {
Log::warning("cron 'do something that throws an error' has failed. Error: $error");
})
->pingBefore($cronUrls['start'])
->pingOnSuccess($cronUrls['success'])
->pingOnFailure($cronUrls['failure'])
->days([1, 2, 3, 4, 5])
->at('17:00');
DoSomethingThatThrowsAnError.php
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use IlluminateConsoleOutputStyle;
class DoSomethingThatThrowsAnError extends Command
{
protected $signature = 'cron:do-something-that-throws-an-error';
protected $description = 'banana';
public function handle()
{
throw new Exception("banana");
}
}
What I excepted to happen
That the exception is logged in laravel.log
, since (correct me if I am wrong) all application exceptions are logged there.
And that the exception would be logged in the onFailure
hook, like "cron 'do something that throws an error' has failed. Error: (error gibberish)"
What actually happened
The exception is only logged in laravel.log
in general. Furthermore, the onFailure
hook is called, but the $error
parameter is empty.
Why is this an issue
I use multiple log files and in the current situation I have to match an exception in laravel.log
with an onFailure
log in another log file. Which is often rather annoying.
The question
How do I set/ get the exception as a parameter of the onFailure
hook?
question from:
https://stackoverflow.com/questions/65941010/laravel-task-scheduling-not-providing-output-on-failure 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…