Skip to content

Commit 304c5e6

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #261 from GoogleCloudPlatform/new-task-queue
Refactoring taskqueue sample
2 parents 7aa1689 + b2f43f0 commit 304c5e6

File tree

9 files changed

+149
-93
lines changed

9 files changed

+149
-93
lines changed

appengine/taskqueue/counter/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# App Engine Task Queue Counter
22

3+
To run this app locally, specify both `.yaml` files to `dev_appserver.py`:
4+
5+
dev_appserver.py -A your-app-id application.yaml worker.yaml
6+
7+
To deploy this application, specify both `.yaml` files to `appcfg.py`:
8+
9+
appcfg.py update -A your-app-id -V 1 application.yaml worker.yaml
10+
311
<!-- auto-doc-link -->
412
These samples are used on the following documentation page:
513

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.api import taskqueue
16+
from google.appengine.ext import ndb
17+
import webapp2
18+
19+
20+
COUNTER_KEY = 'default counter'
21+
22+
23+
class Counter(ndb.Model):
24+
count = ndb.IntegerProperty(indexed=False)
25+
26+
27+
class MainPageHandler(webapp2.RequestHandler):
28+
def get(self):
29+
counter = Counter.get_by_id(COUNTER_KEY)
30+
count = counter.count if counter else 0
31+
32+
self.response.write("""
33+
Count: {count}<br>
34+
<form method="post" action="/enqueue">
35+
<label>Increment amount</label>
36+
<input name="amount" value="1">
37+
<button>Enqueue task</button>
38+
</form>
39+
""".format(count=count))
40+
41+
42+
class EnqueueTaskHandler(webapp2.RequestHandler):
43+
def post(self):
44+
amount = int(self.request.get('amount'))
45+
46+
task = taskqueue.add(
47+
url='/update_counter',
48+
target='worker',
49+
params={'amount': amount})
50+
51+
self.response.write(
52+
'Task {} enqueued, ETA {}.'.format(task.name, task.eta))
53+
54+
55+
class AsyncEnqueueTaskHandler(webapp2.RequestHandler):
56+
def post(self):
57+
amount = int(self.request.get('amount'))
58+
59+
future = taskqueue.add_async(
60+
url='/update_counter',
61+
target='worker',
62+
params={'amount': amount})
63+
64+
task = future.wait()
65+
66+
self.response.write(
67+
'Task {} enqueued, ETA {}.'.format(task.name, task.eta))
68+
69+
70+
app = webapp2.WSGIApplication([
71+
('/', MainPageHandler),
72+
('/enqueue', EnqueueTaskHandler),
73+
('/enqueue_async', EnqueueTaskHandler)
74+
], debug=True)
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
runtime: python27
22
api_version: 1
33
threadsafe: true
4+
module: default
45

56
handlers:
67
- url: /.*
7-
script: main.app
8-
9-
libraries:
10-
- name: jinja2
11-
version: 2.6
8+
script: application.app

appengine/taskqueue/counter/counter_test.py renamed to appengine/taskqueue/counter/application_test.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from google.appengine.ext import ndb
16-
import main
15+
import application
1716
import webtest
17+
import worker
1818

1919

20-
def test_app(testbed, run_tasks):
21-
key_name = 'foo'
20+
def test_all(testbed, run_tasks):
21+
test_app = webtest.TestApp(application.app)
22+
test_worker = webtest.TestApp(worker.app)
2223

23-
app = webtest.TestApp(main.app)
24-
app.post('/', {'key': key_name})
25-
run_tasks(app)
24+
response = test_app.get('/')
25+
assert '0' in response.body
2626

27-
key = ndb.Key('Counter', key_name)
28-
counter = key.get()
29-
assert counter.count == 1
27+
test_app.post('/enqueue', {'amount': 5})
28+
run_tasks(test_worker)
29+
30+
response = test_app.get('/')
31+
assert '5' in response.body

appengine/taskqueue/counter/counter.html

Lines changed: 0 additions & 15 deletions
This file was deleted.

appengine/taskqueue/counter/main.py

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
queue:
2-
# Change the refresh rate of the default queue from 5/s to 1/s
2+
# Change the refresh rate of the default queue from 5/s to 1/s.
33
- name: default
44
rate: 1/s

appengine/taskqueue/counter/worker.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.ext import ndb
16+
import webapp2
17+
18+
19+
COUNTER_KEY = 'default counter'
20+
21+
22+
class Counter(ndb.Model):
23+
count = ndb.IntegerProperty(indexed=False)
24+
25+
26+
class UpdateCounterHandler(webapp2.RequestHandler):
27+
def post(self):
28+
amount = int(self.request.get('amount'))
29+
30+
# This task should run at most once per second because of the datastore
31+
# transaction write throughput.
32+
@ndb.transactional
33+
def update_counter():
34+
counter = Counter.get_or_insert(COUNTER_KEY, count=0)
35+
counter.count += amount
36+
counter.put()
37+
38+
update_counter()
39+
40+
41+
app = webapp2.WSGIApplication([
42+
('/update_counter', UpdateCounterHandler)
43+
], debug=True)
44+
# [END all]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: true
4+
module: worker
5+
6+
handlers:
7+
- url: /.*
8+
script: worker.app

0 commit comments

Comments
 (0)