Skip to content

Commit 891e302

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #248 from GoogleCloudPlatform/jlm/taskqueue-push
App Engine Task Queue Sample.
2 parents d51858b + 494597b commit 891e302

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

appengine/taskqueue/counter/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# App Engine Task Queue Counter
2+
3+
<!-- auto-doc-link -->
4+
These samples are used on the following documentation page:
5+
6+
> https://cloud.google.com/appengine/docs/python/taskqueue/overview-push
7+
8+
<!-- end-auto-doc-link -->

appengine/taskqueue/counter/app.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: true
4+
5+
handlers:
6+
- url: /.*
7+
script: main.app
8+
9+
libraries:
10+
- name: jinja2
11+
version: 2.6
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<form action="/" method="POST">
5+
<label for="key">Key:</label><input type="text" name="key" id="key">
6+
<input type="submit" value="+1">
7+
</form>
8+
<ul>
9+
{% for counter in counters %}
10+
<li>
11+
{{counter.key.id()|e}}: {{counter.count|e}}
12+
</li>
13+
{% endfor %}
14+
</body>
15+
</html>

appengine/taskqueue/counter/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
# [START all]
16+
import os
17+
18+
from google.appengine.api import taskqueue
19+
from google.appengine.ext import ndb
20+
import jinja2
21+
import webapp2
22+
23+
24+
JINJA_ENV = jinja2.Environment(
25+
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
26+
27+
28+
class Counter(ndb.Model):
29+
count = ndb.IntegerProperty(indexed=False)
30+
31+
32+
class CounterHandler(webapp2.RequestHandler):
33+
def get(self):
34+
template_values = {'counters': Counter.query()}
35+
counter_template = JINJA_ENV.get_template('counter.html')
36+
self.response.out.write(counter_template.render(template_values))
37+
38+
def post(self):
39+
key = self.request.get('key')
40+
if key != '':
41+
# Add the task to the default queue.
42+
taskqueue.add(url='/worker', params={'key': key})
43+
self.redirect('/')
44+
45+
46+
class CounterWorker(webapp2.RequestHandler):
47+
def post(self): # should run at most 1/s due to entity group limit
48+
key = self.request.get('key')
49+
50+
@ndb.transactional
51+
def update_counter():
52+
counter = Counter.get_or_insert(key, count=0)
53+
counter.count += 1
54+
counter.put()
55+
update_counter()
56+
57+
58+
app = webapp2.WSGIApplication([
59+
('/', CounterHandler),
60+
('/worker', CounterWorker)
61+
], debug=True)
62+
# [END all]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 main
17+
import webtest
18+
19+
20+
def test_app(testbed, run_tasks):
21+
key_name = 'foo'
22+
23+
app = webtest.TestApp(main.app)
24+
app.post('/', {'key': key_name})
25+
run_tasks(app)
26+
27+
key = ndb.Key('Counter', key_name)
28+
counter = key.get()
29+
assert counter.count == 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
queue:
2+
# Change the refresh rate of the default queue from 5/s to 1/s
3+
- name: default
4+
rate: 1/s

0 commit comments

Comments
 (0)