Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created June 12, 2015 01:03
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/44b717b8a09e984ac470 to your computer and use it in GitHub Desktop.
Save jehiah/44b717b8a09e984ac470 to your computer and use it in GitHub Desktop.
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