thirdparty/google_appengine/google/appengine/api/labs/taskqueue/taskqueue_stub.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 23 Oct 2009 13:54:11 -0500
changeset 3031 7678f72140e6
parent 2864 2e0b0af889be
permissions -rwxr-xr-x
Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Copyright 2007 Google Inc.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#     http://www.apache.org/licenses/LICENSE-2.0
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
#
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""Stub version of the Task Queue API.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
This stub only stores tasks; it doesn't actually run them. It also validates
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
the tasks by checking their queue name against the queue.yaml.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
As well as implementing Task Queue API functions, the stub exposes various other
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
functions that are used by the dev_appserver's admin console to display the
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
application's queues and tasks.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
"""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
import base64
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
import datetime
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
import os
3031
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
    33
import random
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
    34
import time
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
import taskqueue_service_pb
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
from google.appengine.api import apiproxy_stub
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    39
from google.appengine.api import queueinfo
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    40
from google.appengine.api import urlfetch
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
from google.appengine.runtime import apiproxy_errors
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    42
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    43
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    44
DEFAULT_RATE = '5.00/s'
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    45
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
DEFAULT_BUCKET_SIZE = 5
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    48
MAX_ETA_DELTA_DAYS = 30
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    49
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    50
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
def _ParseQueueYaml(unused_self, root_path):
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    52
  """Loads the queue.yaml file and parses it.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    53
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    54
  Args:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    55
    unused_self: Allows this function to be bound to a class member. Not used.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    56
    root_path: Directory containing queue.yaml. Not used.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    57
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    58
  Returns:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    59
    None if queue.yaml doesn't exist, otherwise a queueinfo.QueueEntry object
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    60
    populaeted from the queue.yaml.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    61
  """
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
  if root_path is None:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
    return None
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
  for queueyaml in ('queue.yaml', 'queue.yml'):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    65
    try:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    66
      fh = open(os.path.join(root_path, queueyaml), 'r')
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
    except IOError:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    68
      continue
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    69
    try:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    70
      queue_info = queueinfo.LoadSingleQueue(fh)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    71
      return queue_info
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
    finally:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    73
      fh.close()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    74
  return None
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    75
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    76
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    77
def _CompareTasksByEta(a, b):
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    78
  """Python sort comparator for tasks by estimated time of arrival (ETA).
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    79
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    80
  Args:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    81
    a: A taskqueue_service_pb.TaskQueueAddRequest.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    82
    b: A taskqueue_service_pb.TaskQueueAddRequest.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    83
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    84
  Returns:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    85
    Standard 1/0/-1 comparison result.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
    86
  """
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    87
  if a.eta_usec() > b.eta_usec():
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    88
    return 1
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    89
  if a.eta_usec() < b.eta_usec():
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    90
    return -1
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    91
  return 0
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    92
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    93
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    94
def _FormatEta(eta_usec):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    95
  """Formats a task ETA as a date string in UTC."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    96
  eta = datetime.datetime.fromtimestamp(eta_usec/1000000)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    97
  return eta.strftime('%Y/%m/%d %H:%M:%S')
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    98
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    99
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   100
def _EtaDelta(eta_usec):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   101
  """Formats a task ETA as a relative time string."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   102
  eta = datetime.datetime.fromtimestamp(eta_usec/1000000)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   103
  now = datetime.datetime.utcnow()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   104
  if eta > now:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   105
    return str(eta - now) + ' from now'
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   106
  else:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   107
    return str(now - eta) + ' ago'
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   108
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   109
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   110
class TaskQueueServiceStub(apiproxy_stub.APIProxyStub):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   111
  """Python only task queue service stub.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   112
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   113
  This stub does not attempt to automatically execute tasks.  Instead, it
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   114
  stores them for display on a console.  The user may manually execute the
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   115
  tasks from the console.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   116
  """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   117
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   118
  queue_yaml_parser = _ParseQueueYaml
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   119
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   120
  def __init__(self, service_name='taskqueue', root_path=None):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   121
    """Constructor.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   122
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   123
    Args:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   124
      service_name: Service name expected for all calls.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   125
      root_path: Root path to the directory of the application which may contain
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   126
        a queue.yaml file. If None, then it's assumed no queue.yaml file is
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   127
        available.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   128
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   129
    super(TaskQueueServiceStub, self).__init__(service_name)
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   130
    self._taskqueues = {}
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   131
    self._next_task_id = 1
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   132
    self._root_path = root_path
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   133
3031
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   134
    self._app_queues = {}
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   135
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   136
  def _Dynamic_Add(self, request, response):
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   137
    """Local implementation of the Add RPC in TaskQueueService.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   138
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   139
    Must adhere to the '_Dynamic_' naming convention for stubbing to work.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   140
    See taskqueue_service.proto for a full description of the RPC.
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   141
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   142
    Args:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   143
      request: A taskqueue_service_pb.TaskQueueAddRequest.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   144
      response: A taskqueue_service_pb.TaskQueueAddResponse.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   145
    """
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   146
    if request.eta_usec() < 0:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   147
      raise apiproxy_errors.ApplicationError(
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   148
          taskqueue_service_pb.TaskQueueServiceError.INVALID_ETA)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   149
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   150
    eta = datetime.datetime.utcfromtimestamp(request.eta_usec() / 1e6)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   151
    max_eta = (datetime.datetime.utcnow() +
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   152
               datetime.timedelta(days=MAX_ETA_DELTA_DAYS))
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   153
    if eta > max_eta:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   154
      raise apiproxy_errors.ApplicationError(
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   155
          taskqueue_service_pb.TaskQueueServiceError.INVALID_ETA)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   156
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   157
    if not self._IsValidQueue(request.queue_name()):
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   158
      raise apiproxy_errors.ApplicationError(
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   159
          taskqueue_service_pb.TaskQueueServiceError.UNKNOWN_QUEUE)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   160
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   161
    if not request.task_name():
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   162
      request.set_task_name('task%d' % self._next_task_id)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   163
      response.set_chosen_task_name(request.task_name())
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   164
      self._next_task_id += 1
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   165
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   166
    tasks = self._taskqueues.setdefault(request.queue_name(), [])
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   167
    for task in tasks:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   168
      if task.task_name() == request.task_name():
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   169
        raise apiproxy_errors.ApplicationError(
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   170
            taskqueue_service_pb.TaskQueueServiceError.TASK_ALREADY_EXISTS)
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   171
    tasks.append(request)
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   172
    tasks.sort(_CompareTasksByEta)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   173
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   174
  def _IsValidQueue(self, queue_name):
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   175
    """Determines whether a queue is valid, i.e. tasks can be added to it.
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   176
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   177
    Valid queues are the 'default' queue, plus any queues in the queue.yaml
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   178
    file.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   179
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   180
    Args:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   181
      queue_name: the name of the queue to validate.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   182
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   183
    Returns:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   184
      True iff queue is valid.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   185
    """
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   186
    if queue_name == 'default':
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   187
      return True
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   188
    queue_info = self.queue_yaml_parser(self._root_path)
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   189
    if queue_info and queue_info.queue:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   190
      for entry in queue_info.queue:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   191
        if entry.name == queue_name:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   192
          return True
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   193
    return False
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   194
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   195
  def GetQueues(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   196
    """Gets all the applications's queues.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   197
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   198
    Returns:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   199
      A list of dictionaries, where each dictionary contains one queue's
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   200
      attributes. E.g.:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   201
        [{'name': 'some-queue',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   202
          'max_rate': '1/s',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   203
          'bucket_size': 5,
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   204
          'oldest_task': '2009/02/02 05:37:42',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   205
          'eta_delta': '0:00:06.342511 ago',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   206
          'tasks_in_queue': 12}, ...]
3031
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   207
      The list of queues always includes the default queue.
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   208
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   209
    queues = []
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   210
    queue_info = self.queue_yaml_parser(self._root_path)
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   211
    has_default = False
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   212
    if queue_info and queue_info.queue:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   213
      for entry in queue_info.queue:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   214
        if entry.name == 'default':
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   215
          has_default = True
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   216
        queue = {}
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   217
        queues.append(queue)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   218
        queue['name'] = entry.name
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   219
        queue['max_rate'] = entry.rate
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   220
        if entry.bucket_size:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   221
          queue['bucket_size'] = entry.bucket_size
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   222
        else:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   223
          queue['bucket_size'] = DEFAULT_BUCKET_SIZE
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   224
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   225
        tasks = self._taskqueues.setdefault(entry.name, [])
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   226
        if tasks:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   227
          queue['oldest_task'] = _FormatEta(tasks[0].eta_usec())
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   228
          queue['eta_delta'] = _EtaDelta(tasks[0].eta_usec())
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   229
        else:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   230
          queue['oldest_task'] = ''
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   231
        queue['tasks_in_queue'] = len(tasks)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   232
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   233
    if not has_default:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   234
      queue = {}
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   235
      queues.append(queue)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   236
      queue['name'] = 'default'
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   237
      queue['max_rate'] = DEFAULT_RATE
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   238
      queue['bucket_size'] = DEFAULT_BUCKET_SIZE
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   239
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   240
      tasks = self._taskqueues.get('default', [])
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   241
      if tasks:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   242
        queue['oldest_task'] = _FormatEta(tasks[0].eta_usec())
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   243
        queue['eta_delta'] = _EtaDelta(tasks[0].eta_usec())
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   244
      else:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   245
        queue['oldest_task'] = ''
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   246
      queue['tasks_in_queue'] = len(tasks)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   247
    return queues
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   248
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   249
  def GetTasks(self, queue_name):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   250
    """Gets a queue's tasks.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   251
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   252
    Args:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   253
      queue_name: Queue's name to return tasks for.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   254
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   255
    Returns:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   256
      A list of dictionaries, where each dictionary contains one task's
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   257
      attributes. E.g.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   258
        [{'name': 'task-123',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   259
          'url': '/update',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   260
          'method': 'GET',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   261
          'eta': '2009/02/02 05:37:42',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   262
          'eta_delta': '0:00:06.342511 ago',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   263
          'body': '',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   264
          'headers': {'X-AppEngine-QueueName': 'update-queue',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   265
                      'X-AppEngine-TaskName': 'task-123',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   266
                      'X-AppEngine-TaskRetryCount': '0',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   267
                      'X-AppEngine-Development-Payload': '1',
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   268
                      'Content-Length': 0,
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   269
                      'Content-Type': 'application/octet-streamn'}, ...]
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   270
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   271
    Raises:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   272
      ValueError: A task request contains an unknown HTTP method type.
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   273
    """
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   274
    tasks = self._taskqueues.get(queue_name, [])
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   275
    result_tasks = []
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   276
    for task_request in tasks:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   277
      task = {}
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   278
      result_tasks.append(task)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   279
      task['name'] = task_request.task_name()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   280
      task['url'] = task_request.url()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   281
      method = task_request.method()
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   282
      if method == taskqueue_service_pb.TaskQueueAddRequest.GET:
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   283
        task['method'] = 'GET'
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   284
      elif method == taskqueue_service_pb.TaskQueueAddRequest.POST:
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   285
        task['method'] = 'POST'
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   286
      elif method == taskqueue_service_pb.TaskQueueAddRequest.HEAD:
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   287
        task['method'] = 'HEAD'
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   288
      elif method == taskqueue_service_pb.TaskQueueAddRequest.PUT:
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   289
        task['method'] = 'PUT'
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   290
      elif method == taskqueue_service_pb.TaskQueueAddRequest.DELETE:
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   291
        task['method'] = 'DELETE'
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   292
      else:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   293
        raise ValueError('Unexpected method: %d' % method)
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   294
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   295
      task['eta'] = _FormatEta(task_request.eta_usec())
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   296
      task['eta_delta'] = _EtaDelta(task_request.eta_usec())
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   297
      task['body'] = base64.b64encode(task_request.body())
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   298
      headers = urlfetch._CaselessDict()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   299
      task['headers'] = headers
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   300
      for req_header in task_request.header_list():
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   301
        headers[req_header.key()] = req_header.value()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   302
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   303
      headers['X-AppEngine-QueueName'] = queue_name
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   304
      headers['X-AppEngine-TaskName'] = task['name']
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   305
      headers['X-AppEngine-TaskRetryCount'] = '0'
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   306
      headers['X-AppEngine-Development-Payload'] = '1'
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   307
      headers['Content-Length'] = len(task['body'])
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   308
      headers['Content-Type'] = headers.get(
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   309
          'Content-Type', 'application/octet-stream')
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   310
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   311
    return result_tasks
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   312
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   313
  def DeleteTask(self, queue_name, task_name):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   314
    """Deletes a task from a queue.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   315
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   316
    Args:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   317
      queue_name: the name of the queue to delete the task from.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   318
      task_name: the name of the task to delete.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   319
    """
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   320
    tasks = self._taskqueues.get(queue_name, [])
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   321
    for task in tasks:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   322
      if task.task_name() == task_name:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   323
        tasks.remove(task)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   324
        return
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   325
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   326
  def FlushQueue(self, queue_name):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   327
    """Removes all tasks from a queue.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   328
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   329
    Args:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   330
      queue_name: the name of the queue to remove tasks from.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   331
    """
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   332
    self._taskqueues[queue_name] = []
3031
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   333
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   334
  def _Dynamic_UpdateQueue(self, request, unused_response):
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   335
    """Local implementation of the UpdateQueue RPC in TaskQueueService.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   336
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   337
    Must adhere to the '_Dynamic_' naming convention for stubbing to work.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   338
    See taskqueue_service.proto for a full description of the RPC.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   339
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   340
    Args:
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   341
      request: A taskqueue_service_pb.TaskQueueUpdateQueueRequest.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   342
      unused_response: A taskqueue_service_pb.TaskQueueUpdateQueueResponse.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   343
                       Not used.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   344
    """
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   345
    queues = self._app_queues.setdefault(request.app_id(), {})
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   346
    defensive_copy = taskqueue_service_pb.TaskQueueUpdateQueueRequest()
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   347
    defensive_copy.CopyFrom(request)
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   348
    queues[request.queue_name()] = defensive_copy
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   349
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   350
  def _Dynamic_FetchQueues(self, request, response):
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   351
    """Local implementation of the FetchQueues RPC in TaskQueueService.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   352
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   353
    Must adhere to the '_Dynamic_' naming convention for stubbing to work.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   354
    See taskqueue_service.proto for a full description of the RPC.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   355
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   356
    Args:
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   357
      request: A taskqueue_service_pb.TaskQueueFetchQueuesRequest.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   358
      response: A taskqueue_service_pb.TaskQueueFetchQueuesResponse.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   359
    """
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   360
    queues = self._app_queues.get(request.app_id(), {})
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   361
    for unused_key, queue in sorted(queues.items()[:request.max_rows()]):
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   362
      response_queue = response.add_queue()
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   363
      response_queue.set_queue_name(queue.queue_name())
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   364
      response_queue.set_bucket_refill_per_second(
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   365
          queue.bucket_refill_per_second())
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   366
      response_queue.set_bucket_capacity(queue.bucket_capacity())
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   367
      response_queue.set_user_specified_rate(queue.user_specified_rate())
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   368
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   369
  def _Dynamic_FetchQueueStats(self, request, response):
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   370
    """Local 'random' implementation of the TaskQueueService.FetchQueueStats.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   371
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   372
    This implementation just populates the stats with random numbers.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   373
    Must adhere to the '_Dynamic_' naming convention for stubbing to work.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   374
    See taskqueue_service.proto for a full description of the RPC.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   375
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   376
    Args:
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   377
      request: A taskqueue_service_pb.TaskQueueFetchQueueStatsRequest.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   378
      response: A taskqueue_service_pb.TaskQueueFetchQueueStatsResponse.
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   379
    """
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   380
    for _ in request.queue_name_list():
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   381
      stats = response.add_queuestats()
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   382
      stats.set_num_tasks(random.randint(0, request.max_num_tasks()))
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   383
      if stats.num_tasks() == 0:
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   384
        stats.set_oldest_eta_usec(-1)
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   385
      else:
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   386
        now = datetime.datetime.utcnow()
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   387
        now_sec = time.mktime(now.timetuple())
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   388
        stats.set_oldest_eta_usec(now_sec * 1e6 + random.randint(-1e6, 1e6))
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   389
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   390
      if random.randint(0, 9) > 0:
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   391
        scanner_info = stats.mutable_scanner_info()
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   392
        scanner_info.set_executed_last_minute(random.randint(0, 10))
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   393
        scanner_info.set_executed_last_hour(scanner_info.executed_last_minute()
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   394
                                            + random.randint(0, 100))
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   395
        scanner_info.set_sampling_duration_seconds(random.random() * 10000.0)
7678f72140e6 Update Google App Engine from 1.2.5 to 1.2.7 in thirdparty folder.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2864
diff changeset
   396
    return