示例#1
0
    def run(self):
        # todo: change this
        set_seed(42)

        logging.info("Worker is running")

        params = pika.ConnectionParameters("localhost")
        conn = pika.BlockingConnection(params)

        with conn:
            channel: pika.channel.Channel = conn.channel()
            channel.queue_declare(self._queue_name)

            for method_frame, properties, body in channel.consume(
                    self._queue_name):
                logging.debug(
                    f"processing request for correlation ID {properties.correlation_id}"
                )

                # parse the arguments
                try:
                    payload = json.loads(body)
                except json.JSONDecodeError:
                    logging.exception(
                        "Failed to decode payload, message is invalid")
                    channel.basic_reject(method_frame.delivery_tag, False)

                start_time = time.time()

                response = {}
                try:
                    response["result"] = self._process_message(payload)
                except Exception as e:
                    traceback.print_exc()
                    response[
                        "error"] = "Error running the model. Check the logs for exception details."

                response["time"] = int(round(
                    (time.time() - start_time) * 1000))

                channel.basic_publish(
                    exchange="",
                    routing_key=properties.reply_to,
                    properties=pika.BasicProperties(
                        correlation_id=properties.correlation_id),
                    body=json.dumps(response))
                channel.basic_ack(method_frame.delivery_tag)

                logging.debug(
                    f"request finished for correlation ID {properties.correlation_id}"
                )
def main():

    url = 'amqps://*****:*****@fish.rmq.cloudamqp.com/kyiirjzl'
    parametros = pika.URLParameters(url)
    connection = pika.BlockingConnection(parametros)
    channel = connection.channel()
    channel.queue_declare(queue='Queue_Data')

    def callback(ch, method, properties, body):
        print(body.decode())

    channel.basic_consume(queue='Queue_Data',
                          auto_ack=True,
                          on_message_callback=callback)

    channel.start_consuming()
示例#3
0
def _retry_message(
    name: str,
    retry_exchange: str,
    channel: channel.Channel,
    method: frame.Method,
    properties: spec.BasicProperties,
    body: str,
    delay: float,
) -> None:
    delay = int(delay * 1000)
    # Create queue that should be automatically deleted shortly after
    # the last message expires. The queue is re-declared for each retry
    # which resets the queue expiry.
    delay_name = '{}-delay-{}'.format(name, delay)
    result = channel.queue_declare(
        queue=delay_name,
        durable=True,
        arguments={
            'x-dead-letter-exchange': retry_exchange,
            'x-message-ttl': delay,
            'x-expires': delay + 10000,
        },
    )
    queue_name = result.method.queue
    # Bind the wait queue to the delay exchange before publishing
    channel.exchange_declare(
        exchange=delay_name,
        durable=True,
        auto_delete=True,  # Delete exchange when queue is deleted
        exchange_type='topic')
    channel.queue_bind(exchange=delay_name, routing_key='#', queue=queue_name)
    channel.basic_publish(exchange=delay_name,
                          routing_key=method.routing_key,
                          body=body,
                          properties=properties)
def send_replication_xml_to_queue(xml):
   
    print ('Connecting to Queue with ')
    print ('queue type - '+str(message_queue_type))
    print ('queue host - '+str(message_queue_host))
    print ('queue port - '+str(message_queue_port))
    print ('queue queue - '+str(message_replicate_queuue))
    
    credentials = pika.PlainCredentials(username= message_user_name, password=message_password)
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=message_queue_host, credentials= credentials ))
    channel = connection.channel()
    channel.queue_declare(queue=message_replicate_queuue)
    
    channel.basic_publish(exchange='', routing_key=message_replicate_queuue,body=str(xml)) 
    connection.close()
    
    return 
    def run(self, args, opts, *a, **kw):
        config = ConfigParser()
        config.read("python/configs/config.ini")

        message_count = int(config.get("RABBIT", "message_count"))
        QUEUE_NAME = config.get("QUEUES", "links_pusher")

        channel = RabbitMQ.get_channel()

        logger = get_logger('[' + QUEUE_NAME + ']')
        for i in range(1000, 16939579):
            try:
                stats = channel.queue_declare(queue=QUEUE_NAME, durable=True)
                if stats.method.message_count < 6000:
                    link = self.get_link(i)
                    try:
                        channel.basic_publish(
                            exchange='',
                            routing_key=QUEUE_NAME,
                            properties=BasicProperties(delivery_mode=2),
                            body=JSONEncoder().encode({
                                'url': link,
                            }))
                    except AMQPError as connection_error:
                        time.sleep(10)
                        logger.error("AMQP error")
                        channel = RabbitMQ.get_channel()
                        channel.queue_declare(queue=QUEUE_NAME, durable=True)
                        time.sleep(3)
                    logger.info(f"Pushed url: {link}")
                else:
                    logger.debug(
                        f'There are more then {message_count} messages in the Queue! Sleep for 10 seconds...'
                    )
                    time.sleep(10)
            except ChannelClosed as channel_closed_error:
                logger.error("Channel closed")
                logger.info("Sleep for 10 seconds...")
                time.sleep(10)
                channel = RabbitMQ.get_channel()
                channel.queue_declare(queue=QUEUE_NAME, durable=True)
            except Exception as error:
                logger.error(repr(error))
                DB.reconnect()
                logger.info("Sleep for 10 seconds...")
                time.sleep(10)
示例#6
0
def main():
    # Check motes status every 60 seconds
    interval_monitor = setInterval(checkMotesInactive, 60)
    try:
        connection = pika.BlockingConnection(pika.ConnectionParameters(host=EVENT_SERVER))
        channel = connection.channel()    
        channel.exchange_declare(exchange=EXCHANGE_READINGS, type='fanout')    
        result = channel.queue_declare(exclusive=True)
        queue_name = result.method.queue    
        channel.queue_bind(exchange=EXCHANGE_READINGS, queue=queue_name)
    except Exception, e:
        print("Error %s:" % e.args[0])
示例#7
0
def main():
    # Check motes status every 60 seconds
    interval_monitor = setInterval(checkMotesInactive, 60)
    try:
        connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=EVENT_SERVER))
        channel = connection.channel()
        channel.exchange_declare(exchange=EXCHANGE_READINGS, type='fanout')
        result = channel.queue_declare(exclusive=True)
        queue_name = result.method.queue
        channel.queue_bind(exchange=EXCHANGE_READINGS, queue=queue_name)
    except Exception, e:
        print("Error %s:" % e.args[0])
示例#8
0
def main(argv):

    ledthread = Thread(target=led)
    ledthread.start()

    broker = "netapps.ece.vt.edu"
    vhost = "/2014/fall/sentry"
    login = "******"
    password = "******"
    rkey = "bennybene"

    message_broker = None
    channel = None
    try:
        # Connect to message broker
        message_broker = pika.BlockingConnection(
            pika.ConnectionParameters(host=broker,
                                      virtual_host=vhost,
                                      credentials=pika.PlainCredentials(
                                          login, password, True)))

        print "Connected to message broker"
        channel = message_broker.channel()
        channel.exchange_declare(exchange="alarms", type="direct")

        signal_num = signal.SIGINT
        # Create eventg handler if signal is caught
        try:
            channel_manager = StatsClientChannelHelper(channel)
            signal.signal(signal_num, channel_manager.stop_stats_client)
            signal_num = signal.SIGTERM
            signal.signal(signal_num, channel_manager.stop_stats_client)

        except ValueError, ve:
            print "Warning: Graceful shutdown may not be possible: Unsupported signal: " + signal_num

        # Create a queue to receive messages
        my_queue = channel.queue_declare(exclusive=True)

        # Bind the queue to the stats exchange
        channel.queue_bind(exchange="alarms",
                           queue=my_queue.method.queue,
                           routing_key=rkey)

        # Setup callback for when a subscribed message is received
        channel.basic_consume(on_new_msg,
                              queue=my_queue.method.queue,
                              no_ack=True)
        print "New message from broker"

        channel.start_consuming()
示例#9
0
import pika
from pika import channel

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='', routing_key='hello', body='hello world')

print(" [x] Sent 'Hello World!'")

connection.close()
示例#10
0
import pika, json
from pika import channel

from main import Product, db


params = pika.URLParameters('amqps://*****:*****@baboon.rmq.cloudamqp.com/qtujcpio')

connection = pika.BlockingConnection(params)

channel = connection.channel()

channel.queue_declare(queue='main')

def callback(ch, method, properties, body):
    print(f'Receive in main: {body}')
    data = json.loads(body)
    print(data)

    if properties.content_type == 'product created':
        product = Product(id=data['id'], title=data['title'], image=data['image'])
        db.session.add(product)
        db.session.commit()
        print("Product created")

    elif properties.content_type == 'product updated':
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        db.session.commit()
        print("Product updated")
示例#11
0
import pika,json,os,django
from pika import connection
from pika import channel
from main import Product, db

 # below 2 line added because this file is outside django app and we are using it without this django app throw an error
os.environ.setdefault("DJANGO_SETTING_MODULE","admin.settings") 
django.setup()

params = pika.URLParameters(
    "amqps://*****:*****@hornet.rmq.cloudamqp.com/lrtmtglx")
connection = pika.BlockingConnection(params)

channel = connection.channel()
channel.queue_declare(queue="main")


def callback(ch, method, properties, body):
    print("Received in main")
    data = json.loads(body)
    print(data)

    try:
        if properties.content_type == "product_created":
            product = Product(
                id=data["id"], title=data["title"], image=data["image"])
            db.session.add(product)
            db.session.commit()
            print("Product Created Successfully")

        elif properties.content_type == "product_updated":
示例#12
0
import pika
from pika import channel

params = pika.URLParameters('amqps://*****:*****@beaver.rmq.cloudamqp.com/oljdtgfk')
print(params)
connection  = pika.BlockingConnection(params)

channel = connection.channel()

channel.queue_declare(queue='admin')

def callback(ch,method,properties,body):
    print('Received in admin')
    print(body)

channel.basic_consume(queue='admin',on_message_callback=callback)
 
print('Started')

channel.start_consuming()

channel.close()
示例#13
0
from pika import channel

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# queue
# channel.queue_declare(queue='hello')

# channel.basic_publish(exchange='', routing_key='hello', body='Hello World!..')

# channel.basic_publish(exchange='', routing_key='hello', body=message)

# print(" [x] Sent 'Hello World!...'")

# second part
channel.queue_declare(queue='task_queue', durable=True)

message = ' '.join(sys.argv[1:]) or 'Hello World!'

channel.basic_publish(
    exchange='',
    routing_key='task_queue',
    body=message,
    properties=pika.BasicProperties(
        delivery_mode=2,  # make message persiistant
    ))

print(" [x] Sent %r" % message)

connection.close()
示例#14
0
import pika
from pika import connection
from pika import channel

params = pika.URLParameters(
    "amqps://*****:*****@hornet.rmq.cloudamqp.com/lrtmtglx"
)
connection = pika.BlockingConnection(params)

channel = connection.channel()
channel.queue_declare(queue="admin")


def callback(ch, method, properties, body):
    print("Received in admin")
    id = json.loads(body)
    print(id)
    product = Product.objects.get(id=id)
    product.likes = product.likes + 1
    product.save()
    print("Product likes increased!")


channel.basic_consume(queue="admin",
                      on_message_callback=callback,
                      auto_ack=True)
print("Started consuming")
channel.start_consuming()
channel.close()
示例#15
0
#!/usr/bin/env python
import pika
from pika import connection
from pika import channel
from get_time import data_atual, tempo_atual

#pip install pika

url = 'amqps://*****:*****@fish.rmq.cloudamqp.com/kyiirjzl'
parametros = pika.URLParameters(url)
connection = pika.BlockingConnection(parametros)
channel = connection.channel()
channel.queue_declare(queue='Queue_Data')

messagem = "Data e Hora:\n" + data_atual + "\n" + tempo_atual

channel.basic_publish(exchange='', routing_key='Queue_Data', body=messagem)
print("Data Enviada!'")

connection.close()
示例#16
0
            # Create a StatsClientChannelEvents object to store a reference
            # to the channel that will need to be shutdown if a signal is caught
            channel_manager = StatsClientChannelHelper(channel)
            signal.signal(signal_num, channel_manager.stop_stats_client)
            signal_num = signal.SIGTERM
            signal.signal(signal_num, channel_manager.stop_stats_client)

        except ValueError, ve:
            print "Warning: Graceful shutdown may not be possible: Unsupported Signal: " + signal_num

        # Create a queue
        # --------------------
        # Creating an exclusive queue NOTE that will only exists
        # as long as the client is connected

        result = channel.queue_declare(exclusive=True)
        queue_name = result.method.queue
        print "An exclusive queue declared successfully, now binding the queue and the exchange with the given routing key(s)"

        # Bind you queue to the message exchange, and register your new message event handler
        for topic in topics:
            channel.queue_bind(exchange='pi_utilization', queue=queue_name, routing_key=topic)
        print "Binding of exchange with the declared queue successful, now start pika's event loop by calling channel.basic_consume"

        # Start pika's event loop
        channel.basic_consume(on_new_msg, queue=queue_name, no_ack=True)
        print "Pika's event loop started"

        channel.start_consuming()

    except pika.exceptions.ProbableAccessDeniedError, pade:
示例#17
0
            print "Warning: Graceful shutdown may not be possible: Unsupported " \
                  "Signal: " + signal_num

        # TODO: Create a queue
        # --------------------
        # It is up to you to determine what type of queue to create...
        # For example, if you create an exclusive queue, then the queue will
        # only exist as long as your client is connected
        # or you could create a queue that will continue to receive messages
        # even after your client app disconnects.
        #
        # In the short report, you should document what type of queue you create
        # and the series of events that occur at your client and the message broker
        # when your client connects or disconnects. (Slide 10).
        channel = msg_broker.channel()
        my_queue = channel.queue_declare(exclusive=True)

        # TODO: Bind your queue to the message exchange, and register your
        #       new message event handler
        channel.queue_bind(exchange="pi_utilization",
                           queue=my_queue.method.queue,
                           routing_key=topic)

        # TODO: Start pika's event loop
        channel.basic_consume(on_new_msg,
                              queue=my_queue.method.queue,
                              no_ack=True)
        print "Connecting to Server."
        channel.start_consuming()

    except pika.exceptions.AMQPError, ae:
示例#18
0
    def run(self, args, opts):
        config = ConfigParser()
        config.read("python/configs/config.ini")

        message_count = int(config.get("RABBIT", "message_count"))
        QUEUE_NAME = config.get("QUEUES", "business_pusher")

        channel = RabbitMQ.get_channel()
        # arg_parser = argparse.ArgumentParser()
        # arg_parser.add_argument('-o', '--offset', type=int, help='SQL select offset', default=0)
        # arg_parser.add_argument('-l', '--limit', type=int, help='SQL select limit', default=0)
        # args = arg_parser.parse_args()

        offset = 1
        if offset == 1:
            offset = 0
        else:
            offset = offset * 1000
        limit = 500
        logger = get_logger('['+QUEUE_NAME+']')
        while True:
            try:
                stats = channel.queue_declare(queue=QUEUE_NAME, durable=True)
                if stats.method.message_count < message_count:
                    links = self.get_links()
                    if links:
                        for link in links:
                            try:
                                channel.basic_publish(
                                    exchange='',
                                    routing_key=QUEUE_NAME,
                                    properties=BasicProperties(delivery_mode=2),
                                    body=JSONEncoder().encode({
                                        'id': link['id'],
                                        'url': link['url']
                                    })
                                )
                                DB.update_link_status(table="businesses",item={
                                    'id': link['id'],
                                    'parse_status': "1"
                                })
                            except AMQPError as connection_error:
                                time.sleep(10)
                                logger.error("AMQP error")
                                channel = RabbitMQ.get_channel()
                                channel.queue_declare(queue=QUEUE_NAME, durable=True)
                                time.sleep(3)
                        logger.info("Pushed chunk")
                    else:
                        logger.info('There are no links in the DB! Sleep for 15 seconds...')
                        time.sleep(15)
                else:
                    logger.debug(f'There are more then {message_count} messages in the Queue! Sleep for 10 seconds...')
                    time.sleep(10)
            except ChannelClosed as channel_closed_error:
                logger.error("Channel closed")
                logger.info("Sleep for 10 seconds...")
                time.sleep(10)
                channel = RabbitMQ.get_channel()
                channel.queue_declare(queue=QUEUE_NAME, durable=True)
            except Exception as error:
                logger.error(repr(error))
                DB.reconnect()
                logger.info("Sleep for 10 seconds...")
                time.sleep(10)