Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A pynsq Reader that polls for a file to transparently enable/disable
import os.path
import logging
import tornado.ioloop
from nsq import Reader as BaseReader
from nsq import run
class Reader(BaseReader):
def __init__(self, *args, **kwargs):
max_in_flight = kwargs['max_in_flight']
kwargs['max_in_flight'] = 0
def check_disabled():
if os.path.exists("queuereaders/disable"):
if self.max_in_flight == max_in_flight:
logging.info('[%s] disabling. setting max_in_flight = 0', self.name)
self.set_max_in_flight(0)
else:
if self.max_in_flight == 0:
logging.info('[%s] enabling. setting max_in_flight = %d', self.name, max_in_flight)
self.set_max_in_flight(max_in_flight)
# poll every second for the disabled file, and adjust max_in_flight as appropriate
self.disabled_watcher = tornado.ioloop.PeriodicCallback(check_disabled, 1000)
super(Reader, self).__init__(*args, **kwargs)
self.disabled_watcher.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.