From 3da3eab11d737834d3d2e1c7ae41f5bba81a1d30 Mon Sep 17 00:00:00 2001 From: Marko Mrdjenovic Date: Sun, 29 Jan 2017 21:15:50 +0100 Subject: [PATCH 1/2] moved job run to separate method to make async jobs easier --- rq/queue.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rq/queue.py b/rq/queue.py index 417141a..e0eeeae 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -227,13 +227,17 @@ class Queue(object): job = self.enqueue_job(job, at_front=at_front) if not self._async: - job.perform() - job.set_status(JobStatus.FINISHED) - job.save() - job.cleanup(DEFAULT_RESULT_TTL) + job = self.run_job(job) return job + def run_job(self, job): + job.perform() + job.set_status(JobStatus.FINISHED) + job.save() + job.cleanup(DEFAULT_RESULT_TTL) + return job + def enqueue(self, f, *args, **kwargs): """Creates a job to represent the delayed function call and enqueues it. From 35fb6b0b9ac9ddc1745b6f80d19025e88161fad1 Mon Sep 17 00:00:00 2001 From: Marko Mrdjenovic Date: Sun, 29 Jan 2017 21:53:09 +0100 Subject: [PATCH 2/2] move actual execute to a separate method to make it easier to run async jobs --- rq/job.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rq/job.py b/rq/job.py index b820f79..2b34109 100644 --- a/rq/job.py +++ b/rq/job.py @@ -497,11 +497,14 @@ class Job(object): self.ttl = -1 _job_stack.push(self.id) try: - self._result = self.func(*self.args, **self.kwargs) + self._result = self._execute() finally: assert self.id == _job_stack.pop() return self._result + def _execute(self): + return self.func(*self.args, **self.kwargs) + def get_ttl(self, default_ttl=None): """Returns ttl for a job that determines how long a job will be persisted. In the future, this method will also be responsible