-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds improvements to the scheduler. Previously, when ->runInBackground() was used "after" hooks were not run, meaning output was not e-mailed to the developer (when using emailOutputTo. This change provides a new, hidden schedule:finish command that is used to fire the after callbacks for a given command by its mutex name. This command will not show in the Artisan console command list since it uses the hidden command feature which is new in Symfony 3.2.
- Loading branch information
1 parent
b34a148
commit 42a851a
Showing
6 changed files
with
170 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console\Scheduling; | ||
|
||
use Illuminate\Console\Application; | ||
use Symfony\Component\Process\ProcessUtils; | ||
|
||
class CommandBuilder | ||
{ | ||
/** | ||
* Build the command for the given event. | ||
* | ||
* @param \Illuminate\Console\Scheduling\Event $event | ||
* @return string | ||
*/ | ||
public function buildCommand(Event $event) | ||
{ | ||
if ($event->runInBackground) { | ||
return $this->buildBackgroundCommand($event); | ||
} else { | ||
return $this->buildForegroundCommand($event); | ||
} | ||
} | ||
|
||
/** | ||
* Build the command for running the event in the foreground. | ||
* | ||
* @param \Illuminate\Console\Scheduling\Event $event | ||
* @return string | ||
*/ | ||
protected function buildForegroundCommand(Event $event) | ||
{ | ||
$output = ProcessUtils::escapeArgument($event->output); | ||
|
||
return $this->finalize($event, $event->command.($event->shouldAppendOutput ? ' >> ' : ' > ').$output.' 2>&1'); | ||
} | ||
|
||
/** | ||
* Build the command for running the event in the foreground. | ||
* | ||
* @param \Illuminate\Console\Scheduling\Event $event | ||
* @return string | ||
*/ | ||
protected function buildBackgroundCommand(Event $event) | ||
{ | ||
$output = ProcessUtils::escapeArgument($event->output); | ||
|
||
$redirect = $event->shouldAppendOutput ? ' >> ' : ' > '; | ||
|
||
$finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"'; | ||
|
||
return $this->finalize($event, | ||
'('.$event->command.$redirect.$output.' 2>&1 '.(windows_os() ? '&' : ';').' '.$finished.') > ' | ||
.ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &' | ||
); | ||
} | ||
|
||
/** | ||
* Finalize the event's command syntax. | ||
* | ||
* @param \Illuminate\Console\Scheduling\Event $event | ||
* @param string $command | ||
* @return string | ||
*/ | ||
protected function finalize(Event $event, $command) | ||
{ | ||
return $event->user && ! windows_os() ? 'sudo -u '.$event->user.' -- sh -c \''.$command.'\'' : $command; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console\Scheduling; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class ScheduleFinishCommand extends Command | ||
{ | ||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'schedule:finish {id}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Handle the completion of a scheduled command'; | ||
|
||
/** | ||
* Indicates whether the command should be shown in the Artisan command list. | ||
* | ||
* @var bool | ||
*/ | ||
protected $hidden = true; | ||
|
||
/** | ||
* The schedule instance. | ||
* | ||
* @var \Illuminate\Console\Scheduling\Schedule | ||
*/ | ||
protected $schedule; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @param \Illuminate\Console\Scheduling\Schedule $schedule | ||
* @return void | ||
*/ | ||
public function __construct(Schedule $schedule) | ||
{ | ||
$this->schedule = $schedule; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function fire() | ||
{ | ||
collect($this->schedule->events())->filter(function ($value) { | ||
return $value->mutexName() == $this->argument('id'); | ||
})->each->callAfterCallbacks($this->laravel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters