Skip to content

Commit

Permalink
Merge branch 'master' into sendgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
waprin committed Apr 15, 2016
2 parents 5618848 + f81552c commit 71d55bf
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 56 deletions.
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
82 changes: 82 additions & 0 deletions appengine/taskqueue/counter/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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))


# AsyncEnqueueTaskHandler behaves the same as EnqueueTaskHandler, but shows
# how to queue the task using the asyncronous API. This is not wired up by
# default. To use this, change the MainPageHandler's form action to
# /enqueue_async
class AsyncEnqueueTaskHandler(webapp2.RequestHandler):
def post(self):
amount = int(self.request.get('amount'))

queue = taskqueue.Queue(name='default')
task = taskqueue.Task(
url='/update_counter',
target='worker',
params={'amount': amount})

rpc = queue.add_async(task)

# Wait for the rpc to complete and return the queued task.
task = rpc.get_result()

self.response.write(
'Task {} enqueued, ETA {}.'.format(task.name, task.eta))


app = webapp2.WSGIApplication([
('/', MainPageHandler),
('/enqueue', EnqueueTaskHandler),
('/enqueue_async', AsyncEnqueueTaskHandler)
], 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.

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
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,34 @@
# limitations under the License.

# [START all]
import os

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


JINJA_ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
COUNTER_KEY = 'default counter'


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))

class UpdateCounterHandler(webapp2.RequestHandler):
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')
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(key, count=0)
counter.count += 1
counter = Counter.get_or_insert(COUNTER_KEY, count=0)
counter.count += amount
counter.put()

update_counter()


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

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

0 comments on commit 71d55bf

Please sign in to comment.