def _open_connection(self): """Retries connecting to the AMQP broker over time. See :func:`celery.utils.retry_over_time`. """ def _connection_error_handler(exc, interval): """Callback handler for connection errors.""" self.logger.error("AMQP Listener: Connection Error: %s. " % exc + "Trying again in %d seconds..." % interval) def _establish_connection(): """Establish a connection to the AMQP broker.""" conn = DjangoBrokerConnection() connected = conn.connection # Connection is established lazily. return conn if not conf.AMQP_CONNECTION_RETRY: return _establish_connection() conn = retry_over_time(_establish_connection, (socket.error, IOError), errback=_connection_error_handler, max_retries=conf.AMQP_CONNECTION_MAX_RETRIES) self.logger.debug("CarrotListener: Connection Established.") return conn
def test_returns_retval_on_success(self): def _fun(x, y): return x * y ret = utils.retry_over_time(_fun, (socket.error, ), args=[16, 16], max_retries=3) self.assertEqual(ret, 256)
def test_retries_on_failure(self): iterations = [0] def _fun(x, y): iterations[0] += 1 if iterations[0] == 3: return x * y raise socket.error("foozbaz") ret = utils.retry_over_time(_fun, (socket.error, ), args=[32, 32], max_retries=None) self.assertEqual(iterations[0], 3) self.assertEqual(ret, 1024) self.assertRaises(socket.error, utils.retry_over_time, _fun, (socket.error, ), args=[32, 32], max_retries=1)