Merge common files in one service file.

All the files was only used by service. Merged in one file.

Rename the common package with underscore to indicate it's private one.
Made the same for the eventlet and threading backend.

Change-Id: Id81d2737f45695f6ccda4807924615b9673bf376
Signed-off-by: Daniel Bengtsson <dbengt@redhat.com>
This commit is contained in:
Daniel Bengtsson
2025-07-15 15:31:49 +02:00
committed by Takashi Kajinami
parent 501b9e85f0
commit 89026fdbae
17 changed files with 104 additions and 124 deletions

View File

@@ -44,6 +44,15 @@ _cached_components: dict[str, Any] | None = None
# optional override hook
_backend_hook: Callable[[], BackendType] | None = None
__all__ = [
"get_component",
"init_backend",
"BackendType",
"register_backend_default_hook",
"get_backend_type",
"get_backend"
]
def register_backend_default_hook(hook: Callable[[], BackendType]) -> None:
"""Register a hook that decides the default backend type.
@@ -93,7 +102,7 @@ def init_backend(type_: BackendType) -> None:
LOG.info(f"Loading backend: {backend_name}")
try:
module_name = f"oslo_service.backend.{backend_name}"
module_name = f"oslo_service.backend._{backend_name}"
module = importlib.import_module(module_name)
backend_class = getattr(module, f"{backend_name.capitalize()}Backend")

View File

@@ -12,7 +12,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import errno
import io
import os
import signal
import sys
from oslo_concurrency import lockutils
from oslo_service._i18n import _
from oslo_service.backend.base import ServiceBase
def is_daemon():
try:
return os.getpgrp() != os.tcgetpgrp(sys.stdout.fileno())
except io.UnsupportedOperation:
return True
except OSError as err:
return err.errno == errno.ENOTTY or False
def is_sighup_and_daemon(signo, signal_handler):
return (signal_handler.is_signal_supported('SIGHUP') and
signo == signal.SIGHUP and is_daemon())
def get_signal_mappings(ignore=('SIG_DFL', 'SIG_IGN')):
@@ -36,3 +59,21 @@ class SignalExit(SystemExit):
def __init__(self, signo, exccode=1):
super().__init__(exccode)
self.signo = signo
def check_service_base(service):
if not isinstance(service, ServiceBase):
raise TypeError(
_("Service %(service)s must be an instance of %(base)s!")
% {'service': service, 'base': ServiceBase})
class Singleton(type):
_instances = {}
_semaphores = lockutils.Semaphores()
def __call__(cls, *args, **kwargs):
with lockutils.lock('singleton_lock', semaphores=cls._semaphores):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]

View File

@@ -14,13 +14,11 @@
# limitations under the License.
from oslo_service.backend._common import service as service_common
from oslo_service.backend._eventlet import loopingcall
from oslo_service.backend._eventlet import service
from oslo_service.backend._eventlet import threadgroup
from oslo_service.backend.base import BaseBackend
from oslo_service.backend.common import daemon_utils
from oslo_service.backend.common import signal_utils
from oslo_service.backend.common import singleton
from oslo_service.backend.eventlet import loopingcall
from oslo_service.backend.eventlet import service
from oslo_service.backend.eventlet import threadgroup
class EventletBackend(BaseBackend):
@@ -40,8 +38,8 @@ class EventletBackend(BaseBackend):
"Services": service.Services,
"ServiceWrapper": service.ServiceWrapper,
"SignalHandler": service.SignalHandler,
"SignalExit": signal_utils.SignalExit,
"Singleton": singleton.Singleton,
"SignalExit": service_common.SignalExit,
"Singleton": service_common.Singleton,
# Looping call-related classes
"LoopingCallBase": loopingcall.LoopingCallBase,
@@ -60,6 +58,6 @@ class EventletBackend(BaseBackend):
# Functions
"launch": service.launch,
"_is_daemon": daemon_utils.is_daemon,
"_is_sighup_and_daemon": daemon_utils.is_sighup_and_daemon,
"_is_daemon": service_common.is_daemon,
"_is_sighup_and_daemon": service_common.is_sighup_and_daemon,
}

View File

@@ -34,20 +34,17 @@ from eventlet import tpool
from oslo_service._i18n import _
from oslo_service import _options
from oslo_service.backend.base import ServiceBase
from oslo_service.backend.common.constants import \
from oslo_service.backend._common.constants import \
_LAUNCHER_RESTART_METHODS
from oslo_service.backend.common.daemon_utils import \
from oslo_service.backend._common.service \
import check_service_base as _check_service_base
from oslo_service.backend._common.service import get_signal_mappings
from oslo_service.backend._common.service import \
is_sighup_and_daemon as _is_sighup_and_daemon
from oslo_service.backend.common.signal_utils import \
get_signal_mappings
from oslo_service.backend.common.signal_utils import \
SignalExit
from oslo_service.backend.common.singleton import \
Singleton
from oslo_service.backend.common.validation_utils import \
check_service_base as _check_service_base
from oslo_service.backend.eventlet import threadgroup
from oslo_service.backend._common.service import SignalExit
from oslo_service.backend._common.service import Singleton
from oslo_service.backend._eventlet import threadgroup
from oslo_service.backend.base import ServiceBase
from oslo_service import eventlet_backdoor
from oslo_service import systemd

View File

@@ -21,7 +21,7 @@ from debtcollector import removals
import eventlet
from eventlet import greenpool
from oslo_service.backend.eventlet import loopingcall
from oslo_service.backend._eventlet import loopingcall
from oslo_utils import timeutils
LOG = logging.getLogger(__name__)

View File

@@ -12,13 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from oslo_service.backend._common import service as service_common
from oslo_service.backend._threading import loopingcall
from oslo_service.backend._threading import service
from oslo_service.backend._threading import threadgroup
from oslo_service.backend.base import BaseBackend
from oslo_service.backend.common import daemon_utils
from oslo_service.backend.common import signal_utils
from oslo_service.backend.common import singleton
from oslo_service.backend.threading import loopingcall
from oslo_service.backend.threading import service
from oslo_service.backend.threading import threadgroup
class ThreadingBackend(BaseBackend):
@@ -37,9 +35,9 @@ class ThreadingBackend(BaseBackend):
"Service": service.Service,
"Services": service.Services,
"ServiceWrapper": service.ServiceWrapper,
"SignalExit": signal_utils.SignalExit,
"SignalExit": service_common.SignalExit,
"SignalHandler": service.SignalHandler,
"Singleton": singleton.Singleton,
"Singleton": service_common.Singleton,
# Looping call-related classes
"LoopingCallBase": loopingcall.LoopingCallBase,
"LoopingCallDone": loopingcall.LoopingCallDone,
@@ -54,7 +52,7 @@ class ThreadingBackend(BaseBackend):
"ThreadGroup": threadgroup.ThreadGroup,
"Thread": threadgroup.Thread,
# Functions
"_is_daemon": daemon_utils.is_daemon,
"_is_sighup_and_daemon": daemon_utils.is_sighup_and_daemon,
"_is_daemon": service_common.is_daemon,
"_is_sighup_and_daemon": service_common.is_sighup_and_daemon,
"launch": service.launch,
}

View File

@@ -24,13 +24,13 @@ import cotyledon
from cotyledon import oslo_config_glue
from oslo_service._i18n import _
from oslo_service.backend._common.constants import _LAUNCHER_RESTART_METHODS
from oslo_service.backend._common.service \
import check_service_base as _check_service_base
from oslo_service.backend._common.service import get_signal_mappings
from oslo_service.backend._common.service import Singleton
from oslo_service.backend._threading import threadgroup
from oslo_service.backend.base import ServiceBase
from oslo_service.backend.common.constants import _LAUNCHER_RESTART_METHODS
from oslo_service.backend.common.signal_utils import get_signal_mappings
from oslo_service.backend.common.singleton import Singleton
from oslo_service.backend.common.validation_utils import \
check_service_base as _check_service_base
from oslo_service.backend.threading import threadgroup
LOG = logging.getLogger(__name__)

View File

@@ -18,7 +18,7 @@ import threading
import time
import warnings
from oslo_service.backend.threading import loopingcall
from oslo_service.backend._threading import loopingcall
LOG = logging.getLogger(__name__)

View File

@@ -1,33 +0,0 @@
# Copyright (C) 2025 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import errno
import io
import os
import signal
import sys
def is_daemon():
try:
return os.getpgrp() != os.tcgetpgrp(sys.stdout.fileno())
except io.UnsupportedOperation:
return True
except OSError as err:
return err.errno == errno.ENOTTY or False
def is_sighup_and_daemon(signo, signal_handler):
return (signal_handler.is_signal_supported('SIGHUP') and
signo == signal.SIGHUP and is_daemon())

View File

@@ -1,26 +0,0 @@
# Copyright (C) 2025 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from oslo_concurrency import lockutils
class Singleton(type):
_instances = {}
_semaphores = lockutils.Semaphores()
def __call__(cls, *args, **kwargs):
with lockutils.lock('singleton_lock', semaphores=cls._semaphores):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]

View File

@@ -1,23 +0,0 @@
# Copyright (C) 2025 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from oslo_service._i18n import _
from oslo_service.backend.base import ServiceBase
def check_service_base(service):
if not isinstance(service, ServiceBase):
raise TypeError(
_("Service %(service)s must be an instance of %(base)s!")
% {'service': service, 'base': ServiceBase})

View File

@@ -18,7 +18,7 @@ from unittest import TestCase
from oslo_config import cfg
from oslo_service.backend.threading import service
from oslo_service.backend._threading import service
class DummyService(service.ServiceBase):

View File

@@ -0,0 +1,19 @@
---
upgrade:
- |
Internal backend modules have been renamed to **explicitly indicate that
they are** private:
* ``oslo_service.backend.common`` → ``oslo_service.backend._common``
* ``oslo_service.backend.threading`` → ``oslo_service.backend._threading``
* ``oslo_service.backend.eventlet`` → ``oslo_service.backend._eventlet``
These modules were never intended to be used directly. **This change makes
it clearer** that they are internal to ``oslo_service.backend``. Projects
should only interact with the backend system via the public interface
provided in ``oslo_service.backend.__init__``—**specifically through
functions like** ``get_component()``, ``get_backend()``, and
``init_backend()``.
**If your project was** directly importing any of these modules, please
update the imports to use the public API instead.