Fix web queues not always being present in beanstalkd, meaning dropped events
This commit is contained in:
@@ -17,6 +17,8 @@ class Event(object):
|
||||
def __init__(self, type, data):
|
||||
self.type = type
|
||||
self.data = data
|
||||
def __repr__(self):
|
||||
return json.dumps(self, cls=EventEncoder)
|
||||
|
||||
class ChatEvent(Event):
|
||||
def __init__(self, sender, message):
|
||||
@@ -113,6 +115,14 @@ def web_queue(id):
|
||||
port = settings.CAMINUS_BEANSTALKD_PORT)
|
||||
queue.use(queueName)
|
||||
queue.watch(queueName)
|
||||
activeCache = cache.get('minecraft-web-queues')
|
||||
if activeCache is None:
|
||||
activeCache = {}
|
||||
activeCache[id] = time.time()
|
||||
for queueName,stamp in activeCache.iteritems():
|
||||
if time.time()-stamp > 120:
|
||||
del activeCache[queueName]
|
||||
cache.set('minecraft-web-queues', activeCache, 3600)
|
||||
return queue
|
||||
|
||||
def market_queue():
|
||||
@@ -140,15 +150,22 @@ def send_web_event(event):
|
||||
queue = beanstalkc.Connection(host=settings.CAMINUS_BEANSTALKD_HOST,
|
||||
port = settings.CAMINUS_BEANSTALKD_PORT)
|
||||
json = dumps({'stamp': time.time(), 'event':event}, cls=EventEncoder)
|
||||
for tube in queue.tubes():
|
||||
if tube.startswith("caminus-web-"):
|
||||
queue.use(tube)
|
||||
pendingJob = queue.peek_ready()
|
||||
if pendingJob:
|
||||
pending = loads(pendingJob.body)
|
||||
if time.time()-pending['stamp'] > 30:
|
||||
pendingJob.delete()
|
||||
queue.put(json)
|
||||
activeQueues = cache.get('minecraft-web-queues')
|
||||
print "Active queues", repr(activeQueues)
|
||||
if activeQueues is None:
|
||||
activeQueues = {}
|
||||
for tube in activeQueues.iterkeys():
|
||||
queue.use('caminus-web-%s'%(tube))
|
||||
print "Web queue", tube
|
||||
pendingJob = queue.peek_ready()
|
||||
while pendingJob:
|
||||
pending = loads(pendingJob.body)
|
||||
if time.time()-pending['stamp'] > 30:
|
||||
pendingJob.delete()
|
||||
pendingJob = queue.peek_ready()
|
||||
else:
|
||||
pendingJob = None
|
||||
queue.put(json)
|
||||
|
||||
def chat(playername, message):
|
||||
evt = ChatEvent(playername, message)
|
||||
|
Reference in New Issue
Block a user