示例#1
0
def createExchange(name='', type='', retrandq=False):
    """
	name:     name of exchange. if not assigned, 'default' will be returned
	type:     type of the exchange. valid values are 'direct', 'topic', 'headers', 'fanout'
	          default is 'fanout'
	retrandq: if True, a randomq name will be returned else not returned. Expecting
	          users to send mesages to exchanges instead of queues
	"""
    logger = createExchange.get_logger()
    if name == '':
        name = 'default'

    if type not in ['direct', 'topic', 'headers', 'fanout']:
        type = 'fanout'
    logger.info("%s , %s" % (name, type))
    connection = pika.AsyncoreConnection(
        pika.ConnectionParameters(host=celeryconfig.BROKER_HOST))
    channel = connection.channel()
    channel.exchange_declare(exchange=str(name), type=str(type))

    qname = None
    if retrandq:
        result = channel.queue_declare(exclusive=True)
        qname = result.queue
        logger.info("About to bind %s to %s" % (name, qname))
        channel.queue_bind(exchange=str(name), queue=str(qname))

    connection.close()

    # Return the data in JSON format
    from StringIO import StringIO
    io = StringIO()
    json.dump({'xchg': name, 'type': type, 'qname': qname}, io, sort_keys=True)
    return io.getvalue()
示例#2
0
def sendMsg(msg, xchg='', qname=''):
    """
	msg:	Message you want to send
	xchg:	Name of the exchange you want to send it to
	qname:	Name of the queue you want to send it to

	Note:	The task does not validate that either 'xchg' or 'qname'
			is valid in the system
	"""
    logger = sendMsg.get_logger()
    if qname == '':
        qname = 'default'
    connection = pika.AsyncoreConnection(
        pika.ConnectionParameters(celeryconfig.BROKER_HOST))
    channel = connection.channel()
    # sends to the default exchange
    channel.queue_declare(queue=qname)
    channel.basic_publish(exchange=xchg, routing_key=qname, body=msg)
    logger.info("[SENT] msg: %s" % msg)
    connection.close()

    # Return the data in JSON format
    from StringIO import StringIO
    io = StringIO()
    json.dump({'msg': msg}, io, sort_keys=True)
    return io.getvalue()
示例#3
0
    def __init__(self):
        self.connection = pika.AsyncoreConnection(
            pika.ConnectionParameters(host='localhost'))

        self.channel = self.connection.channel()

        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.queue

        self.requests = {}
        self.channel.basic_consume(self.on_response,
                                   no_ack=True,
                                   queue=self.callback_queue)
示例#4
0
    def monitor(self, qname, callback):
        conn = pika.AsyncoreConnection(pika.ConnectionParameters(
                '127.0.0.1',
                credentials=pika.PlainCredentials('guest', 'guest')))

        ch = conn.channel()

        if not self.queue_exists:
            ch.queue_declare(queue=qname, durable=False, exclusive=False, auto_delete=False)
            ch.queue_bind(queue=qname, exchange=self.exchange_name)
            print "Binding queue %s to exchange %s" % (qname, self.exchange_name)
            #ch.queue_bind(queue=qname, exchange=self.exchange_name, routing_key=qname)
            self.queue_exists = True

        ch.basic_consume(callback, queue=qname)

        pika.asyncore_loop()
        print 'Close reason:', conn.connection_close
示例#5
0
def recvMsg(msg, qname=''):
    logger = recvMsg.get_logger()
    if qname == '':
        qname = 'default'
    connection = pika.AsyncoreConnection(
        pika.ConnectionParameters(celeryconfig.BROKER_HOST))
    channel = connection.channel()
    # sends to the default exchange
    channel.queue_declare(queue=qname)
    channel.basic_consume(callback, queue=qname)
    connection.close()
    logger.info("[x] conn close. %s" % callback.body)

    # Return the data in JSON format
    from StringIO import StringIO
    io = StringIO()
    json.dump({'msg': callback.body}, io, sort_keys=True)
    return io.getvalue()
示例#6
0
    def publish(self, message, routing_key):
        conn = pika.AsyncoreConnection(pika.ConnectionParameters(
                '127.0.0.1',
                credentials=pika.PlainCredentials('guest', 'guest')))

        ch = conn.channel()

        ch.exchange_declare(exchange=self.exchange_name, type="fanout", durable=False, auto_delete=False)

        ch.basic_publish(exchange=self.exchange_name,
                         routing_key=routing_key,
                         body=message,
                         properties=pika.BasicProperties(
                                content_type = "text/plain",
                                delivery_mode = 2, # persistent
                                ),
                         block_on_flow_control = True)
        ch.close()
        conn.close()
示例#7
0
def stress_amqp(amqp_url):
    credentials = pika_credentials_from_url(amqp_url)
    conn = pika.AsyncoreConnection(credentials)

    ch = conn.channel()
    queue_setup(ch, QUEUES)
    ch.close()

    threading.Timer(DELAY, print_stats, [QUEUES]).start()
    print " [*] Starting loop"
    queue_consume(conn, QUEUES)
    try:
        asyncore.loop()
        # get some cpu at least every few seconds
    except KeyboardInterrupt:
        pass
    print " [*] Quitting!"
    sys.exit(0)
    conn.close()
示例#8
0
def createNameQ(name=''):
    """
	Creates a named queue for the default exchange
	name:	if name is empty, then the default queue is created and returned
			'default'.
	"""
    logger = createQ.get_logger()
    if name == '':
        name = 'default'
    connection = pika.AsyncoreConnection(
        pika.ConnectionParameters(celeryconfig.BROKER_HOST))
    channel = connection.channel()
    # sends to the default exchange
    channel.queue_declare(queue=name)
    logger.info("[CREATE_Q] msg: %s" % name)
    connection.close()
    # Return the data in JSON format
    from StringIO import StringIO
    io = StringIO()
    json.dump({'qname': name}, io, sort_keys=True)
    return io.getvalue()
示例#9
0
    def setQueueName(self, incomingQueueName, incomingPreFetchCount=1):

        self.connection = pika.AsyncoreConnection(
            pika.ConnectionParameters(host=self.getSelectedHost()))
        self.channel = self.connection.channel()

        # declare a queue named encodejobs in case it hasn't already
        # been declared.  The server will places messages here.
        self.queueName = incomingQueueName
        self.channel.queue_declare(queue=self.queueName)

        # prefetch_count=1 causes this client to just get 1
        # message from the queue.  By doing this any new client
        # that connects to the message bus will be able to get
        # any messages left in the queue instead of only new ones
        # that were added after an existing client connected.
        # This is important so that you can scale up your encode
        # cluster on the fly
        self.channel.basic_qos(prefetch_count=incomingPreFetchCount)
        self.channel.basic_consume("__messageReceivedCallBack",
                                   queue=self.queueName)
示例#10
0
def youtube(request, id):
    try:

        track = Track.objects.select_related('image',
                                             'artist').filter(id=id)[0]
        video_id = get_video_id(track)
        connection = pika.AsyncoreConnection(\
                pika.ConnectionParameters(host=settings.BROKER_HOST,
                        credentials=pika.PlainCredentials(settings.BROKER_USER,
                            settings.BROKER_PASSWORD)))
        channel = connection.channel()
        channel.queue_declare(queue='testq2',
                              auto_delete=True,
                              durable=False,
                              exclusive=False)
        track_data = json.dumps({
            'status':
            'ok',
            'videoId':
            video_id,
            'image':
            get_image(track),
            'displayStr':
            track.name + ' - ' + track.artist.name,
            'id':
            track.id
        })
        channel.basic_publish(
            exchange='',
            routing_key='testq2',
            body=track_data,
            properties=pika.BasicProperties(content_type='application/json'),
        )
        connection.close()
        return HttpResponse(track_data)

    except Exception, e:
        print e
        return HttpResponse(json.dumps({'status': 'error'}))
示例#11
0
    def run(self):
        settings.logger.info("RUN")
        # Connect to rabbitmq server
        self.connection = pika.AsyncoreConnection(
            pika.ConnectionParameters(settings.host, settings.port,
                                      settings.virtual_host))
        settings.logger.debug(
            "Build a connection to rabbitmq by %s, %s, %s" %
            (settings.host, settings.port, settings.virtual_host))

        self.channel = self.connection.channel()

        settings.logger.debug("get a channel")

        # Create exchange if not exist
        self.channel.exchange_declare(exchange=settings.exchange_name,
                                      type=settings.routing_type,
                                      durable=settings.durable)
        settings.logger.debug("Exchange %s declared", settings.exchange_name)
        # Create queue if not exist
        self.channel.queue_declare(queue=settings.queue_name,
                                   durable=settings.durable,
                                   auto_delete=settings.auto_delete)
        settings.logger.debug("Queue %s declared." % settings.queue_name)
        # Binding exchange and queue with routing key
        self.channel.queue_bind(exchange=settings.exchange_name,
                                queue=settings.queue_name,
                                routing_key=settings.routing_key)
        settings.logger.debug(
            "Binding queue %s to exchagge %s with routing key %s" %
            (settings.queue_name, settings.exchange_name,
             settings.routing_key))

        self.channel.basic_qos(prefetch_count=settings.prefetch_count)
        self.channel.basic_consume(self.callback,
                                   queue=settings.queue_name,
                                   no_ack=settings.no_ack)
        pika.asyncore_loop()
示例#12
0
    def publish(self, message, routing_key):
      try: 
        conn = pika.AsyncoreConnection(pika.ConnectionParameters(
                '127.0.0.1',
                credentials=pika.PlainCredentials('guest', 'guest')))

        ch = conn.channel()

        ch.exchange_declare(exchange=self.exchange_name, type="fanout", durable=False, auto_delete=False)

        ch.basic_publish(exchange=self.exchange_name,
                         routing_key=routing_key,
                         body=message,
                         properties=pika.BasicProperties(
                                content_type = "text/plain",
                                content_encoding = "utf8",
                                delivery_mode = 2, # persistent
                                ),
                         block_on_flow_control = True)
        ch.close()
        conn.close()
      except:
        logging.error("Could not send message to %s.%s because:%s" % (self.exchange_name, routing_key, traceback.format_exc()))
示例#13
0
#!/usr/bin/env python
import pika
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.CRITICAL)
connection = pika.AsyncoreConnection(
    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()
示例#14
0
        dict['picture'] = arg
    elif opt in ('-s', '--song'):
        dict['song'] = arg
    elif opt in ('-S', '--show'):
        dict['show'] = arg
    elif opt in ('-t', '--track'):
        dict['track'] = arg
    elif opt in ('-T', '--tracks'):
        dict['tracks'] = arg
    elif opt in ('-w', '--writer'):
        dict['writer'] = arg
    elif opt in ('-y', '--year'):
        dict['year'] = arg
    elif opt in ('-R', '--albumartist'):
        dict['albumarist'] = arg
    elif opt in ('-r', '--remove'):
        dict['remove'] = arg

basename, extension = os.path.splitext(dict['sourcefile'])
dict['outputfile'] = basename + '.m4v'

message = json.dumps(dict)

connection = pika.AsyncoreConnection(
    pika.ConnectionParameters(RabbitMQServer.getSelectedHost()))
channel = connection.channel()
channel.queue_declare(queue='encodejobs')
channel.basic_publish(exchange='', routing_key='encodejobs', body=message)
print " [x] Enqueued '" + dict['sourcefile'] + " for encoding'"
connection.close()
示例#15
0
        waitAndSee()
        continue

    RabbitMQServer = findAMQHost()
    if not RabbitMQServer:
        waitAndSee()
        continue

    try:
        growl = initGrowlNotifier()
    except:
        logger.debug(' [*] growl must be disabled, because we got here')

    logger.info(' [*] connecting to RabbitMQ @%s' % (RabbitMQServer, ))
    try:
        connection = pika.AsyncoreConnection(
            pika.ConnectionParameters(host=RabbitMQServer))
        channel = connection.channel()
        logger.info(' [*] connected')
    except:
        logger.critical(' [*] failed to connect to RabbitMQ!')
        logger.critical(' [*] Please check RabbitMQ host and configuration')
        waitAndSee()
        continue

    logger.debug(' [*] entering pika.asyncore_loop')
    logger.info(' [*] Waiting for encode jobs. Issue kill to %i to end' %
                (getpid(), ))

    declareAMQPQueue()
    gNotify(
        growl,
示例#16
0
      msg.direct("hi");
      msg.topic("x.y." + msg.routing_key);
      msg.properties.user_id = "THISISTHEUSER";
      msg.body = counter + "\n" + msg.body;
      counter++;
    }
"""

(po, pi) = os.popen2("gpg -u %s -a --detach-sign" % (signingkey,))
po.write(definition)
po.close()
signature = pi.read()
pi.close()

conn = pika.AsyncoreConnection(pika.ConnectionParameters(
    '127.0.0.1',
    credentials=pika.PlainCredentials('guest', 'guest')))

ch = conn.channel()

# try:
#     print ch.exchange_delete(exchange='x-script')
# except pika.exceptions.ChannelClosed:
#     ch = conn.channel()

print ch.exchange_declare(exchange='x-script', type='x-script', arguments={
    "type": "text/javascript",
    "definition": definition,
    "signature": signature
    })