Skip to content

Commit

Permalink
App Engine Task Queue Sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmendoza committed Apr 11, 2016
1 parent 92d24a7 commit fa581b2
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions appengine/taskqueue/counter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# App Engine Task Queue Counter

<!-- auto-doc-link -->
These samples are used on the following documentation page:

> https://cloud.google.com/appengine/docs/python/taskqueue/overview-push
<!-- end-auto-doc-link -->
13 changes: 13 additions & 0 deletions appengine/taskqueue/counter/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
application: taskqueue-counter
runtime: python27
api_version: 1
version: 1
threadsafe: true

handlers:
- url: /.*
script: main.app

libraries:
- name: jinja2
version: latest
15 changes: 15 additions & 0 deletions appengine/taskqueue/counter/counter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<body>
<form action="/" method="POST">
<label for="key">Key:</label><input type="text" name="key" id="key">
<input type="submit" value="+1">
</form>
<ul>
{% for counter in counters %}
<li>
{{counter.key.id()|e}}: {{counter.count|e}}
</li>
{% endfor %}
</body>
</html>
60 changes: 60 additions & 0 deletions appengine/taskqueue/counter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#[START all]
import os

import jinja2
import webapp2

from google.appengine.api import taskqueue
from google.appengine.ext import ndb


JINJA_ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))


class Counter(ndb.Model):
count = ndb.IntegerProperty(indexed=False)

class CounterHandler(webapp2.RequestHandler):
def get(self):
template_values = {'counters': Counter.query()}
counter_template = JINJA_ENV.get_template('counter.html')
self.response.out.write(counter_template.render(template_values))

def post(self):
key = self.request.get('key')
if key != '':
# Add the task to the default queue.
taskqueue.add(url='/worker', params={'key': key})
self.redirect('/')

class CounterWorker(webapp2.RequestHandler):
def post(self): # should run at most 1/s due to entity group limit
key = self.request.get('key')
@ndb.transactional
def update_counter():
counter = Counter.get_or_insert(key, count=0)
counter.count += 1
counter.put()
update_counter()


app = webapp2.WSGIApplication([
('/', CounterHandler),
('/worker', CounterWorker)
], debug=True)
#[END all]
35 changes: 35 additions & 0 deletions appengine/taskqueue/counter/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import main
import webtest
from google.appengine.ext import ndb


def test_app(testbed, run_tasks):
key_name = 'foo'
key = ndb.Key('Counter', key_name)
counter = key.get()
if counter:
count = counter.count
else:
count = 0

app = webtest.TestApp(main.app)
response = app.post('/', {'key': key_name})
run_tasks(app)

counter = key.get()
count2 = counter.count
assert count + 1 == count2
3 changes: 3 additions & 0 deletions appengine/taskqueue/counter/queue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
queue:
- name: default
rate: 1/s

0 comments on commit fa581b2

Please sign in to comment.