catch signals

This commit is contained in:
James Turk 2012-06-26 01:19:58 -04:00
parent d77062f851
commit 60aa0772e9

View File

@ -3,18 +3,30 @@ __version__ = '0.1.0'
import time import time
import datetime import datetime
import signal
import argparse import argparse
from pymongo import Connection from pymongo import Connection
from termcolor import colored from termcolor import colored
quit = False
def watch(dbname, refresh): def watch(dbname, refresh):
global quit
db = getattr(Connection('localhost'), dbname) db = getattr(Connection('localhost'), dbname)
db.set_profiling_level(2) db.set_profiling_level(2)
last_ts = datetime.datetime.utcnow() last_ts = datetime.datetime.utcnow()
exclude_name = '{0}.system.profile'.format(dbname) exclude_name = '{0}.system.profile'.format(dbname)
while True:
def ctrl_c(signal, frame):
global quit
print('returning profiling level to 0...')
db.set_profiling_level(0)
db.system.profile.drop()
quit = True
signal.signal(signal.SIGINT, ctrl_c)
while not quit:
for e in db.system.profile.find({'ns': {'$ne': exclude_name}, for e in db.system.profile.find({'ns': {'$ne': exclude_name},
'ts': {'$gt': last_ts}}): 'ts': {'$gt': last_ts}}):
output = [] output = []
@ -35,7 +47,7 @@ def watch(dbname, refresh):
else: else:
output.append(colored('unknown operation: {op}'.format(**e), output.append(colored('unknown operation: {op}'.format(**e),
'red')) 'red'))
print e print(e)
if 'nscanned' in e: if 'nscanned' in e:
output.append(colored('scanned {nscanned}'.format(**e), output.append(colored('scanned {nscanned}'.format(**e),
@ -48,12 +60,9 @@ def watch(dbname, refresh):
if e.get('scanAndOrder'): if e.get('scanAndOrder'):
output.append(colored('scanAndOrder', 'red')) output.append(colored('scanAndOrder', 'red'))
output.append(colored('{millis}ms'.format(**e), 'green')) output.append(colored('{millis}ms'.format(**e), 'green'))
print ' '.join(output) print(' '.join(output))
last_ts = e['ts'] last_ts = e['ts']
time.sleep(refresh) time.sleep(refresh)
db.set_profiling_level(0)
db.system.profile.drop()
def main(): def main():
parser = argparse.ArgumentParser(description='watch mongo queries') parser = argparse.ArgumentParser(description='watch mongo queries')