-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
51 lines (40 loc) · 1.57 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from apiclient.discovery import build
import base64
import urllib
import urllib2
import oauth2utils
# Get settings from "app/" directory (just for consistency)
import yaml
with open('app/app.yaml') as f:
APP = yaml.safe_load(f)['application']
# See for "s~": http://stackoverflow.com/a/7021985/237285
PROJECT = "s~" + APP
with open('app/queue.yaml') as f:
TASK_QUEUE = yaml.safe_load(f)['queue'][0]['name']
# Build API helper objects
service = build("taskqueue", "v1beta1", http=oauth2utils.http,
discoveryServiceUrl="https://www.googleapis.com/discovery/v1/apis/taskqueue/v1beta1/rest")
queues = service.taskqueues()
tasks = service.tasks()
def get_count():
demo_queue = queues.get(taskqueue=TASK_QUEUE, project=PROJECT, getStats=True).execute()
return demo_queue['stats']['totalTasks']
def lease():
leasedTasks = tasks.lease(taskqueue=TASK_QUEUE, project=PROJECT, leaseSecs=10, numTasks=1, body="").execute()
return leasedTasks['items'][0]
def delete(id):
tasks.delete(taskqueue=TASK_QUEUE, project=PROJECT, task=id).execute()
def main():
print "querying size of queue: " + str(get_count())
print "putting message into queue"
urllib2.urlopen("http://" + APP + ".appspot.com/add", urllib.urlencode({'message': "demo data"}))
print "querying size of queue: " + str(get_count())
print "pulling message from queue:",
task = lease()
print base64.b64decode(task['payloadBase64'])
print "querying size of queue: " + str(get_count())
print "deleting message from queue"
delete(task['id'])
print "querying size of queue: " + str(get_count())
if __name__ == '__main__':
main()