3 # Copyright (C) 2011 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """An very simple app using pull queues to tally votes."""
19 __author__
= 'nverne@google.com (Nicholas Verne)'
24 from google
.appengine
.api
import memcache
25 from google
.appengine
.api
import taskqueue
26 from google
.appengine
.ext
import db
27 from google
.appengine
.ext
import webapp
28 from google
.appengine
.ext
.webapp
import template
29 from google
.appengine
.ext
.webapp
.util
import run_wsgi_app
31 LANGUAGES
= ['Java', 'Go', 'C++', 'Perl', 'Python']
33 class Tally(db
.Model
):
34 """Simple counting model."""
35 count
= db
.IntegerProperty(default
=0, required
=True)
38 def increment_by(cls
, key_name
, count
):
39 """Increases a Tally's count. Should be run in a transaction."""
40 tally
= cls
.get_by_key_name(key_name
)
42 tally
= cls(key_name
=key_name
)
47 class VoteHandler(webapp
.RequestHandler
):
48 """Handles adding of vote tasks."""
51 """Displays the voting form."""
52 self
.render_template('index.html', {'tallies': self
.get_tallies(),
53 'LANGUAGES': LANGUAGES
})
56 """Adds tasks to votes queue if ugliest is valid."""
57 ugliest
= self
.request
.get('ugliest')
59 if ugliest
and ugliest
in LANGUAGES
:
60 q
= taskqueue
.Queue('votes')
61 q
.add(taskqueue
.Task(payload
=ugliest
, method
='PULL'))
64 def get_tallies(self
):
65 """Fetches tallies from memcache if possible, otherwise from datastore."""
66 tallies
= memcache
.get('tallies')
68 tallies
= Tally
.all().fetch(len(LANGUAGES
))
69 memcache
.set('tallies', tallies
, time
=5)
72 def render_template(self
, name
, template_args
):
73 """Renders a named django template."""
74 path
= os
.path
.join(os
.path
.dirname(__file__
), 'templates', name
)
75 self
.response
.out
.write(template
.render(path
, template_args
))
78 class TallyHandler(webapp
.RequestHandler
):
79 """Pulls tasks from the vote queue."""
81 def store_tallies(self
, tallies
):
82 """Updates the tallies in datastore."""
83 for key_name
, count
in tallies
.iteritems():
84 db
.run_in_transaction(Tally
.increment_by
, key_name
, count
)
87 """Leases vote tasks, accumulates tallies and stores them."""
88 q
= taskqueue
.Queue('votes')
89 # Keep leasing tasks in a loop. When the task fails due to
90 # deadline, it should be retried.
92 tasks
= q
.lease_tasks(300, 1000)
94 # Let the retry parameters of the queue cause this
95 # task to be rescheduled.
100 # accumulate tallies in memory
102 tallies
[t
.payload
] = tallies
.get(t
.payload
, 0) + 1
104 self
.store_tallies(tallies
)
105 q
.delete_tasks(tasks
)
107 class StartHandler(webapp
.RequestHandler
):
108 """Starts some tally tasks."""
110 workers
= int(self
.request
.get('workers', 2))
111 q
= taskqueue
.Queue('tally')
116 q
.add([taskqueue
.Task(url
='/tally',
117 countdown
=x
) for x
in xrange(workers
)])
120 application
= webapp
.WSGIApplication([
122 ('/tally', TallyHandler
),
123 ('/start', StartHandler
),
128 run_wsgi_app(application
)
130 if __name__
== '__main__':