def insert_task(self, description, tag=None, client=None): """ Insert task in task queue. If the task isn't found (backend 404), raises a :class:`gcloud.exceptions.NotFound`. :type description: string :param description: Description of task to perform :type tag: string :param tag: Optional. The tag for this task, allows leasing tasks with a specific tag :type client: :class:`gcloud.taskqueue.client.Client` or ``NoneType`` :param client: Optional. The client to use. If not passed, falls back to the ``client`` stored on the task's taskqueue. :rtype: :class:`_Task`. :returns: a task :raises: :class:`gcloud.exceptions.NotFound` """ client = self._require_client(client) new_task = { "queueName": self.full_name, "payloadBase64": base64.b64encode(description).decode('ascii'), "tag": tag } response = client.connection.api_request(method='POST', path=self.path + "/tasks/", data=new_task) task = Task(taskqueue=self, id=response.get('id')) task._set_properties(response) return task
def update_task(self, id, new_lease_time, client=None): """ Updates the duration of a task lease If the task isn't found (backend 404), raises a :class:`gcloud.exceptions.NotFound`. :type id: string :param id: A task name to update :type new_lease_time: int :param new_lease_time: New lease time, in seconds. :type client: :class:`gcloud.taskqueue.client.Client` or ``NoneType`` :param client: Optional. The client to use. If not passed, falls back to the ``client`` stored on the task's taskqueue. :rtype: :class:`_Task`. :returns: a task :raises: :class:`gcloud.exceptions.NotFound` """ client = self._require_client(client) task = Task(taskqueue=self, id=id) try: response = client.connection.api_request(method='POST', path=self.path + "/tasks/" + id, query_params={"newLeaseSeconds": new_lease_time}, _target_object=task) task._set_properties(response) return task except NotFound: return None
def get_items_from_response(self, response): """Yield :class:`.taskqueue.task.Task` items from response. :type response: dict :param response: The JSON API response for a page of tasks. """ for item in response.get('items', []): id = item.get('id') task = Task(id, taskqueue=self.taskqueue) task._set_properties(item) yield task
def lease(self, lease_time, num_tasks, group_by_tag=False, tag=None, client=None): """ Acquires a lease on the topmost N unowned tasks in the specified queue. :type lease_time: int :param lease_time: How long to lease this task, in seconds. :type num_tasks: int :param num_tasks: The number of tasks to lease. :type group_by_tag: bool :param group_by_tag: Optional. When True, returns tasks of the same tag. Specify which tag by using the tag parameter. If tag is not specified, returns tasks of the same tag as the oldest task in the queue. :type tag: string :param tag: Optional. Only specify tag if groupByTag is true. If groupByTag is true and tag is not specified, the tag is assumed to be that of the oldest task by ETA. I.e., the first available tag. :type client: :class:`gcloud.taskqueue.client.Client` or ``NoneType`` :param client: Optional. The client to use. If not passed, falls back to the ``client`` stored on the task's taskqueue. :rtype: :class:`_TaskIterator`. :returns: An iterator of tasks. """ client = self._require_client(client) if group_by_tag: query_params = {"leaseSecs": lease_time, "numTasks": num_tasks, "groupByTag": group_by_tag, "tag": tag} else: query_params = {"leaseSecs": lease_time, "numTasks": num_tasks} response = client.connection.api_request(method='POST', path=self.path + "/tasks/lease", query_params=query_params) for item in response.get('items', []): id = item.get('id') task = Task(id, taskqueue=self) task._set_properties(item) yield task
def get_task(self, id, client=None): """Gets a named task from taskqueue If the task isn't found (backend 404), raises a :class:`gcloud.exceptions.NotFound`. :type id: string :param id: A task name to get :type client: :class:`gcloud.taskqueue.client.Client` or ``NoneType`` :param client: Optional. The client to use. If not passed, falls back to the ``client`` stored on the current taskqueue. :rtype: :class:`_Task`. :returns: a task :raises: :class:`gcloud.exceptions.NotFound` """ client = self._require_client(client) task = Task(taskqueue=self, id=id) try: response = client.connection.api_request(method='GET', path=task.path, _target_object=task) task._set_properties(response) return task except NotFound: return None