kuryr-kubernetes/kuryr_kubernetes/handlers/health.py
Michał Dulko 1e2e5b1633 Add original exception to log about dead component
This commit make sure we save the exception that caused a handler to be
dead and log it when printing "Component X is dead".

Change-Id: Idcc71a60b2fe448287f2e2d1a0747657c12a5719
2021-06-24 13:07:01 +02:00

58 lines
1.6 KiB
Python

# Copyright 2018 Maysa de Macedo Souza.
#
# 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.
class HealthRegister(object):
instance = None
def __init__(self):
self.registry = []
def register(self, elem):
self.registry.append(elem)
@classmethod
def get_instance(cls):
if not HealthRegister.instance:
HealthRegister.instance = cls()
return HealthRegister.instance
class HealthHandler(object):
"""Base class for health handlers."""
def __init__(self):
super(HealthHandler, self).__init__()
self._alive = True
self._ready = True
self._manager = HealthRegister.get_instance()
self._manager.register(self)
self._last_exception = None
def set_liveness(self, alive, exc=None):
if exc:
self._last_exception = exc
self._alive = alive
def set_readiness(self, ready):
self._ready = ready
def is_alive(self):
return self._alive
def is_ready(self, *args):
return self._ready
def get_last_exception(self):
return self._last_exception