Created
May 19, 2015 09:07
-
-
Save jboynyc/4d67041bf46634befbd0 to your computer and use it in GitHub Desktop.
Wrap Beanstalkc to create unique (deduplicated) queues.
This file contains hidden or 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 beanstalkc | |
from redis import Redis | |
class UniqueQueue(object): | |
'''Stores a persistent set of added jobs in Redis to keep jobs from being | |
added to the Beanstalkd queue more than once.''' | |
def __init__(self, tube_name='default', host='localhost'): | |
self.beanstalk = beanstalkc.Connection(host=host) | |
self.beanstalk.use(tube_name) | |
self.tube = self.beanstalk.using() | |
self.redis = Redis(host) | |
self.setname = '-'.join(['rhrhDrhdvaH', self.tube]) | |
def put(self, job, delay=0): | |
assert isinstance(job, str) | |
if not self.has_item(job): | |
jid = self.beanstalk.put(job, delay=delay) | |
self.redis.sadd(self.setname, job) | |
return jid | |
else: | |
return False | |
def has_item(self, job): | |
return self.redis.sismember(self.setname, job) | |
def __contains__(self, job): | |
return self.has_item(job) | |
def __repr__(self): | |
return "UniqueQueue({})".format(self.tube) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment