From b2f43f03cc4e1ff2b9a63c5c52a13cdd56b44377 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Fri, 15 Apr 2016 10:00:41 -0700 Subject: [PATCH] Refactoring taskqueue sample Change-Id: I1c78d57979bde942c5a5a6b232ef87cca10944b2 --- appengine/taskqueue/counter/README.md | 8 ++ appengine/taskqueue/counter/application.py | 74 +++++++++++++++++++ .../counter/{app.yaml => application.yaml} | 7 +- .../{counter_test.py => application_test.py} | 22 +++--- appengine/taskqueue/counter/counter.html | 15 ---- appengine/taskqueue/counter/main.py | 62 ---------------- appengine/taskqueue/counter/queue.yaml | 2 +- appengine/taskqueue/counter/worker.py | 44 +++++++++++ appengine/taskqueue/counter/worker.yaml | 8 ++ 9 files changed, 149 insertions(+), 93 deletions(-) create mode 100644 appengine/taskqueue/counter/application.py rename appengine/taskqueue/counter/{app.yaml => application.yaml} (54%) rename appengine/taskqueue/counter/{counter_test.py => application_test.py} (62%) delete mode 100644 appengine/taskqueue/counter/counter.html delete mode 100644 appengine/taskqueue/counter/main.py create mode 100644 appengine/taskqueue/counter/worker.py create mode 100644 appengine/taskqueue/counter/worker.yaml diff --git a/appengine/taskqueue/counter/README.md b/appengine/taskqueue/counter/README.md index 4ccee7143a01..e97ad7df0ed3 100644 --- a/appengine/taskqueue/counter/README.md +++ b/appengine/taskqueue/counter/README.md @@ -1,5 +1,13 @@ # App Engine Task Queue Counter +To run this app locally, specify both `.yaml` files to `dev_appserver.py`: + + dev_appserver.py -A your-app-id application.yaml worker.yaml + +To deploy this application, specify both `.yaml` files to `appcfg.py`: + + appcfg.py update -A your-app-id -V 1 application.yaml worker.yaml + These samples are used on the following documentation page: diff --git a/appengine/taskqueue/counter/application.py b/appengine/taskqueue/counter/application.py new file mode 100644 index 000000000000..dbd27cd849b4 --- /dev/null +++ b/appengine/taskqueue/counter/application.py @@ -0,0 +1,74 @@ +# 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. + +from google.appengine.api import taskqueue +from google.appengine.ext import ndb +import webapp2 + + +COUNTER_KEY = 'default counter' + + +class Counter(ndb.Model): + count = ndb.IntegerProperty(indexed=False) + + +class MainPageHandler(webapp2.RequestHandler): + def get(self): + counter = Counter.get_by_id(COUNTER_KEY) + count = counter.count if counter else 0 + + self.response.write(""" + Count: {count}
+
+ + + +
+ """.format(count=count)) + + +class EnqueueTaskHandler(webapp2.RequestHandler): + def post(self): + amount = int(self.request.get('amount')) + + task = taskqueue.add( + url='/update_counter', + target='worker', + params={'amount': amount}) + + self.response.write( + 'Task {} enqueued, ETA {}.'.format(task.name, task.eta)) + + +class AsyncEnqueueTaskHandler(webapp2.RequestHandler): + def post(self): + amount = int(self.request.get('amount')) + + future = taskqueue.add_async( + url='/update_counter', + target='worker', + params={'amount': amount}) + + task = future.wait() + + self.response.write( + 'Task {} enqueued, ETA {}.'.format(task.name, task.eta)) + + +app = webapp2.WSGIApplication([ + ('/', MainPageHandler), + ('/enqueue', EnqueueTaskHandler), + ('/enqueue_async', EnqueueTaskHandler) +], debug=True) diff --git a/appengine/taskqueue/counter/app.yaml b/appengine/taskqueue/counter/application.yaml similarity index 54% rename from appengine/taskqueue/counter/app.yaml rename to appengine/taskqueue/counter/application.yaml index e2b5e55709c0..b4e5019bd3c9 100644 --- a/appengine/taskqueue/counter/app.yaml +++ b/appengine/taskqueue/counter/application.yaml @@ -1,11 +1,8 @@ runtime: python27 api_version: 1 threadsafe: true +module: default handlers: - url: /.* - script: main.app - -libraries: -- name: jinja2 - version: 2.6 + script: application.app diff --git a/appengine/taskqueue/counter/counter_test.py b/appengine/taskqueue/counter/application_test.py similarity index 62% rename from appengine/taskqueue/counter/counter_test.py rename to appengine/taskqueue/counter/application_test.py index 6facb14b0437..7303e7da30c3 100644 --- a/appengine/taskqueue/counter/counter_test.py +++ b/appengine/taskqueue/counter/application_test.py @@ -12,18 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -from google.appengine.ext import ndb -import main +import application import webtest +import worker -def test_app(testbed, run_tasks): - key_name = 'foo' +def test_all(testbed, run_tasks): + test_app = webtest.TestApp(application.app) + test_worker = webtest.TestApp(worker.app) - app = webtest.TestApp(main.app) - app.post('/', {'key': key_name}) - run_tasks(app) + response = test_app.get('/') + assert '0' in response.body - key = ndb.Key('Counter', key_name) - counter = key.get() - assert counter.count == 1 + test_app.post('/enqueue', {'amount': 5}) + run_tasks(test_worker) + + response = test_app.get('/') + assert '5' in response.body diff --git a/appengine/taskqueue/counter/counter.html b/appengine/taskqueue/counter/counter.html deleted file mode 100644 index a9f6a39d3609..000000000000 --- a/appengine/taskqueue/counter/counter.html +++ /dev/null @@ -1,15 +0,0 @@ - - - -
- - -
-