Skip to content
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
19 changes: 12 additions & 7 deletions appengine/taskqueue/pull-counter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def post(self):
self.redirect('/')


@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."""
Expand All @@ -60,20 +67,18 @@ def get(self):
logging.exception(e)
time.sleep(1)
continue

if tasks:
key = tasks[0].tag

@ndb.transactional
def update_counter():
counter = Counter.get_or_insert(key, count=0)
counter.count += len(tasks)
counter.put()
try:
update_counter()
update_counter(key, tasks)
except Exception as e:
logging.exception(e)
else:
raise
finally:
queue.delete_tasks(tasks)

time.sleep(1)


Expand Down
9 changes: 9 additions & 0 deletions appengine/taskqueue/pull-counter/pullcounter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from google.appengine.ext import testbed as gaetestbed
import main
import mock
import webtest


Expand All @@ -31,3 +32,11 @@ def test_app(testbed):
tasks = tq_stub.get_filtered_tasks()
assert len(tasks) == 1
assert tasks[0].name == 'task1'

with mock.patch('main.update_counter') as mock_update:
# Force update to fail, otherwise the loop will go forever.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably missing some context, but is it not weird to have a worker that polls indefinitely? I'd expect a cron job or something instead, which you can at least cancel?

mock_update.side_effect = RuntimeError()

app.get('/_ah/start', status=500)

assert mock_update.called