Skip to content
Merged
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: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
1.1.1
- fix: `RQJob` dependencies were never honored, because the job IDs
in `depends_on` had a superfluous underscore and dependent jobs
were enqueued before the jobs they depend on
- tests: increase coverage (parse, RQJob, config helpers,
`wait_for_resource`, S3 helpers)
- tests: actually run `test_iter_buckets_keep_non_circle`
Expand Down
9 changes: 5 additions & 4 deletions dcor_shared/rqjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def enqueue_job(self, resource: Dict, redis_connect=None):
"job_id": jid_cur,
}
if self.depends_on:
rq_args["depends_on"] = [f"{jid}_{dep}" for dep in self.depends_on]
rq_args["depends_on"] = [f"{jid}{dep}" for dep in self.depends_on]

if not Job.exists(jid_cur, connection=redis_connect):
toolkit.enqueue_job(self.method,
Expand All @@ -69,9 +69,10 @@ def enqueue_all_jobs(cls,
from ckan.lib.jobs import _connect as ckan_redis_connect
redis_connect = ckan_redis_connect()

for inst in cls._instances:
if inst.ckanext == ckanext:
inst.enqueue_job(resource, redis_connect=redis_connect)
# Jobs must be enqueued after the jobs they depend on, otherwise
# RQ ignores the dependency.
for inst in cls.get_all_job_methods_in_order(ckanext):
inst.enqueue_job(resource, redis_connect=redis_connect)

@classmethod
def get_all_job_methods_in_order(cls, ckanext: str):
Expand Down
46 changes: 46 additions & 0 deletions tests/test_rqjob.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from unittest import mock
import uuid

from ckan.lib.jobs import _connect as ckan_redis_connect

import pytest
from rq.job import Job

from dcor_shared import RQJob, rqjob_register

Expand Down Expand Up @@ -89,3 +93,45 @@ def test_rqjob_enqueue_job_exists():
job.enqueue_job({"package_id": "pkg", "position": 0},
redis_connect=mock.MagicMock())
enqueue_job.assert_not_called()


def job_first(resource):
"""First job"""


def job_second(resource):
"""Second job"""


def job_third(resource):
"""Third job"""


@pytest.mark.usefixtures("clean_rqjob_instances")
def test_rqjob_enqueue_all_jobs_depends_on():
# register in reverse order of dependencies
RQJob(job_third, ckanext="test_ext",
depends_on=["job_first", "job_second"])
RQJob(job_second, ckanext="test_ext", depends_on=["job_first"])
RQJob(job_first, ckanext="test_ext")

pkg_id = str(uuid.uuid4())
RQJob.enqueue_all_jobs({"package_id": pkg_id, "position": 0},
ckanext="test_ext")
redis_connect = ckan_redis_connect()
jobs = [Job.fetch(f"{pkg_id}_0_{name}", connection=redis_connect)
for name in ["job_first", "job_second", "job_third"]]
try:
def dependencies(job):
return sorted(dep.id for dep in job.fetch_dependencies())

assert dependencies(jobs[0]) == []
assert dependencies(jobs[1]) == [jobs[0].id]
assert dependencies(jobs[2]) == sorted([jobs[0].id, jobs[1].id])
# no worker is running, so the dependent jobs must wait
assert jobs[0].get_status() == "queued"
assert jobs[1].get_status() == "deferred"
assert jobs[2].get_status() == "deferred"
finally:
for job in jobs:
job.delete()
Loading