Commit 6571da15 authored by Vincent Petry's avatar Vincent Petry
Browse files

Merge pull request #24305 from owncloud/update-show-repair-step-progress

[Update] show repair step progress ...
parents 5b45f0f9 7ad1d491
......@@ -25,24 +25,36 @@
namespace OC\Core\Command\Maintenance;
use Exception;
use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class Repair extends Command {
/** @var \OC\Repair $repair */
protected $repair;
/** @var \OCP\IConfig */
/** @var IConfig */
protected $config;
/** @var EventDispatcherInterface */
private $dispatcher;
/** @var ProgressBar */
private $progress;
/** @var OutputInterface */
private $output;
/**
* @param \OC\Repair $repair
* @param \OCP\IConfig $config
* @param IConfig $config
*/
public function __construct(\OC\Repair $repair, \OCP\IConfig $config) {
public function __construct(\OC\Repair $repair, IConfig $config, EventDispatcherInterface $dispatcher) {
$this->repair = $repair;
$this->config = $config;
$this->dispatcher = $dispatcher;
parent::__construct();
}
......@@ -87,21 +99,48 @@ class Repair extends Command {
$maintenanceMode = $this->config->getSystemValue('maintenance', false);
$this->config->setSystemValue('maintenance', true);
$this->repair->listen('\OC\Repair', 'step', function ($description) use ($output) {
$output->writeln(' - ' . $description);
});
$this->repair->listen('\OC\Repair', 'info', function ($description) use ($output) {
$output->writeln(' - ' . $description);
});
$this->repair->listen('\OC\Repair', 'warning', function ($description) use ($output) {
$output->writeln(' - WARNING: ' . $description);
});
$this->repair->listen('\OC\Repair', 'error', function ($description) use ($output) {
$output->writeln(' - ERROR: ' . $description);
});
$this->progress = new ProgressBar($output);
$this->output = $output;
$this->dispatcher->addListener('\OC\Repair::startProgress', [$this, 'handleRepairFeedBack']);
$this->dispatcher->addListener('\OC\Repair::advance', [$this, 'handleRepairFeedBack']);
$this->dispatcher->addListener('\OC\Repair::finishProgress', [$this, 'handleRepairFeedBack']);
$this->dispatcher->addListener('\OC\Repair::step', [$this, 'handleRepairFeedBack']);
$this->dispatcher->addListener('\OC\Repair::info', [$this, 'handleRepairFeedBack']);
$this->dispatcher->addListener('\OC\Repair::warning', [$this, 'handleRepairFeedBack']);
$this->dispatcher->addListener('\OC\Repair::error', [$this, 'handleRepairFeedBack']);
$this->repair->run();
$this->config->setSystemValue('maintenance', $maintenanceMode);
}
public function handleRepairFeedBack($event) {
if (!$event instanceof GenericEvent) {
return;
}
switch ($event->getSubject()) {
case '\OC\Repair::startProgress':
$this->progress->start($event->getArgument(0));
break;
case '\OC\Repair::advance':
$this->progress->advance($event->getArgument(0));
break;
case '\OC\Repair::finishProgress':
$this->progress->finish();
$this->output->writeln('');
break;
case '\OC\Repair::step':
$this->output->writeln(' - ' . $event->getArgument(0));
break;
case '\OC\Repair::info':
$this->output->writeln(' - ' . $event->getArgument(0));
break;
case '\OC\Repair::warning':
$this->output->writeln(' - WARNING: ' . $event->getArgument(0));
break;
case '\OC\Repair::error':
$this->output->writeln(' - ERROR: ' . $event->getArgument(0));
break;
}
}
}
......@@ -162,8 +162,59 @@ class Upgrade extends Command {
}
}
};
$repairListener = function($event) use ($progress, $output) {
if (!$event instanceof GenericEvent) {
return;
}
switch ($event->getSubject()) {
case '\OC\Repair::startProgress':
$progress->setMessage('Starting ...');
$output->writeln($event->getArgument(1));
$output->writeln('');
$progress->start($event->getArgument(0));
break;
case '\OC\Repair::advance':
$desc = $event->getArgument(1);
if (!empty($desc)) {
$progress->setMessage($desc);
}
$progress->advance($event->getArgument(0));
break;
case '\OC\Repair::finishProgress':
$progress->setMessage('Done');
$progress->finish();
$output->writeln('');
break;
case '\OC\Repair::step':
if(OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$output->writeln('<info>Repair step: ' . $event->getArgument(0) . '</info>');
}
break;
case '\OC\Repair::info':
if(OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$output->writeln('<info>Repair info: ' . $event->getArgument(0) . '</info>');
}
break;
case '\OC\Repair::warning':
$output->writeln('<error>Repair warning: ' . $event->getArgument(0) . '</error>');
break;
case '\OC\Repair::error':
$output->writeln('<error>Repair error: ' . $event->getArgument(0) . '</error>');
break;
}
};
$dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener);
$dispatcher->addListener('\OC\DB\Migrator::checkTable', $listener);
$dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
$dispatcher->addListener('\OC\Repair::advance', $repairListener);
$dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
$dispatcher->addListener('\OC\Repair::step', $repairListener);
$dispatcher->addListener('\OC\Repair::info', $repairListener);
$dispatcher->addListener('\OC\Repair::warning', $repairListener);
$dispatcher->addListener('\OC\Repair::error', $repairListener);
$updater->listen('\OC\Updater', 'maintenanceEnabled', function () use($output) {
$output->writeln('<info>Turned on maintenance mode</info>');
......@@ -205,12 +256,6 @@ class Upgrade extends Command {
$updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($output) {
$output->writeln('<info>Update 3rd-party app: ' . $app . '</info>');
});
$updater->listen('\OC\Updater', 'repairWarning', function ($app) use($output) {
$output->writeln('<error>Repair warning: ' . $app . '</error>');
});
$updater->listen('\OC\Updater', 'repairError', function ($app) use($output) {
$output->writeln('<error>Repair error: ' . $app . '</error>');
});
$updater->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($output) {
$output->writeln('<info>Checking updates of apps</info>');
});
......@@ -242,15 +287,6 @@ class Upgrade extends Command {
$output->writeln("<info>Finished code integrity check</info>");
});
if(OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$updater->listen('\OC\Updater', 'repairInfo', function ($message) use($output) {
$output->writeln('<info>Repair info: ' . $message . '</info>');
});
$updater->listen('\OC\Updater', 'repairStep', function ($message) use($output) {
$output->writeln('<info>Repair step: ' . $message . '</info>');
});
}
$success = $updater->upgrade();
$this->postUpgradeCheck($input, $output);
......
......@@ -39,6 +39,56 @@ $eventSource = \OC::$server->createEventSource();
// message
$eventSource->send('success', (string)$l->t('Preparing update'));
class FeedBackHandler {
/** @var integer */
private $progressStateMax = 100;
/** @var integer */
private $progressStateStep = 0;
/** @var string */
private $currentStep;
public function __construct(\OCP\IEventSource $eventSource, \OCP\IL10N $l10n) {
$this->eventSource = $eventSource;
$this->l10n = $l10n;
}
public function handleRepairFeedback($event) {
if (!$event instanceof GenericEvent) {
return;
}
switch ($event->getSubject()) {
case '\OC\Repair::startProgress':
$this->progressStateMax = $event->getArgument(0);
$this->progressStateStep = 0;
$this->currentStep = $event->getArgument(1);
break;
case '\OC\Repair::advance':
$this->progressStateStep += $event->getArgument(0);
$desc = $event->getArgument(1);
if (empty($desc)) {
$desc = $this->currentStep;
}
$this->eventSource->send('success', (string)$this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
break;
case '\OC\Repair::finishProgress':
$this->progressStateMax = $this->progressStateStep;
$this->eventSource->send('success', (string)$this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
break;
case '\OC\Repair::step':
break;
case '\OC\Repair::info':
break;
case '\OC\Repair::warning':
$this->eventSource->send('notice', (string)$this->l10n->t('Repair warning: ') . $event->getArgument(0));
break;
case '\OC\Repair::error':
$this->eventSource->send('notice', (string)$this->l10n->t('Repair error: ') . $event->getArgument(0));
break;
}
}
}
if (OC::checkUpgrade(false)) {
$config = \OC::$server->getSystemConfig();
......@@ -73,6 +123,14 @@ if (OC::checkUpgrade(false)) {
$eventSource->send('success', (string)$l->t('[%d / %d]: Checking table %s', [$event[0], $event[1], $event->getSubject()]));
}
});
$feedBack = new FeedBackHandler($eventSource, $l);
$dispatcher->addListener('\OC\Repair::startProgress', [$feedBack, 'handleRepairFeedback']);
$dispatcher->addListener('\OC\Repair::advance', [$feedBack, 'handleRepairFeedback']);
$dispatcher->addListener('\OC\Repair::finishProgress', [$feedBack, 'handleRepairFeedback']);
$dispatcher->addListener('\OC\Repair::step', [$feedBack, 'handleRepairFeedback']);
$dispatcher->addListener('\OC\Repair::info', [$feedBack, 'handleRepairFeedback']);
$dispatcher->addListener('\OC\Repair::warning', [$feedBack, 'handleRepairFeedback']);
$dispatcher->addListener('\OC\Repair::error', [$feedBack, 'handleRepairFeedback']);
$updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
$eventSource->send('success', (string)$l->t('Turned on maintenance mode'));
......@@ -107,12 +165,6 @@ if (OC::checkUpgrade(false)) {
$updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
$eventSource->send('success', (string)$l->t('Updated "%s" to %s', array($app, $version)));
});
$updater->listen('\OC\Updater', 'repairWarning', function ($description) use ($eventSource, $l) {
$eventSource->send('notice', (string)$l->t('Repair warning: ') . $description);
});
$updater->listen('\OC\Updater', 'repairError', function ($description) use ($eventSource, $l) {
$eventSource->send('notice', (string)$l->t('Repair error: ') . $description);
});
$updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps) {
$incompatibleApps[]= $app;
});
......
......@@ -112,7 +112,9 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) {
$application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader()));
$application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector()));
$application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Maintenance\Repair(new \OC\Repair(\OC\Repair::getRepairSteps()), \OC::$server->getConfig()));
$application->add(new OC\Core\Command\Maintenance\Repair(
new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(),
\OC::$server->getEventDispatcher()));
$application->add(new OC\Core\Command\Maintenance\SingleUser(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger()));
......
......@@ -55,12 +55,15 @@ class DropOldTables implements IRepairStep {
* @throws \Exception in case of failure
*/
public function run(IOutput $output) {
$tables = $this->oldDatabaseTables();
$output->startProgress(count($tables));
foreach ($this->oldDatabaseTables() as $tableName) {
if ($this->connection->tableExists($tableName)){
$output->info(sprintf('Table %s has been deleted', $tableName));
$this->connection->dropTable($tableName);
}
$output->advance(1, "Drop old database table: $tableName");
}
$output->finishProgress();
}
/**
......
......@@ -28,7 +28,6 @@
namespace OC;
use OC\Hooks\BasicEmitter;
use OC\Hooks\Emitter;
use OC\Repair\AssetCache;
use OC\Repair\CleanTags;
......@@ -51,11 +50,13 @@ use OCP\Migration\IRepairStep;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
class Repair extends BasicEmitter implements IOutput{
class Repair implements IOutput{
/* @var IRepairStep[] */
private $repairSteps;
/** @var EventDispatcher */
private $dispatcher;
/** @var string */
private $currentStep;
/**
* Creates a new repair step runner
......@@ -79,7 +80,8 @@ class Repair extends BasicEmitter implements IOutput{
}
// run each repair step
foreach ($this->repairSteps as $step) {
$this->emit('\OC\Repair', 'step', array($step->getName()));
$this->currentStep = $step->getName();
$this->emit('\OC\Repair', 'step', [$this->currentStep]);
if ($step instanceof Emitter) {
$step->listen('\OC\Repair', 'warning', function ($description) use ($self) {
......@@ -178,10 +180,11 @@ class Repair extends BasicEmitter implements IOutput{
}
/**
* {@inheritDoc}
* @param string $scope
* @param string $method
* @param array $arguments
*/
public function emit($scope, $method, array $arguments = []) {
parent::emit($scope, $method, $arguments);
if (!is_null($this->dispatcher)) {
$this->dispatcher->dispatch("$scope::$method",
new GenericEvent("$scope::$method", $arguments));
......@@ -206,15 +209,16 @@ class Repair extends BasicEmitter implements IOutput{
*/
public function startProgress($max = 0) {
// for now just emit as we did in the past
$this->emit('\OC\Repair', 'startProgress', [$max]);
$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
}
/**
* @param int $step
* @param string $description
*/
public function advance($step = 1) {
public function advance($step = 1, $description = '') {
// for now just emit as we did in the past
$this->emit('\OC\Repair', 'advance', [$step]);
$this->emit('\OC\Repair', 'advance', [$step, $description]);
}
/**
......
......@@ -132,6 +132,8 @@ class Updater extends BasicEmitter {
* @return bool true if the operation succeeded, false otherwise
*/
public function upgrade() {
$this->emitRepairEvents();
$logLevel = $this->config->getSystemValue('loglevel', \OCP\Util::WARN);
$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
$this->config->setSystemValue('loglevel', \OCP\Util::DEBUG);
......@@ -195,26 +197,6 @@ class Updater extends BasicEmitter {
&& (version_compare($oldVersion, $newVersion, '<=') || $this->config->getSystemValue('debug', false)));
}
/**
* Forward messages emitted by the repair routine
*
* @param Repair $repair repair routine
*/
private function emitRepairMessages(Repair $repair) {
$repair->listen('\OC\Repair', 'warning', function ($description) {
$this->emit('\OC\Updater', 'repairWarning', array($description));
});
$repair->listen('\OC\Repair', 'error', function ($description) {
$this->emit('\OC\Updater', 'repairError', array($description));
});
$repair->listen('\OC\Repair', 'info', function ($description) {
$this->emit('\OC\Updater', 'repairInfo', array($description));
});
$repair->listen('\OC\Repair', 'step', function ($description) {
$this->emit('\OC\Updater', 'repairStep', array($description));
});
}
/**
* runs the update actions in maintenance mode, does not upgrade the source files
* except the main .htaccess file
......@@ -245,8 +227,7 @@ class Updater extends BasicEmitter {
file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
// pre-upgrade repairs
$repair = new Repair(Repair::getBeforeUpgradeRepairSteps());
$this->emitRepairMessages($repair);
$repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher());
$repair->run();
// simulate DB upgrade
......@@ -278,8 +259,7 @@ class Updater extends BasicEmitter {
}
// post-upgrade repairs
$repair = new Repair(Repair::getRepairSteps());
$this->emitRepairMessages($repair);
$repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher());
$repair->run();
//Invalidate update feed
......@@ -362,7 +342,6 @@ class Updater extends BasicEmitter {
* @throws NeedsUpdateException
*/
protected function doAppUpgrade() {
$this->emitRepairEvents();
$apps = \OC_App::getEnabledApps();
$priorityTypes = array('authentication', 'filesystem', 'logging');
$pseudoOtherType = 'other';
......
......@@ -48,9 +48,10 @@ interface IOutput {
/**
* @param int $step
* @param string $description
* @since 9.1.0
*/
public function advance($step = 1);
public function advance($step = 1, $description = '');
/**
* @param int $max
......
......@@ -6,7 +6,10 @@
* See the COPYING-README file.
*/
namespace Test;
use OCP\Migration\IRepairStep;
use Symfony\Component\EventDispatcher\EventDispatcher;
class TestRepairStep implements IRepairStep {
private $warning;
......@@ -29,91 +32,77 @@ class TestRepairStep implements IRepairStep {
}
}
class Test_Repair extends \Test\TestCase {
public function testRunRepairStep() {
$output = array();
class RepairTest extends TestCase {
/** @var \OC\Repair */
private $repair;
/** @var string[] */
private $outputArray;
$repair = new \OC\Repair();
$repair->addStep(new TestRepairStep(false));
public function setUp() {
parent::setUp();
$dispatcher = new EventDispatcher();
$this->repair = new \OC\Repair([], $dispatcher);
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
$output[] = 'warning: ' . $description;
$dispatcher->addListener('\OC\Repair::warning', function ($event) {
/** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
$this->outputArray[] = 'warning: ' . $event->getArgument(0);
});
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
$output[] = 'info: ' . $description;
$dispatcher->addListener('\OC\Repair::info', function ($event) {
/** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
$this->outputArray[] = 'info: ' . $event->getArgument(0);
});
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
$output[] = 'step: ' . $description;
$dispatcher->addListener('\OC\Repair::step', function ($event) {
/** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
$this->outputArray[] = 'step: ' . $event->getArgument(0);
});
}
public function testRunRepairStep() {
$repair->run();
$this->repair->addStep(new TestRepairStep(false));
$this->repair->run();
$this->assertEquals(
array(
'step: Test Name',
'info: Simulated info',
),
$output
$this->outputArray
);
}
public function testRunRepairStepThatFail() {
$output = array();
$repair = new \OC\Repair();
$repair->addStep(new TestRepairStep(true));
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
$output[] = 'warning: ' . $description;
});
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
$output[] = 'info: ' . $description;
});
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
$output[] = 'step: ' . $description;
});
$repair->run();
$this->repair->addStep(new TestRepairStep(true));
$this->repair->run();
$this->assertEquals(
array(
'step: Test Name',
'warning: Simulated warning',
),
$output
$this->outputArray
);
}
public function testRunRepairStepsWithException() {
$output = array();
$mock = $this->getMock('TestRepairStep');
$mock = $this->getMock('\Test\TestRepairStep');
$mock->expects($this->any())
->method('run')
->will($this->throwException(new Exception));
->will($this->throwException(new \Exception()));
$mock->expects($this->any())
->method('getName')
->will($this->returnValue('Exception Test'));
$repair = new \OC\Repair();
$repair->addStep($mock);
$repair->addStep(new TestRepairStep(false));
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
$output[] = 'warning: ' . $description;
});
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
$output[] = 'info: ' . $description;
});
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
$output[] = 'step: ' . $description;
});
$this->repair->addStep($mock);
$this->repair->addStep(new TestRepairStep(false));
$thrown = false;
try {
$repair->run();
$this->repair->run();
}
catch (Exception $e) {
catch (\Exception $e) {
$thrown = true;
}
......@@ -123,28 +112,14 @@ class Test_Repair extends \Test\TestCase {
array(
'step: Exception Test',
),
$output
$this->outputArray
);
}
public function testRunRepairStepsContinueAfterWarning() {
$output = array();
$repair = new \OC\Repair();
$repair->addStep(new TestRepairStep(true));
$repair->addStep(new TestRepairStep(false));
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
$output[] = 'warning: ' . $description;
});
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
$output[] = 'info: ' . $description;
});
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
$output[] = 'step: ' . $description;
});