From 6a0c412ff3c422262d1b8e7a8760a174433d46fa Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 30 Oct 2018 22:54:33 +0100 Subject: Exit process on Ctrl-C Signed-off-by: Maxence Lange --- lib/ACommandBase.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ lib/Command/Index.php | 18 ++++++++++++------ lib/Command/Live.php | 17 +++++++++++++---- lib/Command/Reset.php | 16 +++++++++++++--- lib/Model/Runner.php | 27 ++++++++++++--------------- 5 files changed, 95 insertions(+), 28 deletions(-) create mode 100644 lib/ACommandBase.php (limited to 'lib') diff --git a/lib/ACommandBase.php b/lib/ACommandBase.php new file mode 100644 index 0000000..56d5129 --- /dev/null +++ b/lib/ACommandBase.php @@ -0,0 +1,45 @@ + + * @copyright 2018 + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\FullTextSearch; + + +use OC\Core\Command\Base; + + +/** + * Abstract class ICommandBase + */ +abstract class ACommandBase extends Base { + + abstract public function abort(); + +} + diff --git a/lib/Command/Index.php b/lib/Command/Index.php index f6c5105..a80d1ef 100644 --- a/lib/Command/Index.php +++ b/lib/Command/Index.php @@ -27,8 +27,9 @@ namespace OCA\FullTextSearch\Command; use Exception; +use OC\Core\Command\InterruptedException; +use OCA\FullTextSearch\ACommandBase; use OCA\FullTextSearch\Exceptions\TickDoesNotExistException; -use OC\Core\Command\Base; use OCA\FullTextSearch\Model\Index as ModelIndex; use OCA\FullTextSearch\Model\IndexOptions; use OCA\FullTextSearch\Model\Runner; @@ -47,7 +48,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Terminal; -class Index extends Base { +class Index extends ACommandBase { // '%job:1s%%message:-40s%%current:6s%/%max:6s% [%bar%] %percent:3s%% \n %duration% %infos:-12s% %jvm:-30s% ' const PANEL_RUN = 'run'; @@ -176,8 +177,6 @@ class Index extends Base { } - - /** * @param InputInterface $input * @param OutputInterface $output @@ -215,7 +214,7 @@ class Index extends Base { $this->runner->setInfo('options', json_encode($options)); try { - $this->runner->sourceIsCommandLine($output); + $this->runner->sourceIsCommandLine($this, $output); $this->runner->start(); if ($options->getOption('errors') === 'reset') { @@ -823,7 +822,14 @@ class Index extends Base { $this->displayError(); } -} + + /** + * @throws InterruptedException + */ + public function abort() { + $this->abortIfInterrupted(); + } +} diff --git a/lib/Command/Live.php b/lib/Command/Live.php index dc1a187..ffdcc40 100644 --- a/lib/Command/Live.php +++ b/lib/Command/Live.php @@ -27,7 +27,8 @@ namespace OCA\FullTextSearch\Command; use Exception; -use OC\Core\Command\Base; +use OC\Core\Command\InterruptedException; +use OCA\FullTextSearch\ACommandBase; use OCA\FullTextSearch\Exceptions\TickDoesNotExistException; use OCA\FullTextSearch\Model\Index as ModelIndex; use OCA\FullTextSearch\Model\Runner; @@ -45,7 +46,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Terminal; -class Live extends Base { +class Live extends ACommandBase { const CYCLE_DELAY = 300000; @@ -216,7 +217,7 @@ class Live extends Base { try { - $this->runner->sourceIsCommandLine($output); + $this->runner->sourceIsCommandLine($this, $output); $this->runner->start(); $this->cliService->runDisplay($output); @@ -720,7 +721,15 @@ class Live extends Base { $this->displayError(); } -} + /** + * @throws InterruptedException + */ + public function abort() { + $this->abortIfInterrupted(); + } + + +} diff --git a/lib/Command/Reset.php b/lib/Command/Reset.php index 42349e1..8d87b0d 100644 --- a/lib/Command/Reset.php +++ b/lib/Command/Reset.php @@ -27,7 +27,8 @@ namespace OCA\FullTextSearch\Command; use Exception; -use OC\Core\Command\Base; +use OC\Core\Command\InterruptedException; +use OCA\FullTextSearch\ACommandBase; use OCA\FullTextSearch\Model\Runner; use OCA\FullTextSearch\Service\IndexService; use OCA\FullTextSearch\Service\MiscService; @@ -37,7 +38,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class Reset extends Base { +class Reset extends ACommandBase { /** @var IndexService */ private $indexService; @@ -85,7 +86,7 @@ class Reset extends Base { protected function execute(InputInterface $input, OutputInterface $output) { try { - $this->runner->sourceIsCommandLine($output); + $this->runner->sourceIsCommandLine($this, $output); $this->runner->start(); $this->runner->output('reset.'); @@ -119,6 +120,15 @@ class Reset extends Base { return $providerId; } + + + /** + * @throws InterruptedException + */ + public function abort() { + $this->abortIfInterrupted(); + } + } diff --git a/lib/Model/Runner.php b/lib/Model/Runner.php index 04f1d4e..3106573 100644 --- a/lib/Model/Runner.php +++ b/lib/Model/Runner.php @@ -27,6 +27,7 @@ namespace OCA\FullTextSearch\Model; +use OCA\FullTextSearch\ACommandBase; use OCA\FullTextSearch\Exceptions\RunnerAlreadyUpException; use OCA\FullTextSearch\Exceptions\TickDoesNotExistException; use OCA\FullTextSearch\Exceptions\TickIsNotAliveException; @@ -57,6 +58,9 @@ class Runner implements IRunner { /** @var int */ private $tickId; + /** @var ACommandBase */ + private $base = null; + /** @var OutputInterface */ private $outputInterface = null; @@ -131,6 +135,9 @@ class Runner implements IRunner { * @throws \Exception */ public function updateAction(string $action = '', bool $force = false): string { + + $this->base->abort(); + $n = ''; if (sizeof($this->methodOnKeyPress) > 0) { $n = fread(STDIN, 9999); @@ -145,13 +152,6 @@ class Runner implements IRunner { } $tick = time(); -// try { -// $this->hasBeenInterrupted(); -// } catch (InterruptException $e) { -// $this->stop(); -// throw $e; -// } - if ($this->oldAction !== $action || $force) { while (true) { if (!$this->isPaused()) { @@ -163,7 +163,9 @@ class Runner implements IRunner { if ($pressed === $this->keys['nextStep']) { break; } + usleep(300000); + $this->base->abort(); } $this->pauseRunning(false); @@ -408,9 +410,11 @@ class Runner implements IRunner { /** + * @param ACommandBase $base * @param OutputInterface $output */ - public function sourceIsCommandLine(OutputInterface $output) { + public function sourceIsCommandLine(ACommandBase $base, OutputInterface $output) { + $this->base = $base; $this->outputInterface = $output; } @@ -445,13 +449,6 @@ class Runner implements IRunner { } -// /** -// * @return bool -// */ -// public function isStrict() { -// return $this->strict; -// } - /** * @param string $line */ -- cgit v1.2.3