From 0202acb926e308dc63aa5d052f30685555b03f65 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Fri, 29 Jan 2016 15:59:57 +0100 Subject: Move signal handling to the base class to allow other commands to reuse this feature --- core/command/base.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'core') diff --git a/core/command/base.php b/core/command/base.php index bc5ae2e429b..a34d7ec1c9a 100644 --- a/core/command/base.php +++ b/core/command/base.php @@ -33,6 +33,12 @@ class Base extends Command { protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; + /** @var boolean */ + private $php_pcntl_signal = false; + + /** @var boolean */ + private $interrupted = false; + protected function configure() { $this ->addOption( @@ -43,6 +49,15 @@ class Base extends Command { $this->defaultOutputFormat ) ; + + // check if the php pcntl_signal functions are accessible + $this->php_pcntl_signal = function_exists('pcntl_signal'); + if ($this->php_pcntl_signal) { + // Collect interrupts and notify the running command + pcntl_signal(SIGTERM, [$this, 'cancelOperation']); + pcntl_signal(SIGINT, [$this, 'cancelOperation']); + } + } /** @@ -116,4 +131,27 @@ class Base extends Command { return $value; } } + + /** + * @return bool + */ + protected function hasBeenInterrupted() { + // return always false if pcntl_signal functions are not accessible + if ($this->php_pcntl_signal) { + pcntl_signal_dispatch(); + return $this->interrupted; + } else { + return false; + } + } + + /** + * Changes the status of the command to "interrupted" if ctrl-c has been pressed + * + * Gives a chance to the command to properly terminate what it's doing + */ + private function cancelOperation() { + $this->interrupted = true; + } + } -- cgit v1.2.3