From 1e7138301a7e7b08fc420fa04c16c0ea0d6c5255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Thu, 5 Nov 2026 22:20:14 +0100 Subject: [PATCH] fix: run synchronous test jobs after the outermost transaction commits CKAN 2.12 runs package_update in a nested transaction. Jobs that commit the session closed it (ResourceClosedError) when executed inline. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 3 +++ dcor_shared/testing.py | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 3e1dbe7..abfaa35 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,7 @@ 1.1.3 + - fix: `testing.synchronous_enqueue_job` runs jobs that are enqueued + within a nested transaction after the outermost transaction is + committed (CKAN 2.12 runs `package_update` in a nested transaction) - ref: connect to Redis via `ckan.lib.redis.connect_to_redis`, since `ckan.lib.jobs._connect` does not exist anymore in CKAN 2.12 - tests: use Redis 7 in CI (CKAN 2.12 requires Redis >= 6) diff --git a/dcor_shared/testing.py b/dcor_shared/testing.py index f0dba31..e64a1db 100644 --- a/dcor_shared/testing.py +++ b/dcor_shared/testing.py @@ -10,6 +10,7 @@ import ckan.tests.factories as factories import ckan.tests.helpers as helpers import requests +import sqlalchemy from .ckan import get_ckan_config_option from . import s3 @@ -160,6 +161,10 @@ def make_resource_via_s3( return rid +#: jobs from :func:`synchronous_enqueue_job` waiting for a commit +_pending_jobs = [] + + def synchronous_enqueue_job(job_func, args=None, kwargs=None, title=None, queue=None, rq_kwargs=None): """ @@ -175,12 +180,44 @@ def synchronous_enqueue_job(job_func, args=None, kwargs=None, title=None, Also, since we are running the tests as root on a ckan instance that is run by www-data, modifying files on disk in background jobs (which were started by supervisor as www-data) does not work. + + If the job is enqueued within a nested database transaction (CKAN 2.12 + runs `package_update` in one), the job is run after the outermost + transaction is committed. Running it right away would commit the + session and close the nested transaction. In production, the jobs + are run by a separate worker. """ if rq_kwargs is None: rq_kwargs = {} args = args or [] kwargs = kwargs or {} - job_func(*args, **kwargs) + session = ckan.model.Session() + if session.in_nested_transaction(): + _pending_jobs.append((job_func, args, kwargs)) + for name, func in [("after_commit", _on_commit), + ("after_transaction_end", _run_pending_jobs)]: + if not sqlalchemy.event.contains(session, name, func): + sqlalchemy.event.listen(session, name, func) + else: + job_func(*args, **kwargs) + + +def _on_commit(session): + # `after_commit` is also called when a nested transaction is committed + if not session.in_nested_transaction(): + session.info["dcor_shared_committed"] = True + + +def _run_pending_jobs(session, transaction): + """Run pending jobs after the outermost transaction ended""" + if transaction.parent is not None: + return + committed = session.info.pop("dcor_shared_committed", False) + jobs = list(_pending_jobs) + _pending_jobs.clear() + if committed: + for job_func, args, kwargs in jobs: + job_func(*args, **kwargs) def upload_presigned_to_s3(