示例#1
0
  def get(cls, queue_names=frozenset()):
    """Returns a 2-tuple: (list of push _QueueInfo, list of pull _QueueInfo)."""
    fetch_queue_request = taskqueue_service_pb.TaskQueueFetchQueuesRequest()
    fetch_queue_request.set_max_rows(1000)
    fetch_queue_response = taskqueue_service_pb.TaskQueueFetchQueuesResponse()
    apiproxy_stub_map.MakeSyncCall('taskqueue',
                                   'FetchQueues',
                                   fetch_queue_request,
                                   fetch_queue_response)

    queue_stats_request = taskqueue_service_pb.TaskQueueFetchQueueStatsRequest()
    for queue in fetch_queue_response.queue_list():
      queue_stats_request.add_queue_name(queue.queue_name())
    queue_stats_response = (
        taskqueue_service_pb.TaskQueueFetchQueueStatsResponse())
    apiproxy_stub_map.MakeSyncCall('taskqueue',
                                   'FetchQueueStats',
                                   queue_stats_request,
                                   queue_stats_response)

    for queue, queue_stats in zip(
        fetch_queue_response.queue_list(),
        queue_stats_response.queuestats_list()):
      if queue_names and queue.queue_name() not in queue_names:
        continue
      yield cls._from_queue_and_stats(queue, queue_stats)
示例#2
0
    def test_get(self):
        fetch_queue_request = taskqueue_service_pb.TaskQueueFetchQueuesRequest(
        )
        fetch_queue_request.set_max_rows(1000)
        fetch_queue_response = taskqueue_service_pb.TaskQueueFetchQueuesResponse(
        )
        queue1 = fetch_queue_response.add_queue()
        queue1.set_queue_name('queue1')
        queue1.set_mode(taskqueue_service_pb.TaskQueueMode.PUSH)
        queue1.set_user_specified_rate('20/s')
        queue1.set_bucket_capacity(10)
        queue2 = fetch_queue_response.add_queue()
        queue2.set_queue_name('queue2')
        queue2.set_mode(taskqueue_service_pb.TaskQueueMode.PULL)
        queue2.set_user_specified_rate('20/s')
        queue2.set_bucket_capacity(10)

        apiproxy_stub_map.MakeSyncCall('taskqueue',
                                       'FetchQueues', fetch_queue_request,
                                       mox.IgnoreArg()).WithSideEffects(
                                           lambda _, _1, _2, response: response
                                           .CopyFrom(fetch_queue_response))

        queue_stats_request = taskqueue_service_pb.TaskQueueFetchQueueStatsRequest(
        )
        queue_stats_request.add_queue_name('queue1')
        queue_stats_request.add_queue_name('queue2')
        queue_stats_response = (
            taskqueue_service_pb.TaskQueueFetchQueueStatsResponse())
        queue_stats1 = queue_stats_response.add_queuestats()
        queue_stats1.set_num_tasks(20)
        queue_stats1.set_oldest_eta_usec(-1)
        queue_stats2 = queue_stats_response.add_queuestats()
        queue_stats2.set_num_tasks(50)
        queue_stats2.set_oldest_eta_usec(1234567890)

        apiproxy_stub_map.MakeSyncCall('taskqueue',
                                       'FetchQueueStats', queue_stats_request,
                                       mox.IgnoreArg()).WithSideEffects(
                                           lambda _, _1, _2, response: response
                                           .CopyFrom(queue_stats_response))

        self.mox.ReplayAll()
        queues = list(taskqueue_utils.QueueInfo.get())
        self.mox.VerifyAll()
        self.assertEqual('queue1', queues[0].name)
        self.assertEqual('queue2', queues[1].name)
示例#3
0
    def fetch_queue_stats(self, app_id, http_data):
        """ Gets statistics about tasks in queues.

    Args:
      app_id: The application ID.
      http_data: The payload containing the protocol buffer request.
    Returns:
      A tuple of a encoded response, error code, and error detail.
    """
        epoch = datetime.datetime.utcfromtimestamp(0)
        request = taskqueue_service_pb.TaskQueueFetchQueueStatsRequest(
            http_data)
        response = taskqueue_service_pb.TaskQueueFetchQueueStatsResponse()

        for queue_name in request.queue_name_list():
            try:
                queue = self.get_queue(app_id, queue_name)
            except QueueNotFound as error:
                return '', TaskQueueServiceError.UNKNOWN_QUEUE, str(error)

            stats_response = response.add_queuestats()

            if isinstance(queue, (PullQueue, PostgresPullQueue)):
                num_tasks = queue.total_tasks()
                oldest_eta = queue.oldest_eta()
            else:
                num_tasks = TaskName.all().\
                  filter("state =", tq_lib.TASK_STATES.QUEUED).\
                  filter("queue =", queue_name).\
                  filter("app_id =", app_id).count()

                # This is not supported for push queues yet.
                oldest_eta = None

            # -1 is used to indicate an absence of a value.
            oldest_eta_usec = (int((oldest_eta - epoch).total_seconds() *
                                   1000000) if oldest_eta else -1)

            stats_response.set_num_tasks(num_tasks)
            stats_response.set_oldest_eta_usec(oldest_eta_usec)

        return response.Encode(), 0, ""