Example #1
0
 private static function buildBulkAddRequest()
 {
     $req = new TaskQueueBulkAddRequest();
     $task = $req->addAddRequest();
     $task->setQueueName('default');
     $task->setTaskName('');
     $task->setUrl('/someUrl');
     global $mockTime;
     $task->setEtaUsec($mockTime * 1000000.0);
     $task->setMethod(RequestMethod::POST);
     return $req;
 }
 private static function buildBulkAddRequest($queue_name = 'default')
 {
     $req = new TaskQueueBulkAddRequest();
     $task = $req->addAddRequest();
     $task->setQueueName($queue_name);
     $task->setTaskName('');
     $task->setUrl('/someUrl');
     $time = 12345.6;
     MockMicrotime::expect($time);
     $task->setEtaUsec($time * 1000000.0);
     $task->setMethod(RequestMethod::POST);
     return $req;
 }
 /**
  * Add many tasks
  *
  * @param Task[] $arr_tasks
  *
  * @return string[]|array Added task names
  */
 public function addTasks(array $arr_tasks)
 {
     // Set up request and response objects
     $obj_request = new TaskQueueBulkAddRequest();
     $obj_response = new TaskQueueBulkAddResponse();
     // Add the tasks
     $int_now = time();
     $int_now_usec = $int_now * 1000000.0;
     foreach ($arr_tasks as $obj_task) {
         $obj_add_request = $obj_request->addAddRequest();
         $obj_add_request->setQueueName($this->str_name)->setMode(\google\appengine\TaskQueueMode\Mode::PULL)->setEtaUsec(null === $obj_task->getEta() ? $int_now_usec : $obj_task->getEta() * 1000000.0)->setTaskName($obj_task->getName())->setBody($obj_task->getPayload());
     }
     // Make the request
     $this->makeCall('BulkAdd', $obj_request, $obj_response);
     // Extract task names
     $arr_task_names = [];
     $arr_results = $obj_response->getTaskResultList();
     foreach ($arr_results as $index => $task_result) {
         if ($task_result->hasChosenTaskName()) {
             $arr_task_names[$index] = $task_result->getChosenTaskName();
         }
         if ($task_result->getResult() != ErrorCode::OK) {
             throw new \RuntimeException($this->translateResultCode($task_result->getResult()));
         }
     }
     return $arr_task_names;
 }
 /**
  * Add tasks to the queue.
  *
  * @param PushTask[] $tasks The tasks to be added to the queue.
  *
  * @return An array containing the name of each task added, with the same
  * ordering as $tasks.
  *
  * @throws TaskAlreadyExistsException if a task of the same name already
  * exists in the queue.
  * If this exception is raised, the caller can be guaranteed that all tasks
  * were successfully added either by this call or a previous call. Another way
  * to express it is that, if any task failed to be added for a different
  * reason, a different exception will be thrown.
  * @throws TaskQueueException if there was a problem using the service.
  */
 public function addTasks($tasks)
 {
     if (!is_array($tasks)) {
         throw new \InvalidArgumentException('$tasks must be an array. Actual type: ' . gettype($tasks));
     }
     if (empty($tasks)) {
         return [];
     }
     if (count($tasks) > self::MAX_TASKS_PER_ADD) {
         throw new \InvalidArgumentException('$tasks must contain at most ' . self::MAX_TASKS_PER_ADD . ' tasks. Actual size: ' . count($tasks));
     }
     $req = new TaskQueueBulkAddRequest();
     $resp = new TaskQueueBulkAddResponse();
     $names = [];
     $current_time = microtime(true);
     foreach ($tasks as $task) {
         if (!$task instanceof PushTask) {
             throw new \InvalidArgumentException('All values in $tasks must be instances of PushTask. ' . 'Actual type: ' . gettype($task));
         }
         $names[] = $task->getName();
         $add = $req->addAddRequest();
         $add->setQueueName($this->name);
         $add->setTaskName($task->getName());
         $add->setEtaUsec(($current_time + $task->getDelaySeconds()) * 1000000.0);
         $add->setMethod(self::$methods[$task->getMethod()]);
         $add->setUrl($task->getUrl());
         foreach ($task->getHeaders() as $header) {
             $pair = explode(':', $header, 2);
             $header_pb = $add->addHeader();
             $header_pb->setKey(trim($pair[0]));
             $header_pb->setValue(trim($pair[1]));
         }
         // TODO: Replace getQueryData() with getBody() and simplify the following
         // block.
         if ($task->getMethod() == 'POST' || $task->getMethod() == 'PUT') {
             if ($task->getQueryData()) {
                 $add->setBody(http_build_query($task->getQueryData()));
             }
         }
         if ($add->byteSizePartial() > PushTask::MAX_TASK_SIZE_BYTES) {
             throw new TaskQueueException('Task greater than maximum size of ' . PushTask::MAX_TASK_SIZE_BYTES . '. size: ' . $add->byteSizePartial());
         }
     }
     try {
         ApiProxy::makeSyncCall('taskqueue', 'BulkAdd', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::errorCodeToException($e->getApplicationError());
     }
     // Update $names with any generated task names. Also, check if there are any
     // error responses.
     $results = $resp->getTaskResultList();
     $exception = null;
     foreach ($results as $index => $task_result) {
         if ($task_result->hasChosenTaskName()) {
             $names[$index] = $task_result->getChosenTaskName();
         }
         if ($task_result->getResult() != ErrorCode::OK) {
             $exception = self::errorCodeToException($task_result->getResult());
             // Other exceptions take precedence over TaskAlreadyExistsException.
             if (!$exception instanceof TaskAlreadyExistsException) {
                 throw $exception;
             }
         }
     }
     if (isset($exception)) {
         throw $exception;
     }
     return $names;
 }
Example #5
0
 private static function addTasks($tasks, $queue)
 {
     $req = new TaskQueueBulkAddRequest();
     $resp = new TaskQueueBulkAddResponse();
     $names = [];
     $current_time = microtime(true);
     foreach ($tasks as $task) {
         $names[] = $task->getName();
         $add = $req->addAddRequest();
         $add->setQueueName($queue);
         $add->setTaskName($task->getName());
         $add->setEtaUsec(($current_time + $task->getDelaySeconds()) * 1000000.0);
         $add->setMethod(self::$methods[$task->getMethod()]);
         if ($task->getMethod() == 'POST' || $task->getMethod() == 'PUT') {
             $add->setUrl($task->getUrlPath());
             if ($task->getQueryData()) {
                 $add->setBody(http_build_query($task->getQueryData()));
                 $header = $add->addHeader();
                 $header->setKey('content-type');
                 $header->setValue('application/x-www-form-urlencoded');
             }
         } else {
             $url_path = $task->getUrlPath();
             if ($task->getQueryData()) {
                 $url_path = $url_path . '?' . http_build_query($task->getQueryData());
             }
             $add->setUrl($url_path);
         }
         if (strlen($add->getUrl()) > self::MAX_URL_LENGTH) {
             throw new TaskQueueException('URL length greater than maximum of ' . self::MAX_URL_LENGTH . '. URL: ' . $add->getUrl());
         }
         if ($add->byteSizePartial() > self::MAX_TASK_SIZE_BYTES) {
             throw new TaskQueueException('Task greater than maximum size of ' . self::MAX_TASK_SIZE_BYTES . '. size: ' . $add->byteSizePartial());
         }
     }
     try {
         ApiProxy::makeSyncCall('taskqueue', 'BulkAdd', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::ApplicationErrorToException($e);
     }
     // Update $names with any generated task names.
     $results = $resp->getTaskResultList();
     foreach ($results as $index => $taskResult) {
         if ($taskResult->hasChosenTaskName()) {
             $names[$index] = $taskResult->getChosenTaskName();
         }
     }
     return $names;
 }