From d5ed04eb1582058e5d2aaf47a62ce27cc55b9d35 Mon Sep 17 00:00:00 2001 From: Louis-David Perron <100434291+perronld@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:52:15 -0500 Subject: [PATCH 1/3] Woraround for registry resolving when restarting celery prefork workers --- weaver/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/weaver/utils.py b/weaver/utils.py index e9b7aec0b..9018e3d62 100644 --- a/weaver/utils.py +++ b/weaver/utils.py @@ -35,6 +35,7 @@ from bs4 import BeautifulSoup from celery.app import Celery from mypy_boto3_s3.literals import RegionName +from pyramid_celery import celery_app as app from pyramid.config import Configurator from pyramid.exceptions import ConfigurationError from pyramid.httpexceptions import ( @@ -47,7 +48,7 @@ from pyramid.request import Request as PyramidRequest from pyramid.response import _guess_type as guess_file_contents # noqa: W0212 from pyramid.settings import asbool, aslist -from pyramid.threadlocal import get_current_registry +from pyramid.threadlocal import get_current_registry, get_current_request from pyramid_beaker import set_cache_regions_from_settings from requests import HTTPError as RequestsHTTPError, Response from requests.structures import CaseInsensitiveDict @@ -469,7 +470,10 @@ def get_registry(container=None, nothrow=False): if isinstance(container, Registry): return container if isinstance(container, WerkzeugRequest) or container is None: - return get_current_registry() + if get_current_request() is None: + return get_registry(app) + else: + return get_current_registry() if nothrow: return None raise TypeError(f"Could not retrieve registry from container object of type [{fully_qualified_name(container)}].") From d85cf99a034142e044891edd2f2250c96474e2a8 Mon Sep 17 00:00:00 2001 From: Louis-David Perron <100434291+perronld@users.noreply.github.com> Date: Wed, 15 Nov 2023 17:06:50 -0500 Subject: [PATCH 2/3] Fixed import sorting --- weaver/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weaver/utils.py b/weaver/utils.py index 9018e3d62..346ea97d9 100644 --- a/weaver/utils.py +++ b/weaver/utils.py @@ -35,7 +35,6 @@ from bs4 import BeautifulSoup from celery.app import Celery from mypy_boto3_s3.literals import RegionName -from pyramid_celery import celery_app as app from pyramid.config import Configurator from pyramid.exceptions import ConfigurationError from pyramid.httpexceptions import ( @@ -50,6 +49,7 @@ from pyramid.settings import asbool, aslist from pyramid.threadlocal import get_current_registry, get_current_request from pyramid_beaker import set_cache_regions_from_settings +from pyramid_celery import celery_app as app from requests import HTTPError as RequestsHTTPError, Response from requests.structures import CaseInsensitiveDict from requests_file import FileAdapter From ab1b834164d79d00935fdb2d902d146c951aa4d2 Mon Sep 17 00:00:00 2001 From: Louis-David Perron <100434291+perronld@users.noreply.github.com> Date: Mon, 20 Nov 2023 14:51:41 -0500 Subject: [PATCH 3/3] Replaced get_current_request by sys.argv to detect celery worker presence --- weaver/utils.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/weaver/utils.py b/weaver/utils.py index 346ea97d9..55308b71a 100644 --- a/weaver/utils.py +++ b/weaver/utils.py @@ -47,7 +47,7 @@ from pyramid.request import Request as PyramidRequest from pyramid.response import _guess_type as guess_file_contents # noqa: W0212 from pyramid.settings import asbool, aslist -from pyramid.threadlocal import get_current_registry, get_current_request +from pyramid.threadlocal import get_current_registry from pyramid_beaker import set_cache_regions_from_settings from pyramid_celery import celery_app as app from requests import HTTPError as RequestsHTTPError, Response @@ -469,11 +469,10 @@ def get_registry(container=None, nothrow=False): return container.registry if isinstance(container, Registry): return container + if container is None and sys.argv[0].rsplit("/", 1)[-1] == "celery": + return app.conf.get("PYRAMID_REGISTRY", {}) if isinstance(container, WerkzeugRequest) or container is None: - if get_current_request() is None: - return get_registry(app) - else: - return get_current_registry() + return get_current_registry() if nothrow: return None raise TypeError(f"Could not retrieve registry from container object of type [{fully_qualified_name(container)}].")