Changeset View
Changeset View
Standalone View
Standalone View
src/applications/search/worker/PhabricatorSearchWorker.php
<?php | <?php | ||||
final class PhabricatorSearchWorker extends PhabricatorWorker { | final class PhabricatorSearchWorker extends PhabricatorWorker { | ||||
public static function queueDocumentForIndexing($phid, $context = null) { | |||||
parent::scheduleTask( | |||||
__CLASS__, | |||||
array( | |||||
'documentPHID' => $phid, | |||||
'context' => $context, | |||||
), | |||||
array( | |||||
'priority' => parent::PRIORITY_IMPORT, | |||||
)); | |||||
} | |||||
protected function doWork() { | protected function doWork() { | ||||
$data = $this->getTaskData(); | $data = $this->getTaskData(); | ||||
$object_phid = idx($data, 'documentPHID'); | |||||
$phid = idx($data, 'documentPHID'); | |||||
$context = idx($data, 'context'); | $context = idx($data, 'context'); | ||||
$engine = new PhabricatorIndexEngine(); | |||||
$key = "index.{$object_phid}"; | |||||
$lock = PhabricatorGlobalLock::newLock($key); | |||||
$lock->lock(1); | |||||
try { | try { | ||||
id(new PhabricatorSearchIndexer()) | $object = $this->loadObjectForIndexing($object_phid); | ||||
->indexDocumentByPHID($phid, $context); | |||||
$engine->indexDocumentByPHID($object->getPHID(), $context); | |||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
throw new PhabricatorWorkerPermanentFailureException( | $lock->unlock(); | ||||
if (!($ex instanceof PhabricatorWorkerPermanentFailureException)) { | |||||
$ex = new PhabricatorWorkerPermanentFailureException( | |||||
pht( | pht( | ||||
'Failed to update search index for document "%s": %s', | 'Failed to update search index for document "%s": %s', | ||||
$phid, | $object_phid, | ||||
$ex->getMessage())); | $ex->getMessage())); | ||||
} | } | ||||
throw $ex; | |||||
} | |||||
$lock->unlock(); | |||||
} | |||||
private function loadObjectForIndexing($phid) { | |||||
$viewer = PhabricatorUser::getOmnipotentUser(); | |||||
$object = id(new PhabricatorObjectQuery()) | |||||
->setViewer($viewer) | |||||
->withPHIDs(array($phid)) | |||||
->executeOne(); | |||||
if (!$object) { | |||||
throw new PhabricatorWorkerPermanentFailureException( | |||||
pht( | |||||
'Unable to load object "%s" to rebuild indexes.', | |||||
$phid)); | |||||
} | |||||
return $object; | |||||
} | } | ||||
} | } |