Beispiel #1
0
 def destroy(self):
     debug_cyan('Closing connection to the broker.')
     if self.queue:
         self.channel.queue_unbind(self.queue, exchange=self.exchange)
         self.channel.queue_purge(self.queue)
     self.channel.close()
     self.connection.close()
Beispiel #2
0
def send(args):
    debug_cyan(
        'Trying to send messages: [{}]'.format(', '.join(args.messages)), )

    with RabbitDumper(args) as dump:
        for msg in args.messages:
            dump.send(msg)
Beispiel #3
0
def record(args):
    try:
        with RabbitDumper(args) as dump, MsgPickler(args) as pickler:
            debug_cyan('Recording messages...')
            dump.receive(lambda msg: log_message(pickler, msg), full_msg=True)
    except KeyboardInterrupt:
        print('')
        sys.exit(0)
Beispiel #4
0
 def flush(self):
     if not self.cahce_dirty:
         return
     debug_cyan(f'Flushing messages in {self.output}')
     for msg in self.cache:
         pickle.dump(msg, self.file_descriptor)
         self.cache.clear()
         self.cahce_dirty = False
Beispiel #5
0
def read(args):
    try:
        with RabbitDumper(args) as dump:
            debug_cyan('Reading the standard ouput.')
            for line in sys.stdin:
                dump.send(line[0:-1])
    except KeyboardInterrupt:
        print('')
        sys.exit(0)
Beispiel #6
0
def monitor(args):
    try:
        printer = MessagePrinter(args)
        with RabbitDumper(args) as dump:
            debug_cyan('Monitoring for messages:')
            dump.receive(printer.print_message)
    except KeyboardInterrupt:
        print('')
        sys.exit(0)
Beispiel #7
0
def pipe(args):
    path = args.pipe_name
    debug_cyan(f'Trying to open pipe on {path}')
    if os.path.exists(path) or os.path.isfile(path):
        print('The given path is already exists: {}'.format(path), )
        sys.exit(1)

    with RabbitDumper(args) as dump:
        try:
            os.mkfifo(path)
            debug('Pipe creted')
            with open(path) as pipe_fd:
                while True:
                    message = pipe_fd.readline()
                    if message:
                        dump.send(message)
                        time.sleep(0.5)
        except OSError as err:
            print('Error wile opening pipe: {}'.format(err), )
        finally:
            debug('Deliting trhe named pipe')
            os.remove(path)
Beispiel #8
0
    def receive(self, callback, full_msg=False):
        self.callback = callback
        self.full_msg = full_msg

        if self.queue:
            self.queue = self.channel.queue_declare(queue=self.queue, auto_delete=True).method.queue
        else:
            self.queue = self.channel.queue_declare(queue='', auto_delete=True).method.queue

        debug(f'Declared queue with name {self.queue}')

        if self.exchange is not None:
            self.channel.queue_bind(
                exchange=self.exchange,
                queue=self.queue,
                routing_key=self.routing_key,
            )
            debug(f'Queue was bound to the exchange {self.exchange} with\
routing key {self.routing_key}')

        debug_cyan(f'Starting to recieve messages from {self.queue}')

        try:
            self.channel.basic_consume(
                queue=self.queue, on_message_callback=self.new_msg,
                auto_ack=True,
            )
            self.channel.start_consuming()
        except pika.exceptions.ConnectionClosedByBroker as err:
            print(f'AMQP Connection closed by the broker: {err}.')
        except pika.exceptions.AMQPChannelError as err:
            print(f'AMQP channel error: {err}.')
        except pika.exceptions.AMQPConnectionError as err:
            print(f'AMQP Connection closed: {err}.')
        except pika.exceptions.StreamLostError as err:
            print(f'AMQP Stream lost: {err}.')
Beispiel #9
0
    def __init__(self, args):

        self.args = args

        self.exchange = args.exchange
        self.queue = args.queue
        self.routing_key = args.routing_key
        self.server = args.server

        self.callback = None
        self.full_msg = None

        debug_cyan(f'Trying to open connection to {args.server}')
        try:
            try:
                self.connection = pika.BlockingConnection(
                    pika.ConnectionParameters(host=args.server),
                )
            except:  # noqa: E722
                print('Establishing AMQP Connection failed! Check the server!')
                sys.exit(1)

            debug('Connection opend')

            self.channel = self.connection.channel()
            self.channel.basic_qos(prefetch_count=1)

        except pika.exceptions.ConnectionClosedByBroker as err:
            print(f'AMQP Connection closed by the broker: {err}')
            sys.exit(1)
        except pika.exceptions.AMQPChannelError as err:
            print(f'AMQP channel error: {err}, stopping...')
            sys.exit(1)
        except pika.exceptions.AMQPConnectionError as err:
            print(f'AMQP Connection closed: {err}')
            sys.exit(1)
Beispiel #10
0
 def new_msg(self, _, method, properties, body):  # noqa: F831
     body = body.decode('utf-8', 'ignore')
     log = body if len(body) < 10 else body[:9] + '...'
     debug_cyan(f'New message received: {log}')
     msg = Message(body, properties.headers, method.exchange, method.routing_key)
     self.callback(msg)
Beispiel #11
0
def log_message(pickler, msg):
    msg.timestamp = msg.props.timestamp if msg.props and msg.props.timestamp else time.time(
    )
    debug_cyan(
        f'Saving message from {msg.exchange} and with key {msg.routing_key}')
    pickler.push_msg(msg)