diff --git a/rq/job.py b/rq/job.py index eceb771..bb61fc4 100644 --- a/rq/job.py +++ b/rq/job.py @@ -58,7 +58,7 @@ def cancel_job(job_id, connection=None): """Cancels the job with the given job ID, preventing execution. Discards any job info (i.e. it can't be requeued later). """ - Job(job_id, connection=connection).cancel() + Job.fetch(job_id, connection=connection).cancel() def requeue_job(job_id, connection=None): diff --git a/tests/test_job.py b/tests/test_job.py index 71bbbea..30917ee 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -10,7 +10,7 @@ from tests.helpers import strip_microseconds from rq.compat import PY2, as_text from rq.exceptions import NoSuchJobError, UnpickleError -from rq.job import Job, get_current_job, JobStatus +from rq.job import Job, get_current_job, JobStatus, cancel_job from rq.queue import Queue from rq.registry import DeferredJobRegistry from rq.utils import utcformat @@ -427,3 +427,11 @@ class TestJob(RQTestCase): queue.enqueue(fixtures.say_hello, job_id="1234", ttl=1) time.sleep(1) self.assertEqual(0, len(queue.get_jobs())) + + def test_create_and_cancel_job(self): + """test creating and using cancel_job deletes job properly""" + queue = Queue(connection=self.testconn) + job = queue.enqueue(fixtures.say_hello) + self.assertEqual(1, len(queue.get_jobs())) + cancel_job(job.id) + self.assertEqual(0, len(queue.get_jobs()))