Created
June 12, 2015 01:03
-
-
Save jehiah/44b717b8a09e984ac470 to your computer and use it in GitHub Desktop.
A pynsq Reader that polls for a file to transparently enable/disable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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