switch to Peter's round-robin scheduling approach

This commit is contained in:
Seth Schoen
2012-08-08 17:30:44 -07:00
parent 7c814bd7b1
commit c6453513b6
+21 -27
View File
@@ -280,42 +280,36 @@ dispatch = { "makechallenge": ("pending-makechallenge", makechallenge),
"done": ("pending-done", lambda x: None) } "done": ("pending-done", lambda x: None) }
# Main loop: act on queues notified via Redis pubsub mechanism. # Main loop: act on queues notified via Redis pubsub mechanism.
# If the queue is empty by the time we pop from it (indicated by # Currently, we ignore the specific details of which queue was
# session is None), some other daemon instance has already handled # notified and, upon any notification, repeatedly process a single
# the request, which is fine; we then return immediately to waiting # item from each queue until all queues are empty.
# for the next request.
ps.subscribe(["requests"]) ps.subscribe(["requests"])
for message in ps.listen(): for message in ps.listen():
populated_queue = message["data"] populated_queue = message["data"]
if populated_queue in dispatch: if populated_queue == "clean-exit":
queue, function = dispatch[populated_queue] pass # fall through to check whether this particular daemon
for repetition in (1,2): # instance has its clean_shutdown flag set
# This is a potentially inappropriate hack to prevent backlogs else:
# from accumulating in queues if daemons die or if spurious while True:
# requests arrived while no daemons were listening. The idea inactive = True
# is that every daemon process double-checks a queue that it for queue in ("makechallenge", "testchallenge", "issue"):
# was told to look at, processing it twice per pubsub message session = r.rpop("pending-" + queue)
# instead of once. This causes a pressure on the daemon to if session:
# empty the queue over time even if it isn't explicitly asked inactive = False
# to. For example, if asked 7 times to process a particular if debug: print "going to %s for %s" % (queue, session)
# queue, a daemon instance would try 14 times. if ancient(session, queue):
session = r.rpop(queue) if queue == "issue":
if session:
if debug: print "going to %s for %s" % (populated_queue, session)
if ancient(session, populated_queue):
if populated_queue == "issue":
if debug: print "not expiring issue-state", session if debug: print "not expiring issue-state", session
else: else:
if debug: print "expiring ancient session", session if debug: print "expiring ancient session", session
r.hset(session, "live", False) r.hset(session, "live", False)
else: else:
function(session) if queue == "makechallenge": makechallenge(session)
elif populated_queue == "clean-exit": elif queue == "testchallenge": testchallenge(session)
pass # fall through to check whether this particular daemon elif queue == "issue": issue(session)
# instance has its clean_shutdown flag set if inactive:
else: break
if debug: print "UNKNOWN queue %s" % populated_queue
if clean_shutdown: if clean_shutdown:
print "daemon exiting cleanly" print "daemon exiting cleanly"