19 |
19 |
20 A library for working with QueueInfo records, describing task queue entries |
20 A library for working with QueueInfo records, describing task queue entries |
21 for an application. Supports loading the records from queue.yaml. |
21 for an application. Supports loading the records from queue.yaml. |
22 |
22 |
23 A queue has two required parameters and one optional one. The required |
23 A queue has two required parameters and one optional one. The required |
24 parameters are 'name' (must be unique for an appid) and 'rate' (the |
24 parameters are 'name' (must be unique for an appid) and 'rate' (the rate |
25 rate at which jobs in the queue are run). There is an optional 'bucket_size' |
25 at which jobs in the queue are run). There is an optional parameter |
26 that will allow tokens to be 'saved up' and bucket_size. Rate and bucket_size rate are |
26 'bucket_size' that will allow tokens to be 'saved up' (for more on the |
27 expressed as number/unit, with number being an int or a float, and unit being |
27 algorithm, see http://en.wikipedia.org/wiki/Token_Bucket). rate is expressed |
28 one of 's' (seconds), 'm' (minutes), 'h' (hours) or 'd' (days). |
28 as number/unit, with number being an int or a float, and unit being one of |
|
29 's' (seconds), 'm' (minutes), 'h' (hours) or 'd' (days). bucket_size is |
|
30 an integer. |
29 |
31 |
30 An example of the use of bucket_size rate: the free email quota is 2000/d, and the |
32 An example of the use of bucket_size rate: the free email quota is 2000/d, |
31 maximum you can send in a single minute is 11. So we can define a queue for |
33 and the maximum you can send in a single minute is 11. So we can define a |
32 sending email like this: |
34 queue for sending email like this: |
33 |
35 |
34 queue: |
36 queue: |
35 - name: mail_queue |
37 - name: mail_queue |
36 rate: 2000/d |
38 rate: 2000/d |
37 bucket_size: 10/m |
39 bucket_size: 10 |
38 |
40 |
39 If this queue had been idle for a while before some jobs were submitted to it, |
41 If this queue had been idle for a while before some jobs were submitted to it, |
40 the first 10 jobs submitted would be run immediately, then subsequent ones |
42 the first 10 jobs submitted would be run immediately, then subsequent ones |
41 would be run once every 40s or so. The limit of 2000 per day would still apply. |
43 would be run once every 40s or so. The limit of 2000 per day would still apply. |
42 """ |
44 """ |
47 from google.appengine.api import yaml_builder |
49 from google.appengine.api import yaml_builder |
48 from google.appengine.api import yaml_listener |
50 from google.appengine.api import yaml_listener |
49 from google.appengine.api import yaml_object |
51 from google.appengine.api import yaml_object |
50 |
52 |
51 _NAME_REGEX = r'^[A-Za-z0-9-]{0,499}$' |
53 _NAME_REGEX = r'^[A-Za-z0-9-]{0,499}$' |
52 _RATE_REGEX = r'^[0-9]+(\.[0-9]+)?/[smhd]' |
54 _RATE_REGEX = r'^(0|[0-9]+(\.[0-9]*)?/[smhd])' |
53 |
55 |
54 QUEUE = 'queue' |
56 QUEUE = 'queue' |
55 |
57 |
56 NAME = 'name' |
58 NAME = 'name' |
57 RATE = 'rate' |
59 RATE = 'rate' |
113 a floating point number representing the rate/second. |
115 a floating point number representing the rate/second. |
114 |
116 |
115 Raises: |
117 Raises: |
116 MalformedQueueConfiguration: if the rate is invalid |
118 MalformedQueueConfiguration: if the rate is invalid |
117 """ |
119 """ |
|
120 if rate == "0": |
|
121 return 0.0 |
118 elements = rate.split('/') |
122 elements = rate.split('/') |
119 if len(elements) != 2: |
123 if len(elements) != 2: |
120 raise MalformedQueueConfiguration('Rate "%s" is invalid.' % rate) |
124 raise MalformedQueueConfiguration('Rate "%s" is invalid.' % rate) |
121 number, unit = elements |
125 number, unit = elements |
122 try: |
126 try: |