Skip to content

[12.x] Add default scheduler output #56611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class Schedule
*/
protected array $groupStack = [];

/**
* The schedule default output.
*
* @var string|null
*/
protected ?string $defaultOutput = null;

/**
* Create a new schedule instance.
*
Expand Down Expand Up @@ -327,6 +334,8 @@ public function group(Closure $events)
*/
protected function mergePendingAttributes(Event $event)
{
$this->mergeDefaultAttributes($event);

if (isset($this->attributes)) {
$this->attributes->mergeAttributes($event);

Expand All @@ -340,6 +349,13 @@ protected function mergePendingAttributes(Event $event)
}
}

protected function mergeDefaultAttributes(Event $event): void
{
if (! is_null($this->defaultOutput)) {
$event->appendOutputTo($this->defaultOutput);
}
}

/**
* Compile parameters for a command.
*
Expand Down Expand Up @@ -462,6 +478,19 @@ protected function getDispatcher()
return $this->dispatcher;
}

/**
* Specify the default output events should use from now on.
*
* @param string|null $output
* @return $this
*/
public function setDefaultOutput(?string $output): Schedule
{
$this->defaultOutput = $output;

return $this;
}

/**
* Dynamically handle calls into the schedule instance.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Console/Scheduling/ScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,33 @@ public function testJobIsNotInstantiatedIfSuppliedAsClassname(): void
self::assertSame(JobToTestWithSchedule::class, $scheduledJob->description);
self::assertFalse($this->container->resolved(JobToTestWithSchedule::class));
}

#[DataProvider('scheduledEventAppendsOutputToDefaultOutputProvider')]
public function testScheduledEventAppendsOutputToDefaultOutput(string $method, array $arguments): void
{
$schedule = new Schedule();

/** @var \Illuminate\Console\Scheduling\Event $oldEvent */
$oldEvent = call_user_func_array([$schedule, $method], $arguments);
$oldEventOutput = $oldEvent->output;

$schedule->setDefaultOutput($defaultOutput = '/custom/scheduler/output');

/** @var \Illuminate\Console\Scheduling\Event $newEvent */
$newEvent = call_user_func_array([$schedule, $method], $arguments);

self::assertSame($oldEventOutput, $oldEvent->output);
self::assertSame($defaultOutput, $newEvent->output);
self::assertTrue($newEvent->shouldAppendOutput);
}

public static function scheduledEventAppendsOutputToDefaultOutputProvider(): array
{
return [
['job', [JobToTestWithSchedule::class]],
['command', ['env']],
['call', [fn () => 0]],
['exec', ['whoami']],
];
}
}