Merge pull request #22023 from owncloud/interruptable-commands

Interruptable commands
This commit is contained in:
Thomas Müller 2016-02-02 14:07:07 +01:00
commit 8049b6e998
3 changed files with 55 additions and 61 deletions

View File

@ -26,20 +26,19 @@
namespace OCA\Files\Command; namespace OCA\Files\Command;
use OC\Core\Command\Base;
use OC\ForbiddenException; use OC\ForbiddenException;
use OCP\Files\StorageNotAvailableException; use OCP\Files\StorageNotAvailableException;
use Symfony\Component\Console\Command\Command; use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
class Scan extends Command { class Scan extends Base {
/** /** @var IUserManager $userManager */
* @var \OC\User\Manager $userManager
*/
private $userManager; private $userManager;
/** @var float */ /** @var float */
protected $execTime = 0; protected $execTime = 0;
@ -47,19 +46,15 @@ class Scan extends Command {
protected $foldersCounter = 0; protected $foldersCounter = 0;
/** @var int */ /** @var int */
protected $filesCounter = 0; protected $filesCounter = 0;
/** @var bool */
protected $interrupted = false;
/** @var bool */
protected $php_pcntl_signal = true;
public function __construct(IUserManager $userManager) {
public function __construct(\OC\User\Manager $userManager) {
$this->userManager = $userManager; $this->userManager = $userManager;
parent::__construct(); parent::__construct();
} }
protected function configure() { protected function configure() {
parent::configure();
$this $this
->setName('files:scan') ->setName('files:scan')
->setDescription('rescan filesystem') ->setDescription('rescan filesystem')
@ -96,7 +91,7 @@ class Scan extends Command {
protected function scanFiles($user, $path, $verbose, OutputInterface $output) { protected function scanFiles($user, $path, $verbose, OutputInterface $output) {
$scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger()); $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exeption # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
# printout and count # printout and count
if ($verbose) { if ($verbose) {
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
@ -118,13 +113,13 @@ class Scan extends Command {
}); });
# count only # count only
} else { } else {
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) {
$this->filesCounter += 1; $this->filesCounter += 1;
if ($this->hasBeenInterrupted()) { if ($this->hasBeenInterrupted()) {
throw new \Exception('ctrl-c'); throw new \Exception('ctrl-c');
} }
}); });
$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) {
$this->foldersCounter += 1; $this->foldersCounter += 1;
if ($this->hasBeenInterrupted()) { if ($this->hasBeenInterrupted()) {
throw new \Exception('ctrl-c'); throw new \Exception('ctrl-c');
@ -194,7 +189,7 @@ class Scan extends Command {
$path = $inputPath ? $inputPath : '/' . $user; $path = $inputPath ? $inputPath : '/' . $user;
$user_count += 1; $user_count += 1;
if ($this->userManager->userExists($user)) { if ($this->userManager->userExists($user)) {
# add an extra line when verbose is set to optical seperate users # add an extra line when verbose is set to optical separate users
if ($verbose) {$output->writeln(""); } if ($verbose) {$output->writeln(""); }
$output->writeln("Starting scan for user $user_count out of $users_total ($user)"); $output->writeln("Starting scan for user $user_count out of $users_total ($user)");
# full: printout data if $verbose was set # full: printout data if $verbose was set
@ -212,10 +207,8 @@ class Scan extends Command {
if (!$quiet) { if (!$quiet) {
$this->presentStats($output); $this->presentStats($output);
} }
} }
/** /**
* Initialises some useful tools for the Command * Initialises some useful tools for the Command
*/ */
@ -224,45 +217,7 @@ class Scan extends Command {
$this->execTime = -microtime(true); $this->execTime = -microtime(true);
// Convert PHP errors to exceptions // Convert PHP errors to exceptions
set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
// check if the php pcntl_signal functions are accessible
if (function_exists('pcntl_signal')) {
// Collect interrupts and notify the running command
pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
pcntl_signal(SIGINT, [$this, 'cancelOperation']);
} else {
$this->php_pcntl_signal = 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;
}
/**
* @return bool
*/
protected function hasBeenInterrupted() {
// return always false if pcntl_signal functions are not accessible
if ($this->php_pcntl_signal) {
pcntl_signal_dispatch();
if ($this->interrupted) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/** /**
* Processes PHP errors as exceptions in order to be able to keep track of problems * Processes PHP errors as exceptions in order to be able to keep track of problems
@ -284,7 +239,6 @@ class Scan extends Command {
throw new \ErrorException($message, 0, $severity, $file, $line); throw new \ErrorException($message, 0, $severity, $file, $line);
} }
/** /**
* @param OutputInterface $output * @param OutputInterface $output
*/ */
@ -300,7 +254,6 @@ class Scan extends Command {
$this->showSummary($headers, null, $output); $this->showSummary($headers, null, $output);
} }
/** /**
* Shows a summary of operations * Shows a summary of operations
* *
@ -332,10 +285,9 @@ class Scan extends Command {
*/ */
protected function formatExecTime() { protected function formatExecTime() {
list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime))); list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime)));
# add the following to $niceDate if you want to have microsecons added: . '.' . $tens;
$niceDate = date('H:i:s', $secs);
return $niceDate; # if you want to have microseconds add this: . '.' . $tens;
return date('H:i:s', $secs);
} }
} }

View File

@ -76,6 +76,10 @@ try {
exit(1); exit(1);
} }
if (!function_exists('pcntl_signal')) {
echo "The process control (PCNTL) extensions are required in case you want to interrupt long running commands - see http://php.net/manual/en/book.pcntl.php" . PHP_EOL;
}
$application = new Application(\OC::$server->getConfig()); $application = new Application(\OC::$server->getConfig());
$application->loadCommands(new ConsoleOutput()); $application->loadCommands(new ConsoleOutput());
$application->run(); $application->run();

View File

@ -33,6 +33,12 @@ class Base extends Command {
protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN;
/** @var boolean */
private $php_pcntl_signal = false;
/** @var boolean */
private $interrupted = false;
protected function configure() { protected function configure() {
$this $this
->addOption( ->addOption(
@ -43,6 +49,15 @@ class Base extends Command {
$this->defaultOutputFormat $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 $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;
}
} }