Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

php - Laravel Task Scheduling not providing output on Failure

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...