Daniel Bengtsson 795aad12aa Introduce configurable backend system in oslo.service
This patch introduces a new backend system in oslo.service, enabling
the dynamic loading of backends based on a configurable constant
(`DEFAULT_BACKEND`). Key updates include:

- A `get_backend()` method in `backend/__init__.py` to dynamically load
  backends based on the `DEFAULT_BACKEND` constant, which can be
  overridden at import time.
- Validation of backends against an enumeration type to ensure correctness
- Improved caching mechanism for loaded backends, reducing overhead
  during repeated calls to `get_backend()`.
- Robust error handling for invalid or non-existent backend modules.
- Unit tests covering:
  - Default backend behavior.
  - Cache functionality and invalidation.
  - Error handling for missing backend modules.
  - Retrieval of components from loaded backends.

- Simplifies backend configuration by relying on a single constant that
  can be overridden at import time.
- Ensures modularity and flexibility to support multiple backends like
  Eventlet and Threading.
- Enhances testability through isolation and comprehensive mocks.
- Maintains backward compatibility with the existing implementation
  while providing a clear migration path.

This patch introduces the backend system without altering the current
behavior of oslo.service, ensuring a seamless integration.

Change-Id: I57a6371aea42d2dfc1ad06a0bbf60000e6682be3
2025-01-23 12:31:55 -05:00

68 lines
2.5 KiB
Python

# Copyright (C) 2024 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.backend.base import BaseBackend
from oslo_service import loopingcall
from oslo_service import service
from oslo_service import threadgroup
class EventletBackend(BaseBackend):
"""Backend implementation for Eventlet.
In this revision, this is a "stub" for a real backend; right now it imports
the regular implementation of oslo.service, which will be moved
entirely into a backend in a subsequent patch.
"""
@staticmethod
def get_service_components():
"""Return the components provided by the Eventlet backend."""
return {
# Classes
"ServiceBase": service.ServiceBase,
"ServiceLauncher": service.ServiceLauncher,
"Launcher": service.Launcher,
"ProcessLauncher": service.ProcessLauncher,
"Service": service.Service,
"Services": service.Services,
"ServiceWrapper": service.ServiceWrapper,
"SignalHandler": service.SignalHandler,
"SignalExit": service.SignalExit,
# Looping call-related classes
"LoopingCallBase": loopingcall.LoopingCallBase,
"LoopingCallDone": loopingcall.LoopingCallDone,
"LoopingCallTimeOut": loopingcall.LoopingCallTimeOut,
"FixedIntervalLoopingCall": loopingcall.FixedIntervalLoopingCall,
"FixedIntervalWithTimeoutLoopingCall":
loopingcall.FixedIntervalWithTimeoutLoopingCall,
"DynamicLoopingCall": loopingcall.DynamicLoopingCall,
"BackOffLoopingCall": loopingcall.BackOffLoopingCall,
"RetryDecorator": loopingcall.RetryDecorator,
# Threadgroup call-related classes
"ThreadGroup": threadgroup.ThreadGroup,
"Thread": threadgroup.Thread,
# Functions
"launch": service.launch,
"_is_daemon": service._is_daemon,
"_is_sighup_and_daemon": service._is_sighup_and_daemon,
}