diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-02 14:07:07 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-02 14:07:07 +0100 |
commit | 8049b6e9984e1eeb2eeac388be8fc473b0b60cc0 (patch) | |
tree | dcab75f624944c9b7777ebd155f35d789fcf67e5 /core | |
parent | ce053b980860070957bedb13e6fcd9d45fe50b9b (diff) | |
parent | 79d2f3186cad9d2412166c9bd414d73dd97cb3d3 (diff) | |
download | nextcloud-server-8049b6e9984e1eeb2eeac388be8fc473b0b60cc0.tar.gz nextcloud-server-8049b6e9984e1eeb2eeac388be8fc473b0b60cc0.zip |
Merge pull request #22023 from owncloud/interruptable-commands
Interruptable commands
Diffstat (limited to 'core')
-rw-r--r-- | core/command/base.php | 38 |
1 files changed, 38 insertions, 0 deletions
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; + } + } |