Skip to content

[12.x] add 'dry-run' option for schedule:run #56532

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

Closed
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
42 changes: 39 additions & 3 deletions src/Illuminate/Console/Scheduling/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class ScheduleRunCommand extends Command
*
* @var string
*/
protected $signature = 'schedule:run {--whisper : Do not output message indicating that no jobs were ready to run}';
protected $signature = 'schedule:run
{--whisper : Do not output message indicating that no jobs were ready to run}
{--dry-run : Display scheduled commands without executing them}';

/**
* The console command description.
Expand Down Expand Up @@ -56,6 +58,13 @@ class ScheduleRunCommand extends Command
*/
protected $eventsRan = false;

/**
* Print commands without executing them.
*
* @var bool
*/
protected $dryRun = false;

/**
* The event dispatcher.
*
Expand Down Expand Up @@ -110,13 +119,15 @@ public function handle(Schedule $schedule, Dispatcher $dispatcher, Cache $cache,
$this->cache = $cache;
$this->handler = $handler;
$this->phpBinary = Application::phpBinary();
$this->dryRun = $this->option('dry-run');

$events = $this->schedule->dueEvents($this->laravel);

if ($events->contains->isRepeatable()) {
$this->clearInterruptSignal();
}

/* @var Event $event */
foreach ($events as $event) {
if (! $event->filtersPass($this->laravel)) {
$this->dispatcher->dispatch(new ScheduledTaskSkipped($event));
Expand All @@ -128,7 +139,9 @@ public function handle(Schedule $schedule, Dispatcher $dispatcher, Cache $cache,
$this->newLine();
}

if ($event->onOneServer) {
if ($this->dryRun) {
$this->outputDryRunMessage($event);
} elseif ($event->onOneServer) {
$this->runSingleServerEvent($event);
} else {
$this->runEvent($event);
Expand Down Expand Up @@ -254,7 +267,9 @@ protected function repeatEvents($events)
continue;
}

if ($event->onOneServer) {
if ($this->dryRun) {
$this->outputDryRunMessage($event);
} elseif ($event->onOneServer) {
$this->runSingleServerEvent($event);
} else {
$this->runEvent($event);
Expand Down Expand Up @@ -286,4 +301,25 @@ protected function clearInterruptSignal()
{
$this->cache->forget('illuminate:schedule:interrupt');
}

/**
* Output a formatted message indicating a scheduled task is skipped in dry-run mode.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return void
*/
private function outputDryRunMessage(Event $event)
{
$summary = $event->getSummaryForDisplay();

$command = $event instanceof CallbackEvent
? $summary
: trim(str_replace($this->phpBinary, '', $event->command));

$this->components->info(sprintf(
'<fg=gray>%s</> Skipping [%s], because the scheduler is started with the dry-run option.',
Carbon::now()->format('Y-m-d H:i:s'),
$command
));
}
}