Commit 2e565f06 authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Added basic exception handling for cli commands.

parent 84ff9ddc
Loading
Loading
Loading
Loading
+15 −4
Original line number Diff line number Diff line
@@ -7,8 +7,12 @@

import json
import redis
import sys
import uuid

from redis.exceptions import ConnectionError


class RedisRPCClient(object):

    def __init__(self, host, port, db, rpcQueue):
@@ -18,8 +22,15 @@ class RedisRPCClient(object):
    def call(self, request):
        requestId = uuid.uuid1().hex
        request["req_id"] = requestId
        try:
            self.client.lpush(self.rpcQueue, json.dumps(request))
            channel, response = self.client.brpop(requestId, timeout = 30)
        except ConnectionError:
            sys.exit("\nFATAL: unable to establish a connection with the message broker (Redis).\n")
        except TypeError:
            sys.exit("""\nFATAL: no response received.\n
Please check if the message broker (Redis) and the database (PostgreSQL) are up and running.\n""")
        else:
            response = json.loads(response.decode("utf-8"))
            return response