Refactor AMQP retry loop

This commit is contained in:
Josh Kearney
2010-11-22 15:22:02 -06:00
parent 242db7aebd
commit e5b513fe3d

View File

@@ -86,25 +86,25 @@ class Consumer(messaging.Consumer):
Contains methods for connecting the fetch method to async loops Contains methods for connecting the fetch method to async loops
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.failed_connection = False for i in xrange(AMQP_MAX_RETRIES):
if i > 0:
for i in range(AMQP_MAX_RETRIES): time.sleep(AMQP_RETRY_INT)
try: try:
super(Consumer, self).__init__(*args, **kwargs) super(Consumer, self).__init__(*args, **kwargs)
self.failed_connection = False
break break
except: # Catching all because carrot sucks except: # Catching all because carrot sucks
if i + 1 == AMQP_MAX_RETRIES: logging.exception("AMQP server on %s:%d is unreachable." \
logging.exception("Unable to connect to AMQP server" \ " Trying again in %d seconds." % (
" after %d tries. Shutting down." % AMQP_MAX_RETRIES) FLAGS.rabbit_host,
sys.exit(1) FLAGS.rabbit_port,
else: AMQP_RETRY_INT))
logging.exception("AMQP server on %s:%d is unreachable." \ self.failed_connection = True
" Trying again in %d seconds." % ( continue
FLAGS.rabbit_host, if self.failed_connection:
FLAGS.rabbit_port, logging.exception("Unable to connect to AMQP server" \
AMQP_RETRY_INT)) " after %d tries. Shutting down." % AMQP_MAX_RETRIES)
time.sleep(AMQP_RETRY_INT) sys.exit(1)
continue
def fetch(self, no_ack=None, auto_ack=None, enable_callbacks=False): def fetch(self, no_ack=None, auto_ack=None, enable_callbacks=False):
"""Wraps the parent fetch with some logic for failed connections""" """Wraps the parent fetch with some logic for failed connections"""