From d297057a95bd4304d3fdba9c7a564480f57ebb94 Mon Sep 17 00:00:00 2001 From: Florian Bosdorff Date: Tue, 28 May 2019 10:46:56 +0200 Subject: [PATCH 1/2] add option to enable/avoid error output --- src/Event.php | 21 ++++++++++++++++++++- src/ScheduleController.php | 8 +++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Event.php b/src/Event.php index 3dfec6a..22f55c3 100644 --- a/src/Event.php +++ b/src/Event.php @@ -87,6 +87,13 @@ class Event extends Component */ protected $_mutex; + /** + * Decide if errors will be displayed. + * + * @var bool + */ + protected $_omitErrors = false; + /** * Create a new event instance. * @@ -147,7 +154,7 @@ protected function runCommandInForeground(Application $app) */ public function buildCommand() { - $command = $this->command . $this->_redirect . $this->_output . ' 2>&1 &'; + $command = $this->command . $this->_redirect . $this->_output . ' ' . (($this->_omitErrors) ? '' : ' 2>&1 &'); return $this->_user ? 'sudo -u ' . $this->_user . ' ' . $command : $command; } @@ -505,6 +512,18 @@ public function user($user) return $this; } + /** + * Set if errors should be displayed + * + * @param bool $omitErrors + * @return $this + */ + public function omitErrors(bool $omitErrors = false) + { + $this->_omitErrors = $omitErrors; + return $this; + } + /** * Do not allow the event to overlap each other. * diff --git a/src/ScheduleController.php b/src/ScheduleController.php index bd42a6d..c2d5893 100644 --- a/src/ScheduleController.php +++ b/src/ScheduleController.php @@ -19,10 +19,15 @@ class ScheduleController extends Controller */ public $scheduleFile; + /** + * @var bool set to true to avoid error output + */ + public $omitErrors = false; + public function options($actionID) { return array_merge(parent::options($actionID), - $actionID == 'run' ? ['scheduleFile'] : [] + $actionID == 'run' ? ['scheduleFile', 'omitErrors'] : [] ); } @@ -45,6 +50,7 @@ public function actionRun() $events = $this->schedule->dueEvents(\Yii::$app); foreach ($events as $event) { + $event->omitErrors($this->omitErrors); $this->stdout('Running scheduled command: '.$event->getSummaryForDisplay()."\n"); $event->run(\Yii::$app); } From d57ec88762b859d6773a31c259579daeabc587e9 Mon Sep 17 00:00:00 2001 From: Florian Bosdorff Date: Tue, 28 May 2019 11:08:47 +0200 Subject: [PATCH 2/2] fix omitErrors interpretation in Event --- src/Event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event.php b/src/Event.php index 22f55c3..7d8a76a 100644 --- a/src/Event.php +++ b/src/Event.php @@ -154,7 +154,7 @@ protected function runCommandInForeground(Application $app) */ public function buildCommand() { - $command = $this->command . $this->_redirect . $this->_output . ' ' . (($this->_omitErrors) ? '' : ' 2>&1 &'); + $command = $this->command . $this->_redirect . $this->_output . ' ' . (($this->_omitErrors) ? ' 2>&1 &' : ''); return $this->_user ? 'sudo -u ' . $this->_user . ' ' . $command : $command; }