#!/usr/bin/env python
import pika
import subprocess
import json
import os
from pymongo import MongoClient


dbcon = MongoClient()

NETSNIFF_UTIL = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'tools', 'netsniff.js')

queuecon = pika.BlockingConnection(pika.ConnectionParameters(
               'localhost'))
channel = queuecon.channel()
channel.exchange_declare(exchange='perfmonitor', type='direct')

result = channel.queue_declare('perf')
queue_name = result.method.queue

channel.queue_bind(exchange='perfmonitor', queue=queue_name, routing_key='perftest')

print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)
    content = json.loads(body)
     
    try:
        print ' [x] Executing command phantomjs', content['url']
        harcontent = subprocess.check_output(['phantomjs', NETSNIFF_UTIL, content['url'], content['agent']])
        jscontent = json.loads(harcontent)
        jscontent['site'] = content['site']
        jscontent['agent'] = content['agent']
        dbcon.perfmonitor.har.insert(jscontent)
    except:
        print ' [x] Unable to parse JSON, ignoring request'

    ch.basic_ack(delivery_tag = method.delivery_tag)
    try:
        if content['nb'] > 1:
            content['nb'] -= 1
            channel.basic_publish(exchange='perfmonitor',
                      routing_key='perftest',
                      body=json.dumps(content))
            print ' [x] Message sent back to queue'
    except:
        print ' [x] Error while trying to send message back to queue'
    print " [x] Done"

channel.basic_consume(callback,
                      queue=queue_name)

channel.start_consuming()

@@ -632,137 +632,385 @@
 def 
-callback(ch, method, properties, body):%0A    print %22 %5Bx%5D Received %25r%22 %25 (body,)%0A    content = json.loads(body)%0A     %0A    try:%0A    
+sendback(msg):%0A    channel.basic_publish(exchange='perfmonitor',%0A          routing_key='perftest',%0A          body=json.dumps(msg))%0A%0Adef callback(ch, method, properties, body):%0A    print %22 %5Bx%5D Received %25r%22 %25 (body,)%0A    content = json.loads(body)%0A     %0A    content%5B'nb'%5D -= 1%0A%0A    if content%5B'nb'%5D %3E 0:%0A        sendback(content)%0A        print ' %5Bx%5D Message sent back to queue'%0A%0A
     
@@ -1066,20 +1066,17 @@
 %5B'url'%5D%0A
-    
+%0A
     harc
@@ -1176,39 +1176,16 @@
 %5D%5D)%0A
-        jscontent = json.loads(
+%0A    if 
 harc
@@ -1186,25 +1186,25 @@
 f harcontent
-)
+:
 %0A        jsc
@@ -1204,129 +1204,51 @@
     
-jscontent%5B'site'%5D = content%5B'site'%5D%0A        jscontent%5B'agent'%5D = content%5B'agent'%5D%0A        dbcon.perfmonitor.har.insert(js
+try:%0A            jscontent = json.loads(har
 cont
@@ -1248,24 +1248,28 @@
 harcontent)%0A
+    
     except:%0A
@@ -1268,32 +1268,36 @@
 except:%0A        
+    
 print ' %5Bx%5D Unab
@@ -1316,90 +1316,46 @@
 JSON
-, ignoring request'%0A%0A    ch.basic_ack(delivery_tag = method.delivery_tag)%0A    try:
+ output'%0A            jscontent = None%0A
 %0A   
@@ -1366,25 +1366,17 @@
  if 
+js
 content
-%5B'nb'%5D %3E 1
 :%0A  
@@ -1381,24 +1381,26 @@
             
+js
 content%5B'nb'
@@ -1400,75 +1400,83 @@
 nt%5B'
-nb
+site
 '%5D 
--
 = 
-1%0A            channel.basic_publish(exchange='perfmonitor',
+content%5B'site'%5D%0A            jscontent%5B'agent'%5D = content%5B'agent'%5D%0A
 %0A   
@@ -1472,32 +1472,41 @@
 gent'%5D%0A%0A        
+    try:%0A
               ro
@@ -1507,79 +1507,117 @@
     
-routing_key='perftest',%0A                      body=json.dumps(content))
+  dbcon.perfmonitor.har.insert(jscontent)%0A                print ' %5Bx%5D HAR response saved'%0A            except:
 %0A   
@@ -1617,32 +1617,36 @@
 pt:%0A            
+    
 print ' %5Bx%5D Mess
@@ -1645,138 +1645,223 @@
 %5Bx%5D 
-Message sent back to queue'%0A    except:%0A        print ' %5Bx%5D Error while trying to send message back to queue'%0A    print %22 %5Bx%5D Done
+Unable to save HAR response, sending one request to queue'%0A                content%5B'nb'%5D = 1%0A                sendback(content)%0A%0A    ch.basic_ack(delivery_tag = method.delivery_tag)%0A    print %22 %5Bx%5D Acknoledgment sent
 %22%0A%0Ac

Make receive processing able to work with multiple processes