Differential D17572 Diff 42279 src/applications/search/management/PhabricatorSearchManagementInitWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/search/management/PhabricatorSearchManagementInitWorkflow.php
<?php | <?php | ||||
final class PhabricatorSearchManagementInitWorkflow | final class PhabricatorSearchManagementInitWorkflow | ||||
extends PhabricatorSearchManagementWorkflow { | extends PhabricatorSearchManagementWorkflow { | ||||
protected function didConstruct() { | protected function didConstruct() { | ||||
$this | $this | ||||
->setName('init') | ->setName('init') | ||||
->setSynopsis(pht('Initialize or repair an index.')) | ->setSynopsis(pht('Initialize or repair a search service.')) | ||||
->setExamples('**init**'); | ->setExamples('**init**'); | ||||
} | } | ||||
public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
$console = PhutilConsole::getConsole(); | |||||
$work_done = false; | $work_done = false; | ||||
foreach (PhabricatorSearchService::getAllServices() as $service) { | foreach (PhabricatorSearchService::getAllServices() as $service) { | ||||
$console->writeOut( | echo tsprintf( | ||||
"%s\n", | "%s\n", | ||||
pht('Initializing search service "%s"', $service->getDisplayName())); | pht( | ||||
'Initializing search service "%s".', | |||||
$service->getDisplayName())); | |||||
try { | if (!$service->isWritable()) { | ||||
$host = $service->getAnyHostForRole('write'); | echo tsprintf( | ||||
} catch (PhabricatorClusterNoHostForRoleException $e) { | "%s\n", | ||||
// If there are no writable hosts for a given cluster, skip it | pht( | ||||
$console->writeOut("%s\n", $e->getMessage()); | 'Skipping service "%s" because it is not writable.', | ||||
$service->getDisplayName())); | |||||
continue; | continue; | ||||
} | } | ||||
$engine = $host->getEngine(); | $engine = $service->getEngine(); | ||||
if (!$engine->indexExists()) { | if (!$engine->indexExists()) { | ||||
$console->writeOut( | echo tsprintf( | ||||
'%s', | |||||
pht('Index does not exist, creating...')); | |||||
$engine->initIndex(); | |||||
$console->writeOut( | |||||
"%s\n", | "%s\n", | ||||
pht('done.')); | pht('Service index does not exist, creating...')); | ||||
$engine->initIndex(); | |||||
$work_done = true; | $work_done = true; | ||||
} else if (!$engine->indexIsSane()) { | } else if (!$engine->indexIsSane()) { | ||||
$console->writeOut( | echo tsprintf( | ||||
'%s', | |||||
pht('Index exists but is incorrect, fixing...')); | |||||
$engine->initIndex(); | |||||
$console->writeOut( | |||||
"%s\n", | "%s\n", | ||||
pht('done.')); | pht('Service index is out of date, repairing...')); | ||||
$engine->initIndex(); | |||||
$work_done = true; | $work_done = true; | ||||
} | } else { | ||||
echo tsprintf( | |||||
"%s\n", | |||||
pht('Service index is already up to date.')); | |||||
} | } | ||||
if ($work_done) { | echo tsprintf( | ||||
$console->writeOut( | |||||
"%s\n", | "%s\n", | ||||
pht( | pht('Done.')); | ||||
'Index maintenance complete. Run `%s` to reindex documents', | } | ||||
'./bin/search index')); | |||||
} else { | if (!$work_done) { | ||||
$console->writeOut( | echo tsprintf( | ||||
"%s\n", | "%s\n", | ||||
pht('Nothing to do.')); | pht('No services need initialization.')); | ||||
return 0; | |||||
} | } | ||||
echo tsprintf( | |||||
"%s\n", | |||||
pht('Service initialization complete.')); | |||||
} | } | ||||
} | } |