Changeset View
Changeset View
Standalone View
Standalone View
src/applications/nuance/worker/NuanceItemUpdateWorker.php
| <?php | <?php | ||||
| final class NuanceItemUpdateWorker | final class NuanceItemUpdateWorker | ||||
| extends NuanceWorker { | extends NuanceWorker { | ||||
| protected function doWork() { | protected function doWork() { | ||||
| $item_phid = $this->getTaskDataValue('itemPHID'); | $item_phid = $this->getTaskDataValue('itemPHID'); | ||||
| $hash = PhabricatorHash::digestForIndex($item_phid); | $lock = $this->newLock($item_phid); | ||||
| $lock_key = "nuance.item.{$hash}"; | |||||
| $lock = PhabricatorGlobalLock::newLock($lock_key); | |||||
| $lock->lock(1); | $lock->lock(1); | ||||
| try { | try { | ||||
| $item = $this->loadItem($item_phid); | $item = $this->loadItem($item_phid); | ||||
| $this->updateItem($item); | $this->updateItem($item); | ||||
| $this->routeItem($item); | $this->routeItem($item); | ||||
| $this->applyCommands($item); | $this->applyCommands($item); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| Show All 30 Lines | $item | ||||
| ->setQueuePHID($source->getDefaultQueuePHID()) | ->setQueuePHID($source->getDefaultQueuePHID()) | ||||
| ->setStatus(NuanceItem::STATUS_OPEN) | ->setStatus(NuanceItem::STATUS_OPEN) | ||||
| ->save(); | ->save(); | ||||
| } | } | ||||
| private function applyCommands(NuanceItem $item) { | private function applyCommands(NuanceItem $item) { | ||||
| $viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
| $impl = $item->getImplementation(); | |||||
| $impl->setViewer($viewer); | |||||
| $commands = id(new NuanceItemCommandQuery()) | $commands = id(new NuanceItemCommandQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withItemPHIDs(array($item->getPHID())) | ->withItemPHIDs(array($item->getPHID())) | ||||
| ->withStatuses( | ->withStatuses( | ||||
| array( | array( | ||||
| NuanceItemCommand::STATUS_ISSUED, | NuanceItemCommand::STATUS_ISSUED, | ||||
| )) | )) | ||||
| ->execute(); | ->execute(); | ||||
| $commands = msort($commands, 'getID'); | $commands = msort($commands, 'getID'); | ||||
| $this->executeCommandList($item, $commands); | |||||
| } | |||||
| public function executeCommands(NuanceItem $item, array $commands) { | |||||
| if (!$commands) { | |||||
| return true; | |||||
| } | |||||
| $item_phid = $item->getPHID(); | |||||
| $viewer = $this->getViewer(); | |||||
| $lock = $this->newLock($item_phid); | |||||
| try { | |||||
| $lock->lock(1); | |||||
| } catch (PhutilLockException $ex) { | |||||
| return false; | |||||
| } | |||||
| try { | |||||
| $item = $this->loadItem($item_phid); | |||||
| // Reload commands now that we have a lock, to make sure we don't | |||||
| // execute any commands twice by mistake. | |||||
| $commands = id(new NuanceItemCommandQuery()) | |||||
| ->setViewer($viewer) | |||||
| ->withIDs(mpull($commands, 'getID')) | |||||
| ->execute(); | |||||
| $this->executeCommandList($item, $commands); | |||||
| } catch (Exception $ex) { | |||||
| $lock->unlock(); | |||||
| throw $ex; | |||||
| } | |||||
| $lock->unlock(); | |||||
| return true; | |||||
| } | |||||
| private function executeCommandList(NuanceItem $item, array $commands) { | |||||
| $viewer = $this->getViewer(); | |||||
| $executors = NuanceCommandImplementation::getAllCommands(); | $executors = NuanceCommandImplementation::getAllCommands(); | ||||
| foreach ($commands as $command) { | foreach ($commands as $command) { | ||||
| if ($command->getItemPHID() !== $item->getPHID()) { | |||||
| throw new Exception( | |||||
| pht('Trying to apply a command to the wrong item!')); | |||||
| } | |||||
| if ($command->getStatus() !== NuanceItemCommand::STATUS_ISSUED) { | |||||
| // Never execute commands which have already been issued. | |||||
| continue; | |||||
| } | |||||
| $command | $command | ||||
| ->setStatus(NuanceItemCommand::STATUS_EXECUTING) | ->setStatus(NuanceItemCommand::STATUS_EXECUTING) | ||||
| ->save(); | ->save(); | ||||
| try { | try { | ||||
| $command_key = $command->getCommand(); | $command_key = $command->getCommand(); | ||||
| $executor = idx($executors, $command_key); | $executor = idx($executors, $command_key); | ||||
| Show All 19 Lines | foreach ($commands as $command) { | ||||
| ->setStatus(NuanceItemCommand::STATUS_FAILED) | ->setStatus(NuanceItemCommand::STATUS_FAILED) | ||||
| ->save(); | ->save(); | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| private function newLock($item_phid) { | |||||
| $hash = PhabricatorHash::digestForIndex($item_phid); | |||||
| $lock_key = "nuance.item.{$hash}"; | |||||
| return PhabricatorGlobalLock::newLock($lock_key); | |||||
| } | |||||
| } | } | ||||