diff --git a/README.rst b/README.rst index 7cd6c11..2a283fb 100644 --- a/README.rst +++ b/README.rst @@ -86,7 +86,8 @@ This is how you do it:: args=[arg1, arg2], # Arguments passed into function when executed kwargs={'foo': 'bar'}, # Keyword arguments passed into function when executed interval=60, # Time before the function is called again, in seconds - repeat=10 # Repeat this number of times (None means repeat forever) + repeat=10, # Repeat this number of times (None means repeat forever) + uniq=False # If True - do not queue job if it is already present in queue ) **IMPORTANT NOTE**: If you set up a repeated job, you must make sure that you diff --git a/rq_scheduler/scheduler.py b/rq_scheduler/scheduler.py index 139eced..df345bc 100644 --- a/rq_scheduler/scheduler.py +++ b/rq_scheduler/scheduler.py @@ -124,7 +124,7 @@ def enqueue_in(self, time_delta, func, *args, **kwargs): return job def schedule(self, scheduled_time, func, args=None, kwargs=None, - interval=None, repeat=None, result_ttl=None, ttl=None, + interval=None, repeat=None, uniq=None, result_ttl=None, ttl=None, timeout=None, id=None, description=None, queue_name=None): """ Schedule a job to be periodically executed, at a certain interval. @@ -143,6 +143,8 @@ def schedule(self, scheduled_time, func, args=None, kwargs=None, job.meta['repeat'] = int(repeat) if repeat and interval is None: raise ValueError("Can't repeat a job without interval argument") + if uniq is not None: + job.meta['uniq'] = bool(uniq) job.save() self.connection._zadd(self.scheduled_jobs_key, to_unix(scheduled_time), @@ -287,13 +289,17 @@ def enqueue_job(self, job): interval = job.meta.get('interval', None) repeat = job.meta.get('repeat', None) cron_string = job.meta.get('cron_string', None) + uniq = job.meta.get('uniq', None) # If job is a repeated job, decrement counter if repeat: job.meta['repeat'] = int(repeat) - 1 queue = self.get_queue_for_job(job) - queue.enqueue_job(job) + if not uniq or uniq and job.id not in queue.get_job_ids(): + queue.enqueue_job(job) + else: + self.log.debug('{0} not pushed: already in queue with uniq'.format(job.id)) self.connection.zrem(self.scheduled_jobs_key, job.id) if interval: