sendmail(EMAIL_RECIPS, "CRITICAL ALERT", message) print "Sent alert via e-mail! Alert Text: %s " + "Recipients: %s" % ( str(message), str(EMAIL_RECIPS)) channel.basic_ack(delivery_tag=method.delivery_tag) def rate_limit_notify(channel, method, header, body): message = json.loads(body) send_mail(EMAIL_RECIPS, "RATE LIMIT ALERT!", message) print "Sent alert via e-mail! Alert Text: %s " + "Recipients: %s" % ( str(message), str(EMAIL_RECIPS)) channel.basic_ack(delivery_tag=method.delivery_tag) if __name__ == "__main__": amqp = AMQP(USERNAME, PASSWORD, HOST, PORT, PATH) # amqp.connect() amqp.receive(exchange=EXCHANGE_NAME, queue=QUEUE_CRITICAL, _type=TOPIC, routing_key=ROUTING_CRITICAL, binding=True) amqp.receive(exchange=EXCHANGE_NAME, queue=QUEUE_RATE, _type=TOPIC, routing_key=ROUTING_RATE, binding=True) amqp.basic_consume(callback=critical_notify, queue=QUEUE_CRITICAL, consumer_tag="critical") amqp.basic_consume(callback=rate_limit_notify,
'--message', dest='message', help="message to send to rabbit server") opt_parse.add_option('-b', '--host', dest='hostname', help="message to send to rabbit server") opt_parse.add_option('-p', '--port', dest='port', help="port server") opt_parse.add_option('-u', '--user', dest='username', help="username") opt_parse.add_option('-P', '--pass', dest='pasword', help="pasword") params = opt_parse.parse_args()[0] if params.hostname == None and len(sys.argv) > 1: params = urlparse(sys.argv[1]) try: USERNAME = params.username if params.username != None else USERNAME PASSWORD = params.pasword if params.password != None else PASSWORD except: pass AMQP_HOST = params.hostname AMQP_PORT = int(params.port) try: amqp = AMQP(USERNAME, PASSWORD, AMQP_HOST, AMQP_PORT, PATH) amqp.connect() except Exception, e: print "CRITICAL: Could not connect to %s:%s!" % (AMQP_HOST, AMQP_PORT) exit(EXIT_CRITICAL) print "OK: Connect to %s:%s successful." % (AMQP_HOST, AMQP_PORT) exit(EXIT_OK)
# -*- coding: utf-8 -*- from rb import AMQP from config import * import json def ping(channel, method, header, body): channel.basic_ack(delivery_tag=method.delivery_tag) msg = json.loads(body) print 'received ping ' + header.reply_to channel.basic_publish(body="Pong " + str(msg['time']), exchange="", routing_key=header.reply_to) if __name__ == "__main__": amqp = AMQP(USERNAME, PASSWORD, HOST, PORT, PATH) amqp.connect() amqp.receive(exchange=EXCHANGE_NAME, queue=QUEUE, _type=TYPE, binding=True, routing_key="ping") amqp.basic_consume(callback=ping, queue=QUEUE, consumer_tag=CONSUMER_TAG) print "Server has started, waiting for RPC calls..." amqp.startConsumer()
opt_parse.add_option('-p', '--port', dest='port', help="port server") opt_parse.add_option('-u', '--user', dest='username', help="username") opt_parse.add_option('-P', '--pass', dest='pasword', help="pasword") params = opt_parse.parse_args()[0] if params.hostname == None and len(sys.argv) > 1: params = urlparse(sys.argv[1]) try: USERNAME = params.username if params.username != None else USERNAME PASSWORD = params.pasword if params.password != None else PASSWORD except: pass AMQP_HOST = params.hostname AMQP_PORT = int(params.port) while True: try: amqp = AMQP(USERNAME, PASSWORD, AMQP_HOST, AMQP_PORT, PATH) amqp.connect() amqp.receive(exchange=EXCHANGE_NAME, queue=QUEUE, _type=TYPE, binding=True, routing_key="cluster") amqp.basic_consume(callback=msg_rcvd, queue=QUEUE, no_ack=False, consumer_tag=CONSUMER_TAG) amqp.startConsumer() except Exception, e: import traceback traceback.print_exc() time.sleep(5)
def add_points_to_user(channel, method, header, body): (check, result) = callback(channel, method, header, body) if check: print 'send a point to user: %s' % result['user_id'] def resize_picture(channel, method, header, body): (check, result) = callback(channel, method, header, body) if check: print 'resize image %s at %s' % (result['image_id'], result['image_path']) if __name__ == "__main__": amqp = AMQP(USERNAME, PASSWORD, HOST, PORT, PATH) amqp.connect() amqp.receive(exchange=EXCHANGE_NAME, queue=QUEUE, _type=TOPIC, binding=True) amqp.receive(exchange=EXCHANGE_NAME, queue=QUEUE_RESIZE, _type=TOPIC, binding=True) amqp.basic_consume(callback=add_points_to_user, queue=QUEUE, consumer_tag=CONSUMER_TAG) amqp.basic_consume(callback=resize_picture, queue=QUEUE_RESIZE,
import sys, json import time, traceback from config import * from rb import AMQP def msg_rcvd(channel, method, header, body): message = json.loads(body) print "Received order %(ordernum)d for %(type)s." % message channel.basic_ack(delivery_tag=method.delivery_tag) if __name__ == "__main__": amqp = AMQP(USERNAME,PASSWORD,"Erlang",5676,PATH) amqp.connect() amqp.receive(exchange = EXCHANGE_NAME, queue = QUEUE_WAREHOUSE, _type = TYPE, binding = True, routing_key = ROUTING) amqp.basic_consume(callback = msg_rcvd, queue = QUEUE_WAREHOUSE, no_ack = False, consumer_tag = CONSUMER_TAG) amqp.startConsumer()
from optparse import OptionParser if __name__ == "__main__": opt_parse = OptionParser() opt_parse.add_option('-m', '--message', dest = 'message', help = "message to send to rabbit server") opt_parse.add_option('-b', '--host', dest = 'hostname', help = "message to send to rabbit server") opt_parse.add_option('-p', '--port', dest = 'port', help = "port server") opt_parse.add_option('-u', '--user', dest = 'username', help = "username") opt_parse.add_option('-P', '--pass', dest = 'pasword', help = "pasword") params = opt_parse.parse_args()[0] if params.hostname == None and len(sys.argv) > 1: params = urlparse(sys.argv[1]) try: USERNAME = params.username if params.username != None else USERNAME PASSWORD = params.pasword if params.password != None else PASSWORD except: pass AMQP_HOST = params.hostname AMQP_PORT = int(params.port) msg = json.dumps({"content": "Cluster Sending test", "time" : time.time()}) amqp = AMQP(USERNAME,PASSWORD,AMQP_HOST,AMQP_PORT,PATH) amqp.connect() amqp.send(message = msg, exchange = EXCHANGE_NAME, content_type = "application/json", routing_key = "cluster")
from rb import AMQP from config import * import json from optparse import OptionParser if __name__ == '__main__': opt_parse = OptionParser() opt_parse.add_option('-r', '--routing-key', dest='routing_key', help="Routing key for message (ex: myalert.im)") opt_parse.add_option('-m', '--message', dest='message', help="Message text") args = opt_parse.parse_args()[0] amqp = AMQP(USERNAME, PASSWORD, HOST, PORT, PATH) amqp.send(exchange=EXCHANGE_NAME, message=json.dumps(args.message), _type=TOPIC, content_type="application/json", routing_key=json.dumps(args.routing_key))
from rb import AMQP import json from optparse import OptionParser if __name__ == "__main__": opt_parse = OptionParser() # opt_parse.add_option('-r', '--routing-key', dest = 'routing_key', # help = "Routing key for message (ex: myalert.im)") # opt_parse.add_option('-m', '--message', dest = 'message', # help = "Message text") opt_parse.add_option('-i', '--image-id', dest='image_id', help="Image id") opt_parse.add_option('-u', '--user-id', dest='user_id', help="User id") opt_parse.add_option('-p', '--image-path', dest='image_path', help="Image path") args = opt_parse.parse_args()[0] message = json.dumps({ 'image_id': args.image_id, 'user_id': args.user_id, 'image_path': args.image_path }) amqp = AMQP(USERNAME, PASSWORD, HOST, PORT, PATH) amqp.connect() amqp.send(exchange=EXCHANGE_NAME, _type=TOPIC, content_type="application/json", message=message, delivery_mode=2) amqp.close()
# -*- coding: utf-8 -*- from rb import AMQP from config import * import time import json import pika def reply(channel, method, header, body): print 'Receive rpc server replies' channel.close() if __name__ == "__main__": amqp = AMQP(USERNAME, PASSWORD, HOST, PORT, PATH) amqp.connect() result = amqp.connect().queue_declare(exclusive=True, auto_delete=True) msg = json.dumps({"client_name": "RPC client", "time": time.time()}) msg_props = pika.BasicProperties() msg_props.reply_to = result.method.queue amqp.connect().basic_publish(exchange=EXCHANGE_NAME, body=msg, properties=msg_props, routing_key="ping") print "Sent 'ping' RPC call. Waiting for reply..." + result.method.queue amqp.basic_consume(callback=reply, queue=result.method.queue, consumer_tag=result.method.queue) amqp.startConsumer()