示例#1
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)
示例#2
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 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)
示例#5
0
import pika, sys
from pika import channel

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

channel.exchange_declare(exchange='logs', exchange_type='fanout')
message = ' '.join(sys.argv[1:]) or 'Hello World!'

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

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

connection.close()
示例#6
0
def publish(method, body):
    properties = pika.BasicProperties(method)
    channel.basic_publish(exchange="",
                          routing_key="main",
                          body=json.dumps(body),
                          properties=properties)
示例#7
0
def publish():
    channel.basic_publish(exchange='', routing_key='admin', body='hello')
示例#8
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()
示例#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
    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)
示例#11
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()