Skip to content

Instantly share code, notes, and snippets.

@jboynyc
Created May 19, 2015 09:07
Show Gist options
  • Save jboynyc/4d67041bf46634befbd0 to your computer and use it in GitHub Desktop.
Save jboynyc/4d67041bf46634befbd0 to your computer and use it in GitHub Desktop.
Wrap Beanstalkc to create unique (deduplicated) queues.
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