summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-01-29 15:59:57 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-02-01 09:47:13 +0100
commit0202acb926e308dc63aa5d052f30685555b03f65 (patch)
tree2d4f3e91295968e3131addfd8887a4431b1ace53 /core
parenta025b2865f54a586151f84d5259a41850252c21a (diff)
downloadnextcloud-server-0202acb926e308dc63aa5d052f30685555b03f65.tar.gz
nextcloud-server-0202acb926e308dc63aa5d052f30685555b03f65.zip
Move signal handling to the base class to allow other commands to reuse this feature
Diffstat (limited to 'core')
-rw-r--r--core/command/base.php38
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;
+ }
+
}