Skip to content

Instantly share code, notes, and snippets.

@santagada
Created August 2, 2016 09:54
Show Gist options
  • Save santagada/f8197bfdcad35350013a7bd2c71ce48d to your computer and use it in GitHub Desktop.
Save santagada/f8197bfdcad35350013a7bd2c71ce48d to your computer and use it in GitHub Desktop.
from collections import defaultdict
import nsq
import msgpack
from time import strftime
from colorama import init, Fore, Style
init()
def wraphand(topic):
def handler(message):
def dec(key):
return pkg[key.encode('utf8')].decode('utf8')
pkg = msgpack.unpackb(message.body)
body = msgpack.unpackb(pkg[b'Body'])
pkg.pop(b'Body')
d = defaultdict(bytes)
d.update(pkg)
pkg = d
v = '[{}][{}][{}] @{} A: {} V:{}'.format(
strftime("%H:%M:%S"), topic, dec('Type'), dec('Origin'), dec('AnswerChan'), dec('Version')
)
print(v)
toprint = []
if isinstance(body, dict):
for key in sorted(body):
try:
pkey = key.decode('utf8')
except:
pkey = key
if isinstance(body[key], bytes):
pvalue = body[key].decode('utf8')
else:
pvalue = body[key]
toprint.append('{}{}{}={}'.format(Fore.BLUE ,pkey, Style.RESET_ALL, pvalue))
print(' ' + ' '.join(toprint))
else:
print(body)
return True
return handler
def register(topic):
r = nsq.Reader(message_handler=wraphand(topic),
nsqd_tcp_addresses=['nsq.dev.blippar.com:4150'],
topic=topic, channel='nsqshark#ephemeral', lookupd_poll_interval=15)
import requests
url = "http://nsq.dev.blippar.com:4151/"
json_format = "?format=json"
def build_url(command):
return url + command + json_format
r = requests.get(build_url('stats'))
rj = r.json()
for topic in rj['data']['topics']:
if topic['topic_name'] != 'stats':
print('registering '+topic['topic_name'])
register(topic['topic_name'])
# register('api')
# register('assets')
# register('transcoder-worker')
# register('api.all')
# register('assets.all')
# register('transcoder-worker.all')
# register('blipp-info')
nsq.run()
{
b'Type': b'api_entitycan',
b'Origin': b'us-west.reporting.i-f1d13539#ephemeral',
b'AnswerChan': b'us-west.reporting.i-f1d13539-7c927d4dd97c4ee4',
b'IsAnswer': False,
b'IsDistributed': False,
b'NeedAnswer': True,
b'To': b'',
b'Version': b'0.3'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment