earl/beanstalkc — Issue #17: Burst of DeadlineSoon exceptions
Author: gonvaled
Created: 2012-01-30 17:52:48+00:00
State: open

I get over 6700 exceptions in under a second, and then it works normally again.
What exactly happens is as follows (please, see my implementation of BOSS and WORKER below):
1) The boss and the worker connects
2) I put a job in the tube
3) I receive it
4) Some time later, lots of DeadlineSoon exceptions arrive, very fast
5) After ca. 1s, they stop again, and a new job is received (which I never sent)
6) the situation is normal again, for a while, until we find ourselves again in step 4

My implementation is as follows:

class BeanstalkWorker:
'''This class receives jobs from beanstalkd, encoded as JSON strings'''
def __init__ (self, bt_tube, process_data, host = BEANSTALK_HOST, port = BEANSTALK_PORT):
module_log.info("Connecting to beanstalk host %s, port %d, tube %s" % (host, port, bt_tube))
self.json_support = JsonSupport()
self.process_data = process_data
try:
self.beanstalk = beanstalkc.Connection(host=host, port=port)
self.beanstalk.watch(bt_tube)
except beanstalkc.SocketError, e:
module_log.warning("Problems connecting to beanstalk host %s, port %d: %s" % (host, port, e))
self.beanstalk = None

def show_stats(self):
pprint(self.beanstalk.stats())

def run(self):
if not self.beanstalk:
module_log.warning("We have no connection to the beanstalk host")
return
stop_requested = False
while True:
try:
job = self.beanstalk.reserve(timeout=1)
except beanstalkc.DeadlineSoon, e:
module_log.info("DeadlineSoon: %s" % (e))
job = None
if not job:
module_log.info("No job")
if stop_requested : break
else :
module_log.info("Job found")
data = self.json_support.Decode(job.body)
if data:
if data.get('management', None) == 'STOP':
module_log.info("STOP requested")
stop_requested = True
job.delete()
else :
if self.process_data(data):
module_log.info("Job completed")
job.delete()

class BeanstalkBoss:
'''This class sends dictionaries encoded as JSON strings to beanstalkd'''
def __init__ (self, bt_tube, host = BEANSTALK_HOST, port = BEANSTALK_PORT):
module_log.info("Connecting to beanstalk host %s, port %d, tube %s" % (host, port, bt_tube))
try:
self.beanstalk = beanstalkc.Connection(host=host, port=port)
self.beanstalk.use(bt_tube)
except beanstalkc.SocketError, e:
module_log.warning("Problems connecting to beanstalk host %s, port %d: %s" % (host, port, e))
self.beanstalk = None
self.json_support = JsonSupport()

def put(self, data):
if not self.beanstalk:
module_log.warning("We have no connection to the beanstalk host")
return
json = self.json_support.Encode(data)
self.beanstalk.put(json)

This is the output:

2012-01-30 16:11:21,829 - beanstalk_support - INFO - - No job
2012-01-30 16:11:22,833 - beanstalk_support - INFO - - No job
2012-01-30 16:11:23,837 - beanstalk_support - INFO - - No job
2012-01-30 16:11:24,841 - beanstalk_support - INFO - - No job
2012-01-30 16:11:25,845 - beanstalk_support - INFO - - No job
2012-01-30 16:11:26,848 - beanstalk_support - INFO - - No job
2012-01-30 16:11:27,852 - beanstalk_support - INFO - - No job
2012-01-30 16:11:28,856 - beanstalk_support - INFO - - No job
2012-01-30 16:11:29,861 - beanstalk_support - INFO - - No job
2012-01-30 16:11:30,865 - beanstalk_support - INFO - - No job
2012-01-30 16:11:31,520 - beanstalk_support - INFO - - DeadlineSoon: []
2012-01-30 16:11:31,520 - beanstalk_support - INFO - - No job
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - DeadlineSoon: []
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - No job
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - DeadlineSoon: []
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - No job
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - DeadlineSoon: []
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - No job
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - DeadlineSoon: []
2012-01-30 16:11:31,521 - beanstalk_support - INFO - - No job
...