Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring taskqueue sample #261

Merged
merged 1 commit into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions appengine/taskqueue/counter/README.md
Original file line number Diff line number Diff line change
@@ -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

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

Expand Down
74 changes: 74 additions & 0 deletions appengine/taskqueue/counter/application.py
Original file line number Diff line number Diff line change
@@ -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}<br>
<form method="post" action="/enqueue">
<label>Increment amount</label>
<input name="amount" value="1">
<button>Enqueue task</button>
</form>
""".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)
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 0 additions & 15 deletions appengine/taskqueue/counter/counter.html

This file was deleted.

62 changes: 0 additions & 62 deletions appengine/taskqueue/counter/main.py

This file was deleted.

2 changes: 1 addition & 1 deletion appengine/taskqueue/counter/queue.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
queue:
# Change the refresh rate of the default queue from 5/s to 1/s
# Change the refresh rate of the default queue from 5/s to 1/s.
- name: default
rate: 1/s
44 changes: 44 additions & 0 deletions appengine/taskqueue/counter/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.ext import ndb
import webapp2


COUNTER_KEY = 'default counter'


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


class UpdateCounterHandler(webapp2.RequestHandler):
def post(self):
amount = int(self.request.get('amount'))

# This task should run at most once per second because of the datastore
# transaction write throughput.
@ndb.transactional
def update_counter():
counter = Counter.get_or_insert(COUNTER_KEY, count=0)
counter.count += amount
counter.put()

update_counter()


app = webapp2.WSGIApplication([
('/update_counter', UpdateCounterHandler)
], debug=True)
# [END all]
8 changes: 8 additions & 0 deletions appengine/taskqueue/counter/worker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
runtime: python27
api_version: 1
threadsafe: true
module: worker

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