Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added uniq parameter, update readme #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions rq_scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
Expand Down Expand Up @@ -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():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will kill performance if we have a queue with many jobs. I'm not against this feature, but it will have to be implemented in a scaleable way.

I think we can use a set to check for whether this specific function is already scheduled.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worthwhile to just add a comment around this flag about a possible performance hit? Seems like a popular feature request.

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:
Expand Down