Example #1
0
 public function execute()
 {
     $loop = true;
     $offset = 0;
     while ($loop) {
         // 1. Find record
         $request = new Request([Request::EXTRA_OFFSET => $offset, 'externalTypeId' => $this->externalTypeId]);
         $this->runUseCaseWithNoOfRetriesOnFail('task|retrieveOneToProcess', $request, $this->getMaxRetries());
         $response = $this->getUseCaseResponse();
         if ($response->getStatus() == Response::STATUS_FAIL) {
             $this->lastOperationSuccess = false;
             return;
         }
         $result = $response->getResult();
         // 2. If there are no assigned records then, return
         $totalResultCount = $response->getTotalResultCount();
         if (empty($totalResultCount) || !isset($result[0])) {
             $loop = false;
             continue;
         }
         if ($offset >= $totalResultCount) {
             $offset = 0;
         }
         // 3. Get the id
         $id = $result[0]->getId();
         // 4. Lock by id
         $lockId = ActManager::ID_TYPE . '-' . $id;
         $this->createLock($lockId);
         $response = $this->getUseCaseResponse();
         // 5. If it could not lock, then exit
         if ($response->getStatus() == Response::STATUS_FAIL) {
             $this->lastOperationSuccess = false;
             return;
         }
         // 6. If conflict then increase offset
         if (in_array(23000, $response->getCodes())) {
             $this->lastOperationSuccess = false;
             $offset++;
             continue;
         }
         /*
          * Save the packet so it can be accessed by methods that can be called by the callback like endTask()
          * through Action class
          */
         if ($result[0] instanceof Task) {
             $this->currentTask = $result[0];
         }
         // 7. Update the current task to change its status
         $this->updateTaskStatusByIdAndStatusId($id, Task::STATUS_ID_PROCESSING);
         // 8. Retrieve record
         $this->runUseCaseWithNoOfRetriesOnFail('task|retrieve', new Request(['id' => $id]), $this->getMaxRetries());
         $response = $this->getUseCaseResponse();
         $result = $response->getResult();
         if ($response->getTotalResultCount() > 0 && isset($result[0]) && $result[0] instanceof Task) {
             $actPacket = new ActPacket();
             /** @var \Tasker\Entity\Task $task */
             $task = $result[0];
             $actPacket->setExternalId($task->getExternalId());
             $actPacket->setExternalTypeId($task->getExternalTypeId());
             $actPacket->setExternalData($task->getExternalData());
             $actPacket->setRepeatingInterval($task->getRepeatingInterval());
             $actPacket->setPriority($task->getPriority());
             $actPacket->setTypeId($task->getTypeId());
             $actPacket->setStartingDateTime($task->getStartingDateTime());
             // Save the old values before calling callback
             $this->actPacket = $actPacket;
             $this->callback->callback($this->info, $actPacket, new Action($this));
         }
         // 9. Update the current task to change its status
         if ($this->currentTask->getStatusId() != Task::STATUS_ID_ENDED) {
             $this->updateTaskStatusByIdAndStatusId($id, Task::STATUS_ID_SLEEPING);
         }
         // 10. Delete the lock record
         $this->deleteLock($lockId);
         return;
     }
 }