Skip to content

[7.x] Add Callbacks with Output to Console Schedule - #32633

Merged
taylorotwell merged 2 commits into
laravel:7.xfrom
DougSisk:console-schedule-output-callbacks
May 3, 2020
Merged

taylorotwell merged 2 commits into
laravel:7.xfrom
DougSisk:console-schedule-output-callbacks

Conversation

@DougSisk

@DougSisk DougSisk commented May 1, 2020

Copy link
Copy Markdown

Our health check provider allows POSTing output logs. Currently there's no built in way to do this. Instead of trying to figure out a one-size-fits-all to POST, my proposal is to use callbacks. This way you can use any kind of HTTP library you want, attach additional data, filter the output, etc.

I took the $onlyIfOutputExists argument from the emailOutputTo() command, so you can optionally only run the callback if an output was generated.

There are 4 callable methods, similar to the ping URL methods:

  • thenWithOutput()
  • thenIfWithOutput()
  • onSuccessWithOutput()
  • onFailureWithOutput()

Example:

$schedule->command('inspire')
    ->onSuccessWithOutput(function ($text) {
        Http::send('POST', 'https://healthcheckservice.test/abc123', [
            'body' => $text,
        ]);
    })
    ->daily();

Comment thread src/Illuminate/Console/Scheduling/Event.php Outdated
@DougSisk
DougSisk requested a review from GrahamCampbell May 1, 2020 19:42
@taylorotwell
taylorotwell merged commit 28d28c4 into laravel:7.x May 3, 2020
@taylorotwell

taylorotwell commented May 3, 2020 •

Copy link
Copy Markdown
Member

I made a few tweaks to this. The "output" is injected into the Closure using the container->call() method like the other callbacks. This means you must name the variable $output.

Which leads me to my next point, I think these methods shouldn't really be called directly and in 8.x I'm going to make the standard then callbacks reflect into the Closure and inject a Stringable $output if the Closure is requesting it.

@taylorotwell

Copy link
Copy Markdown
Member

OK, got it fixed up on 8.x (master branch)... works with after, then, onSuccess, onFailure:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants