3 final class NuanceItemUpdateWorker
6 protected function doWork() {
7 $item_phid = $this->getTaskDataValue('itemPHID');
9 $lock = $this->newLock($item_phid);
13 $item = $this->loadItem($item_phid);
14 $this->updateItem($item);
15 $this->routeItem($item);
16 $this->applyCommands($item);
17 } catch (Exception
$ex) {
25 private function updateItem(NuanceItem
$item) {
26 $impl = $item->getImplementation();
27 if (!$impl->canUpdateItems()) {
31 $viewer = $this->getViewer();
33 $impl->setViewer($viewer);
34 $impl->updateItem($item);
37 private function routeItem(NuanceItem
$item) {
38 $status = $item->getStatus();
39 if ($status != NuanceItem
::STATUS_ROUTING
) {
43 $source = $item->getSource();
45 // For now, always route items into the source's default queue.
48 ->setQueuePHID($source->getDefaultQueuePHID())
49 ->setStatus(NuanceItem
::STATUS_OPEN
)
53 private function applyCommands(NuanceItem
$item) {
54 $viewer = $this->getViewer();
56 $commands = id(new NuanceItemCommandQuery())
58 ->withItemPHIDs(array($item->getPHID()))
61 NuanceItemCommand
::STATUS_ISSUED
,
64 $commands = msort($commands, 'getID');
66 $this->executeCommandList($item, $commands);
69 public function executeCommands(NuanceItem
$item, array $commands) {
74 $item_phid = $item->getPHID();
75 $viewer = $this->getViewer();
77 $lock = $this->newLock($item_phid);
80 } catch (PhutilLockException
$ex) {
85 $item = $this->loadItem($item_phid);
87 // Reload commands now that we have a lock, to make sure we don't
88 // execute any commands twice by mistake.
89 $commands = id(new NuanceItemCommandQuery())
91 ->withIDs(mpull($commands, 'getID'))
94 $this->executeCommandList($item, $commands);
95 } catch (Exception
$ex) {
105 private function executeCommandList(NuanceItem
$item, array $commands) {
106 $viewer = $this->getViewer();
108 $executors = NuanceCommandImplementation
::getAllCommands();
109 foreach ($commands as $command) {
110 if ($command->getItemPHID() !== $item->getPHID()) {
112 pht('Trying to apply a command to the wrong item!'));
115 if ($command->getStatus() !== NuanceItemCommand
::STATUS_ISSUED
) {
116 // Never execute commands which have already been issued.
121 ->setStatus(NuanceItemCommand
::STATUS_EXECUTING
)
125 $command_key = $command->getCommand();
127 $executor = idx($executors, $command_key);
131 'Unable to execute command "%s": this command does not have '.
132 'a recognized command implementation.',
136 $executor = clone $executor;
140 ->applyCommand($item, $command);
143 ->setStatus(NuanceItemCommand
::STATUS_DONE
)
145 } catch (Exception
$ex) {
147 ->setStatus(NuanceItemCommand
::STATUS_FAILED
)
155 private function newLock($item_phid) {
156 $hash = PhabricatorHash
::digestForIndex($item_phid);
157 $lock_key = "nuance.item.{$hash}";
158 return PhabricatorGlobalLock
::newLock($lock_key);