forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (72 loc) · 2.6 KB
/
main.py
File metadata and controls
91 lines (72 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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]
"""A simple counter with App Engine pull queue."""
import logging
import os
import time
from google.appengine.api import taskqueue
from google.appengine.ext import ndb
from google.appengine.runtime import apiproxy_errors
import jinja2
import webapp2
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))
# [START adding_task]
def post(self):
key = self.request.get('key')
if key:
queue = taskqueue.Queue('pullq')
queue.add(taskqueue.Task(payload='', method='PULL', tag=key))
self.redirect('/')
# [END adding_task]
@ndb.transactional
def update_counter(key, tasks):
counter = Counter.get_or_insert(key, count=0)
counter.count += len(tasks)
counter.put()
class CounterWorker(webapp2.RequestHandler):
def get(self):
"""Indefinitely fetch tasks and update the datastore."""
queue = taskqueue.Queue('pullq')
while True:
try:
tasks = queue.lease_tasks_by_tag(3600, 1000, deadline=60)
except (taskqueue.TransientError,
apiproxy_errors.DeadlineExceededError) as e:
logging.exception(e)
time.sleep(1)
continue
if tasks:
key = tasks[0].tag
try:
update_counter(key, tasks)
except Exception as e:
logging.exception(e)
raise
finally:
queue.delete_tasks(tasks)
time.sleep(1)
app = webapp2.WSGIApplication([
('/', CounterHandler),
('/_ah/start', CounterWorker)
], debug=True)
# [END all]