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

Add feature to respect dependency #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions rq_scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def stop(signum, frame):
signal.signal(signal.SIGTERM, stop)

def _create_job(self, func, args=None, kwargs=None, commit=True,
result_ttl=None):
result_ttl=None, depends_on=None):
"""
Creates an RQ job and saves it to Redis.
"""
Expand All @@ -81,7 +81,7 @@ def _create_job(self, func, args=None, kwargs=None, commit=True,
if kwargs is None:
kwargs = {}
job = Job.create(func, args=args, connection=self.connection,
kwargs=kwargs, result_ttl=result_ttl)
kwargs=kwargs, result_ttl=result_ttl, depends_on=depends_on)
job.origin = self.queue_name
if commit:
job.save()
Expand Down Expand Up @@ -133,15 +133,15 @@ def enqueue_periodic(self, scheduled_time, interval, repeat, func,
interval=interval, repeat=repeat)

def schedule(self, scheduled_time, func, args=None, kwargs=None,
interval=None, repeat=None, result_ttl=None, timeout=None):
interval=None, repeat=None, result_ttl=None, timeout=None, depends_on=None):
"""
Schedule a job to be periodically executed, at a certain interval.
"""
# Set result_ttl to -1 for periodic jobs, if result_ttl not specified
if interval is not None and result_ttl is None:
result_ttl = -1
job = self._create_job(func, args=args, kwargs=kwargs, commit=False,
result_ttl=result_ttl)
result_ttl=result_ttl, depends_on=depends_on)
if interval is not None:
job.meta['interval'] = int(interval)
if repeat is not None:
Expand Down Expand Up @@ -296,8 +296,13 @@ def enqueue_jobs(self):

jobs = self.get_jobs_to_queue()
for job in jobs:
self.enqueue_job(job)

if job.dependency is None: # if there is no dependency, then go ahead schedule it
self.enqueue_job(job)
else: # if there is a dependency, then check if that job is done
parent_job = job.dependency
if parent_job.is_finished:
self.enqueue_job(job)

# Refresh scheduler key's expiry
self.connection.expire(self.scheduler_key, self._interval + 10)
return jobs
Expand Down