3 final class PhabricatorSearchWorker
extends PhabricatorWorker
{
5 public static function queueDocumentForIndexing(
10 if ($parameters === null) {
11 $parameters = array();
17 'documentPHID' => $phid,
18 'parameters' => $parameters,
19 'strict' => $is_strict,
22 'priority' => parent
::PRIORITY_INDEX
,
23 'objectPHID' => $phid,
27 protected function doWork() {
28 $data = $this->getTaskData();
29 $object_phid = idx($data, 'documentPHID');
31 // See T12425. By the time we run an indexing task, the object it indexes
32 // may have been deleted. This is unusual, but not concerning, and failing
33 // to index these objects is correct.
35 // To avoid showing these non-actionable errors to users, don't report
36 // indexing exceptions unless we're in "strict" mode. This mode is set by
37 // the "bin/search index" tool.
39 $is_strict = idx($data, 'strict', false);
42 $object = $this->loadObjectForIndexing($object_phid);
43 } catch (PhabricatorWorkerPermanentFailureException
$ex) {
51 $engine = id(new PhabricatorIndexEngine())
54 $parameters = idx($data, 'parameters', array());
55 $engine->setParameters($parameters);
57 if (!$engine->shouldIndexObject()) {
61 $lock = PhabricatorGlobalLock
::newLock(
64 'objectPHID' => $object_phid,
69 } catch (PhutilLockException
$ex) {
70 // If we fail to acquire the lock, just yield. It's expected that we may
71 // contend on this lock occasionally if a large object receives many
72 // updates in a short period of time, and it's appropriate to just retry
73 // rebuilding the index later.
74 throw new PhabricatorWorkerYieldException(15);
79 // Reload the object now that we have a lock, to make sure we have the
80 // most current version.
81 $object = $this->loadObjectForIndexing($object->getPHID());
83 $engine->setObject($object);
84 $engine->indexObject();
85 } catch (Exception
$ex) {
89 // Release the lock before we deal with the exception.
93 if (!($caught instanceof PhabricatorWorkerPermanentFailureException
)) {
94 $caught = new PhabricatorWorkerPermanentFailureException(
96 'Failed to update search index for document "%s": %s',
98 $caught->getMessage()));
107 private function loadObjectForIndexing($phid) {
108 $viewer = PhabricatorUser
::getOmnipotentUser();
110 $object = id(new PhabricatorObjectQuery())
112 ->withPHIDs(array($phid))
116 throw new PhabricatorWorkerPermanentFailureException(
118 'Unable to load object "%s" to rebuild indexes.',