diff --git a/frontik/service_discovery.py b/frontik/service_discovery.py index 79f0638b8..813a24eee 100644 --- a/frontik/service_discovery.py +++ b/frontik/service_discovery.py @@ -109,37 +109,45 @@ def __init__( self.consul_weight_total_timeout_sec = options.consul_weight_total_timeout_sec self.consul_weight_consistency_mode = ConsistencyMode(options.consul_weight_consistency_mode.lower()) self.consul_cache_initial_warmup_timeout_sec = options.consul_cache_initial_warmup_timeout_sec + self.weight: int | None = None async def register_service(self) -> None: address = _get_service_address(self.options) http_check = _create_http_check(self.options, address) - index = None - old_weight = None + await self._async_register(address, http_check) while True: - index, value = await self.consul.kv.get( - f'host/{self.hostname}/weight', - index=index, - wait=self.consul_weight_watch_seconds, - total_timeout=self.consul_weight_total_timeout_sec, - consistency=self.consul_weight_consistency_mode, - ) - weight = _get_weight_or_default(value) - if old_weight != weight: - old_weight = weight - register_params = { - 'service_id': self.service_id, - 'address': address, - 'port': self.options.port, - 'check': http_check, - 'tags': self.options.consul_tags, - 'weights': Weight.weights(weight, 0), - 'caller': self.service_name, - } - if await self.consul.agent.service.register(self.service_name, **register_params): - log.info('Successfully registered service %s', register_params) - else: - msg = f'Failed to register {self.service_id}' - raise Exception(msg) + try: + await asyncio.sleep(options.consul_cache_backoff_delay_seconds) + await self._async_register(address, http_check) + except Exception as exc: + log.exception('Failed to register %s: %s', self.service_id, exc) + + async def _async_register(self, address: str | None, http_check: dict) -> None: + index, value = await self.consul.kv.get( + f'host/{self.hostname}/weight', + wait=self.consul_weight_watch_seconds, + total_timeout=self.consul_weight_total_timeout_sec, + consistency=self.consul_weight_consistency_mode, + ) + weight = _get_weight_or_default(value) + if self.weight == weight: + return + + self.weight = weight + register_params = { + 'service_id': self.service_id, + 'address': address, + 'port': self.options.port, + 'check': http_check, + 'tags': self.options.consul_tags, + 'weights': Weight.weights(weight, 0), + 'caller': self.service_name, + } + if await self.consul.agent.service.register(self.service_name, **register_params): + log.info('Successfully registered service %s', register_params) + else: + msg = f'Failed to register {self.service_id}' + raise Exception(msg) async def deregister_service_and_close(self) -> None: if await self.consul.agent.service.deregister(self.service_id, self.service_name): diff --git a/poetry.lock b/poetry.lock index 94726c63c..d5b103db8 100644 --- a/poetry.lock +++ b/poetry.lock @@ -759,16 +759,6 @@ files = [ {file = "MarkupSafe-2.1.3-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:5bbe06f8eeafd38e5d0a4894ffec89378b6c6a625ff57e3028921f8ff59318ac"}, {file = "MarkupSafe-2.1.3-cp311-cp311-win32.whl", hash = "sha256:dd15ff04ffd7e05ffcb7fe79f1b98041b8ea30ae9234aed2a9168b5797c3effb"}, {file = "MarkupSafe-2.1.3-cp311-cp311-win_amd64.whl", hash = "sha256:134da1eca9ec0ae528110ccc9e48041e0828d79f24121a1a146161103c76e686"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-macosx_10_9_universal2.whl", hash = "sha256:f698de3fd0c4e6972b92290a45bd9b1536bffe8c6759c62471efaa8acb4c37bc"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:aa57bd9cf8ae831a362185ee444e15a93ecb2e344c8e52e4d721ea3ab6ef1823"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ffcc3f7c66b5f5b7931a5aa68fc9cecc51e685ef90282f4a82f0f5e9b704ad11"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:47d4f1c5f80fc62fdd7777d0d40a2e9dda0a05883ab11374334f6c4de38adffd"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1f67c7038d560d92149c060157d623c542173016c4babc0c1913cca0564b9939"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:9aad3c1755095ce347e26488214ef77e0485a3c34a50c5a5e2471dff60b9dd9c"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:14ff806850827afd6b07a5f32bd917fb7f45b046ba40c57abdb636674a8b559c"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8f9293864fe09b8149f0cc42ce56e3f0e54de883a9de90cd427f191c346eb2e1"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-win32.whl", hash = "sha256:715d3562f79d540f251b99ebd6d8baa547118974341db04f5ad06d5ea3eb8007"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-win_amd64.whl", hash = "sha256:1b8dd8c3fd14349433c79fa8abeb573a55fc0fdd769133baac1f5e07abf54aeb"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:8e254ae696c88d98da6555f5ace2279cf7cd5b3f52be2b5cf97feafe883b58d2"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:cb0932dc158471523c9637e807d9bfb93e06a95cbf010f1a38b98623b929ef2b"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9402b03f1a1b4dc4c19845e5c749e3ab82d5078d16a2a4c2cd2df62d57bb0707"},