-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatchprocessor.py
78 lines (64 loc) · 2.53 KB
/
batchprocessor.py
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
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# 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.
import sys
if __name__ != "__main__":
print >>sys.stderr, "Don't include batchprocessor.py as a module!"
sys.exit(1)
import traceback
import time
import cStringIO
import dbaccess
import extensions
import review.mail
POLLING_INTERVAL = 5
# Process loop. Terminates on any kind of error (such as if the DB connection
# is lost.)
def processLoop():
db = dbaccess.connect()
cursor = db.cursor()
while True:
cursor.execute("""SELECT DISTINCT roles.uid, batches.id
FROM extensionroles_processchanges AS roles
JOIN batches ON (batches.id > roles.skip)
JOIN reviewusers ON (reviewusers.review=batches.review AND reviewusers.uid=roles.uid)
LEFT OUTER JOIN extensionprocessedbatches AS processed ON (processed.batch=batches.id AND processed.role=roles.id)
WHERE processed.batch IS NULL""")
queue = cursor.fetchall()
if not queue:
# Nothing to do right now; sleep a little to avoid a tight loop of
# DB queries. (Not that the query will be expensive at all in the
# foreseeable future, but)
time.sleep(POLLING_INTERVAL)
else:
print repr(queue)
for user_id, batch_id in queue:
output = cStringIO.StringIO()
if extensions.executeProcessChanges(db, user_id, batch_id, output):
pending_mails = review.mail.sendExtensionOutput(db, user_id, batch_id, output.getvalue())
review.mail.sendPendingMails(pending_mails)
# Main loop.
while True:
try:
try:
processLoop()
except KeyboardInterrupt:
raise
except:
print >>sys.stderr, "".join(traceback.format_exception(*sys.exc_info()))
time.sleep(5)
except KeyboardInterrupt:
break
print "exiting"