Skip to content

Commit

Permalink
auto-retry on example producer when redis fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mlasevich committed Oct 22, 2024
1 parent 6dc3759 commit 78f5644
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions examples/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Example Producer for a queue
In this example we create RedisSMQ controller and using it to send messages to that at
regular intervals
In this example we create RedisSMQ controller and using it to send messages
to that at regular intervals
"""
import argparse
Expand All @@ -16,6 +16,8 @@

LOG = logging.getLogger("Producer")

MAX_DELAY = 30

redis_host = os.environ.get("REDIS_HOST", "127.0.0.1")


Expand All @@ -26,7 +28,8 @@ def create_message(msg_number):

def produce(rsmq, long_qname, count, interval):
LOG.info(
"Starting producer for queue '%s' - sending %s messages every %s seconds...",
"Starting producer for queue '%s' - sending %s messages every %s "
"seconds...",
long_qname,
count,
interval,
Expand All @@ -45,7 +48,7 @@ def produce(rsmq, long_qname, count, interval):
LOG.info("Ended producer after sending %s messages.", msg_count)


def main(argv=None):
def loop(argv=None):
if argv is None:
argv = sys.argv
""" Parse args and run producer """
Expand Down Expand Up @@ -153,7 +156,8 @@ def main(argv=None):
)

if args.delete:
rsqm.deleteQueue(qname=args.queue, quiet=True).exceptions(False).execute()
rsqm.deleteQueue(qname=args.queue, quiet=True).exceptions(
False).execute()

# Create queue if it is missing. Swallow errors if already exists
rsqm.createQueue(qname=args.queue, quiet=True).exceptions(False).execute()
Expand All @@ -162,6 +166,21 @@ def main(argv=None):
produce(rsqm, "%s:%s" % (args.ns, args.queue), args.count, args.interval)


def main(argv=None):
""" Main with retry """
delay = 1
while True:
try:
loop(argv)
delay = 1
except Exception as ex:
LOG.error("Error: %s", ex)
LOG.info("Sleeping %s seconds before restarting...", delay)
time.sleep(delay)
delay *= 2
delay = min(delay, MAX_DELAY)


if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main(sys.argv)

0 comments on commit 78f5644

Please sign in to comment.