File tree 2 files changed +21
-7
lines changed
appengine/taskqueue/pull-counter 2 files changed +21
-7
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,13 @@ def post(self):
48
48
self .redirect ('/' )
49
49
50
50
51
+ @ndb .transactional
52
+ def update_counter (key , tasks ):
53
+ counter = Counter .get_or_insert (key , count = 0 )
54
+ counter .count += len (tasks )
55
+ counter .put ()
56
+
57
+
51
58
class CounterWorker (webapp2 .RequestHandler ):
52
59
def get (self ):
53
60
"""Indefinitely fetch tasks and update the datastore."""
@@ -60,20 +67,18 @@ def get(self):
60
67
logging .exception (e )
61
68
time .sleep (1 )
62
69
continue
70
+
63
71
if tasks :
64
72
key = tasks [0 ].tag
65
73
66
- @ndb .transactional
67
- def update_counter ():
68
- counter = Counter .get_or_insert (key , count = 0 )
69
- counter .count += len (tasks )
70
- counter .put ()
71
74
try :
72
- update_counter ()
75
+ update_counter (key , tasks )
73
76
except Exception as e :
74
77
logging .exception (e )
75
- else :
78
+ raise
79
+ finally :
76
80
queue .delete_tasks (tasks )
81
+
77
82
time .sleep (1 )
78
83
79
84
Original file line number Diff line number Diff line change 16
16
17
17
from google .appengine .ext import testbed as gaetestbed
18
18
import main
19
+ import mock
19
20
import webtest
20
21
21
22
@@ -31,3 +32,11 @@ def test_app(testbed):
31
32
tasks = tq_stub .get_filtered_tasks ()
32
33
assert len (tasks ) == 1
33
34
assert tasks [0 ].name == 'task1'
35
+
36
+ with mock .patch ('main.update_counter' ) as mock_update :
37
+ # Force update to fail, otherwise the loop will go forever.
38
+ mock_update .side_effect = RuntimeError ()
39
+
40
+ app .get ('/_ah/start' , status = 500 )
41
+
42
+ assert mock_update .called
You can’t perform that action at this time.
0 commit comments