Ejemplo n.º 1
0
 def __init__(self, url: str, callback, name: str = ''):
     self.parameters = URLParameters(url)
     self.client = ClientCreator(reactor, TwistedProtocolConnection,
                                 self.parameters)
     self.name = name
     self.connection = None
     self.callback = callback
Ejemplo n.º 2
0
def main():
    connection = BlockingConnection(
        parameters=URLParameters("amqp://*****:*****@localhost:5672/%2F")
    )
    props = BasicProperties(
        content_type="application/json",
        content_encoding="utf8",
        delivery_mode=2,
    )
    ch = connection.channel()
    ch.exchange_declare(exchange="worker.mm", exchange_type="topic")
    ch.queue_declare(queue="splitter_action_request_q")
    ch.queue_bind(
        queue="splitter_action_request_q",
        exchange="worker.mm",
        routing_key="splitter.*.request",
    )
    ch.basic_publish(
        exchange="worker.mm",
        routing_key="splitter.split.request",
        properties=props,
        body=json.dumps(
            {
                "cache_dir": "/home/xy/misc/mtm/cache/youtube/好葉怎樣練習冥想完整教學 動畫講解-NLJcwbpkiJ0",
                "action.type": "split",
                "partial_duration": 2 * 60,
            }
        ),
    )
Ejemplo n.º 3
0
 def test_1(self):
     self.spec = AMQPConfigSpec(
         URLParameters("amqp://*****:*****@localhost:5672/%2f"),
         queues=[
             AMQPQueueDescriptor(self.queue,
                                 declare=True,
                                 exclusive=True,
                                 auto_delete=True)
         ],
         exchanges=[
             AMQPExchangeDescriptor(exchange=self.exchange,
                                    auto_delete=True,
                                    exchange_type="topic")
         ],
         bindings=[
             AMQPBindDescriptor(queue=self.queue,
                                exchange=self.exchange,
                                routing_key='dog.*')
         ],
         consumers=[
             AMQPConsumerDescriptor(self.queue, self._consume, no_ack=True)
         ])
     init = AMQPInitializer(self.spec, self.on_complete)
     init.run()
     assert self.complete
     assert self.body == self.BODY
Ejemplo n.º 4
0
def get_client(name: str):
    cl = client.get_client(name, URLParameters(CLOUD_AMQP))
    assert cl._thread
    if not cl._thread.is_alive():
        logging.debug('waiting for client thread to live')
        time.sleep(5)
        assert cl._thread.is_alive(), 'Client thread dead'
    return cl
Ejemplo n.º 5
0
def _get_channel():
    global _channel
    assert 'kompaq' in settings.INSTALLED_APPS

    if not _channel:
        connection = BlockingConnection(URLParameters(settings.KOMPAQ_URL))
        _channel = connection.channel()

    return _channel
Ejemplo n.º 6
0
 def check(self):
     import pika
     from pika.connection import URLParameters
     url = f'amqp://*****:*****@{self.host}:{self.get_port()}/%2F'
     try:
         connection = pika.BlockingConnection(URLParameters(url))
     except Exception:
         return False
     else:
         return connection.is_open
Ejemplo n.º 7
0
 def GetConnection(self):
     self.CancelOrContinueTask()
     try:
         self._connection_parameters = URLParameters(self.connection_url)
         self._connection = BlockingConnection(self._connection_parameters)
     except (ProbableAccessDeniedError, ProbableAuthenticationError):
         raise AmqpInvalidUrl(
             "Invalid credentials to the AMQP node and vhost.")
     except (IncompatibleProtocolError, ConnectionClosed, ValueError,
             IndexError, gaierror):
         raise AmqpInvalidUrl(
             "The connection URL to the AMQP node is invalid.")
     return self._connection
Ejemplo n.º 8
0
    def parse(cls, url):
        if not url.startswith("amqp:"):
            raise ValueError(url)
        obj = cls(URLParameters(url))

        if url[0:4] == 'amqp':
            url = 'http' + url[4:]
        parts = urlparse.urlparse(url)
        if parts.query:
            obj.extras.update(
                {k: v
                 for k, v in urlparse.parse_qsl(parts.query)})
        return obj
Ejemplo n.º 9
0
def main():
    connection = BlockingConnection(
        parameters=URLParameters("amqp://*****:*****@localhost:5672/%2F"))
    ch = connection.channel()
    ch.basic_publish(
        exchange="worker.mm",
        routing_key="request.upload",
        body=json.dumps(
            {
                "url": "https://www.youtube.com/watch?v=PJ1QwhNL72A",
                "username": "******",
            },
            ensure_ascii=True,
        ),
    )
Ejemplo n.º 10
0
def main():
    connection = BlockingConnection(
        parameters=URLParameters("amqp://*****:*****@localhost:5672/%2F"))
    props = BasicProperties(
        content_type="application/json",
        content_encoding="utf8",
        delivery_mode=2,
    )
    ch = connection.channel()
    ch.exchange_declare(exchange="worker.mm", exchange_type="topic")
    ch.queue_declare(queue="user_action_request_q")
    ch.queue_bind(
        queue="user_action_request_q",
        exchange="worker.mm",
        routing_key="user.*.request",
    )
    url_list = [
        # "https://www.youtube.com/watch?v=PJ1QwhNL72A",
        # "https://www.youtube.com/watch?v=b5K192_hilA",
        # "https://www.youtube.com/watch?v=L0skErRNc5Y",
        # "https://www.youtube.com/watch?v=G8GWtGZuHSk",
        # "https://www.youtube.com/watch?v=Vn4wxZlaFYc",
        # "https://www.youtube.com/watch?v=ObO6XEQSWak",
        # "https://www.youtube.com/watch?v=fPz2tZibAAQ",
        # "https://www.youtube.com/watch?v=v0ADJy2Menk",
    ]
    for url in url_list:
        ch.basic_publish(
            exchange="worker.mm",
            routing_key="user.download.request",
            properties=props,
            body=json.dumps({
                "url": url,
                "action.type": "download"
            }),
        )

    ch.queue_declare(queue="user_action_result_q")
    ch.queue_bind("user_action_result_q",
                  "worker.mm",
                  routing_key="user.*.result")
    ch.basic_consume(
        "user_action_result_q",
        on_message_callback=print_msg,
        consumer_tag="user",
    )
    ch.start_consuming()
Ejemplo n.º 11
0
def start_rabbitmq() -> dict:
    connection = BlockingConnection(URLParameters(rabbitmq_connection_string))
    channel: BlockingChannel = connection.channel()
    channel.basic_qos(prefetch_count=1)

    for queue in Queues:
        channel.queue_declare(queue.value, durable=True)

    rabbitmq = {
        'connection': connection,
        'channel': channel,
    }

    on_document_to_process_thread_handler_partial = partial(
        on_document_to_process_thread_handler, threads=threads)
    channel.basic_consume(
        queue=Queues.TO_PROCESS.value,
        on_message_callback=on_document_to_process_thread_handler_partial)

    return rabbitmq
Ejemplo n.º 12
0
from pika import BlockingConnection
from pika.connection import URLParameters
import pickle


class Msg:
    def __init__(self, content):
        self._content = content


connection = BlockingConnection(
    parameters=URLParameters("amqp://*****:*****@localhost:5672/%2F"))
ch = connection.channel()
# ch.exchange_declare(exchange="tenant.chain.action.tx", exchange_type="topic")
# ch.exchange_declare(
#     exchange="tenant.chain.action.address", exchange_type="topic"
# )
# ch.exchange_declare(
#     exchange="tenant.chain.commodity.action.chaintask", exchange_type="topic"
# )
ch.basic_publish(
    exchange="tenant.chain.action.chain_task",
    routing_key="EGamer.eth.txcheck",
    body=pickle.dumps(Msg("Hello world!")),
)
Ejemplo n.º 13
0
    stdOut = pop.stdout.read().decode()
    stdErr = pop.stderr.read().decode()
    for line in stdOut.split('\n'):
        if line.startswith(QUEUE_NAME):
            COUNT = int(line.split()[1])
            break
    print(("Total number of messages in %s: %d" % (QUEUE_NAME, COUNT)))
    if COUNT == 0:
        sys.exit()

    # Connect to RabbitMQ
    match = DEF_HOST.search(app.conf['BROKER_URL'])
    broker_url = "{}%2F".format(
        match.group(1)) if match else app.conf['BROKER_URL']
    print("broker_url: {}".format(broker_url))
    CONNECTION = BlockingConnection(URLParameters(broker_url))

    # Open the channel
    channel = CONNECTION.channel()

    # Declare the queue
    channel.queue_declare(queue=QUEUE_NAME,
                          durable=True,
                          exclusive=False,
                          auto_delete=False,
                          arguments={'x-max-priority': 10})

    # Add a queue to consume
    channel.basic_consume(handle_delivery, queue=QUEUE_NAME)

    # Start consuming, block until keyboard interrupt
Ejemplo n.º 14
0
 def get_parameters(self):
     return URLParameters(self.unparse())