Add error rate to messaging queue
This commit is contained in:
@@ -3,20 +3,28 @@ import pika
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import pika.exceptions as exceptions
|
||||||
|
|
||||||
from cfenv import AppEnv
|
from cfenv import AppEnv
|
||||||
|
from random import randrange
|
||||||
|
|
||||||
class Publisher:
|
class Publisher:
|
||||||
EXCHANGE='robot-shop'
|
EXCHANGE='robot-shop'
|
||||||
TYPE='direct'
|
TYPE='direct'
|
||||||
ROUTING_KEY = 'orders'
|
ROUTING_KEY = 'orders'
|
||||||
|
|
||||||
|
error_rate = 0
|
||||||
|
|
||||||
|
if 'ERROR_RATE' in os.environ:
|
||||||
|
error_rate = int(os.getenv('ERROR_RATE'))
|
||||||
|
|
||||||
def __init__(self, logger):
|
def __init__(self, logger):
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
|
|
||||||
if 'VCAP_SERVICES' in os.environ:
|
if 'VCAP_SERVICES' in os.environ:
|
||||||
self._logger.info('Cloud Foundry detected')
|
self._logger.info('Cloud Foundry detected')
|
||||||
|
|
||||||
if int(os.getenv('CF_INSTANCE_INDEX')) % 2 == 1:
|
if int(os.getenv('CF_INSTANCE_INDEX')) > 2:
|
||||||
# Crash horribly to show how we detect these scenarios
|
# Crash horribly to show how we detect these scenarios
|
||||||
sys.exit(42)
|
sys.exit(42)
|
||||||
|
|
||||||
@@ -57,12 +65,15 @@ class Publisher:
|
|||||||
def publish(self, msg, headers):
|
def publish(self, msg, headers):
|
||||||
if self._channel is None or self._channel.is_closed or self._conn is None or self._conn.is_closed:
|
if self._channel is None or self._channel.is_closed or self._conn is None or self._conn.is_closed:
|
||||||
self._connect()
|
self._connect()
|
||||||
|
|
||||||
|
if error_rate > randrange(100):
|
||||||
|
raise exceptions.ConnectionWrongStateError(
|
||||||
|
'Connection is not open')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._publish(msg, headers)
|
self._publish(msg, headers)
|
||||||
except pika.exceptions.ConnectionClosed:
|
except pika.exceptions.ConnectionClosed:
|
||||||
self._logger.info('reconnecting to queue')
|
self._logger.error('Connection to queue is closed')
|
||||||
self._connect()
|
|
||||||
self._publish(msg, headers)
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self._conn and self._conn.is_open:
|
if self._conn and self._conn.is_open:
|
||||||
|
Reference in New Issue
Block a user