Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created February 25, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehiah/237577c9a5843861a668 to your computer and use it in GitHub Desktop.
Save jehiah/237577c9a5843861a668 to your computer and use it in GitHub Desktop.
clear_nsqd_topics_and_channels.py
#!/bin/env python2.7
"""
A script to clear out nsq topics and channels. This is particularly useful in dev for when
topics naturally backup, or when launching a producer with some time before a consumer exists
"""
import tornado.options
import tornado.httpclient
import logging
import json
import urllib
def clear(topic, channel=None):
logging.info('clearing %s %s', topic, channel)
if channel:
endpoint = "/empty_channel?" + urllib.urlencode(dict(topic=topic, channel=channel))
else:
endpoint = "/empty_topic?" + urllib.urlencode(dict(topic=topic))
client = tornado.httpclient.HTTPClient()
client.fetch('http://127.0.0.1:4151' + endpoint)
def run():
min_depth = tornado.options.options.min_depth
client = tornado.httpclient.HTTPClient()
data = json.loads(client.fetch('http://127.0.0.1:4151/stats?format=json').body)
for topic in data['data']['topics']:
if tornado.options.options.topic and topic['topic_name'] not in tornado.options.options.topic:
continue
if topic['depth'] > min_depth:
clear(topic['topic_name'])
for channel in topic['channels']:
if channel['depth'] > min_depth:
clear(topic['topic_name'], channel['channel_name'])
if __name__ == "__main__":
tornado.options.define("min_depth", type=int, default=100, help="the depth after which things are zero'd")
tornado.options.define("topic", type=str, multiple=True)
tornado.options.parse_command_line()
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment