-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Fix/deploy async fixes #239
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
80cf517
reimplemented healthcheck
solarw 1d4c44e
linting
solarw bd393ee
version bump
solarw f89882c
version bump
solarw 699ba38
version bump
solarw 279ebf9
version bump
solarw b3162a3
version bump
solarw 20bc26c
fix for blocking call in async endpoints
solarw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2024 Valory AG | ||
# | ||
# 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. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
"""Source code for checking aea is alive..""" | ||
import asyncio | ||
import typing as t | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
import aiohttp # type: ignore | ||
from aea.helpers.logging import setup_logger | ||
|
||
from operate.services.manage import ServiceManager # type: ignore | ||
|
||
|
||
HTTP_OK = 200 | ||
|
||
|
||
class HealthChecker: | ||
"""Health checker manager.""" | ||
|
||
SLEEP_PERIOD = 30 | ||
PORT_UP_TIMEOUT = 120 # seconds | ||
|
||
def __init__(self, service_manager: ServiceManager) -> None: | ||
"""Init the healtch checker.""" | ||
self._jobs: t.Dict[str, asyncio.Task] = {} | ||
self.logger = setup_logger(name="operate.health_checker") | ||
self.logger.info("[HEALTCHECKER]: created") | ||
self._service_manager = service_manager | ||
|
||
def start_for_service(self, service: str) -> None: | ||
"""Start for a specific service.""" | ||
self.logger.info(f"[HEALTCHECKER]: Starting healthcheck job for {service}") | ||
if service in self._jobs: | ||
self.stop_for_service(service=service) | ||
|
||
loop = asyncio.get_running_loop() | ||
self._jobs[service] = loop.create_task( | ||
self.healthcheck_job( | ||
service=service, | ||
) | ||
) | ||
|
||
def stop_for_service(self, service: str) -> None: | ||
"""Stop for a specific service.""" | ||
if service not in self._jobs: | ||
return | ||
self.logger.info( | ||
f"[HEALTCHECKER]: Cancelling existing healthcheck_jobs job for {service}" | ||
) | ||
status = self._jobs[service].cancel() | ||
if not status: | ||
self.logger.info( | ||
f"[HEALTCHECKER]: Healthcheck job cancellation for {service} failed" | ||
) | ||
|
||
@staticmethod | ||
async def check_service_health(service: str) -> bool: | ||
"""Check the service health""" | ||
del service | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the purpose of |
||
async with aiohttp.ClientSession() as session: | ||
async with session.get("http://localhost:8716/healthcheck") as resp: | ||
status = resp.status | ||
response_json = await resp.json() | ||
return status == HTTP_OK and response_json.get( | ||
"is_transitioning_fast", False | ||
) | ||
|
||
async def healthcheck_job( | ||
self, | ||
service: str, | ||
) -> None: | ||
"""Start a background health check job.""" | ||
|
||
try: | ||
self.logger.info( | ||
f"[HEALTCHECKER] Start healthcheck job for service: {service}" | ||
) | ||
|
||
async def _wait_for_port(sleep_period: int = 15) -> None: | ||
self.logger.info("[HEALTCHECKER]: wait port is up") | ||
while True: | ||
try: | ||
await self.check_service_health(service) | ||
self.logger.info("[HEALTCHECKER]: port is UP") | ||
return | ||
except aiohttp.ClientConnectionError: | ||
self.logger.error("[HEALTCHECKER]: error connecting http port") | ||
await asyncio.sleep(sleep_period) | ||
|
||
async def _check_port_ready( | ||
timeout: int = self.PORT_UP_TIMEOUT, sleep_period: int = 15 | ||
) -> bool: | ||
try: | ||
await asyncio.wait_for( | ||
_wait_for_port(sleep_period=sleep_period), timeout=timeout | ||
) | ||
return True | ||
except asyncio.TimeoutError: | ||
return False | ||
|
||
async def _check_health( | ||
number_of_fails: int = 5, sleep_period: int = self.SLEEP_PERIOD | ||
) -> None: | ||
fails = 0 | ||
while True: | ||
try: | ||
# Check the service health | ||
healthy = await self.check_service_health(service) | ||
except aiohttp.ClientConnectionError: | ||
self.logger.info( | ||
f"[HEALTCHECKER] {service} port read failed. restart" | ||
) | ||
return | ||
|
||
if not healthy: | ||
fails += 1 | ||
self.logger.info( | ||
f"[HEALTCHECKER] {service} not healthy for {fails} time in a row" | ||
) | ||
else: | ||
self.logger.info(f"[HEALTCHECKER] {service} is HEALTHY") | ||
# reset fails if comes healty | ||
fails = 0 | ||
|
||
if fails >= number_of_fails: | ||
# too much fails, exit | ||
self.logger.error( | ||
f"[HEALTCHECKER] {service} failed {fails} times in a row. restart" | ||
) | ||
return | ||
await asyncio.sleep(sleep_period) | ||
|
||
async def _restart(service_manager: ServiceManager, service: str) -> None: | ||
def _do_restart() -> None: | ||
service_manager.stop_service_locally(hash=service) | ||
service_manager.deploy_service_locally(hash=service) | ||
|
||
loop = asyncio.get_event_loop() | ||
with ThreadPoolExecutor() as executor: | ||
future = loop.run_in_executor(executor, _do_restart) | ||
await future | ||
exception = future.exception() | ||
if exception is not None: | ||
raise exception | ||
|
||
# upper cycle | ||
while True: | ||
self.logger.info(f"[HEALTCHECKER] {service} wait for port ready") | ||
if await _check_port_ready(timeout=self.PORT_UP_TIMEOUT): | ||
# blocking till restart needed | ||
self.logger.info( | ||
f"[HEALTCHECKER] {service} port is ready, checking health every {self.SLEEP_PERIOD}" | ||
) | ||
await _check_health(sleep_period=self.SLEEP_PERIOD) | ||
|
||
else: | ||
self.logger.info( | ||
"[HEALTCHECKER] port not ready within timeout. restart deployment" | ||
) | ||
|
||
# perform restart | ||
# TODO: blocking!!!!!!! | ||
await _restart(self._service_manager, service) | ||
except Exception: | ||
self.logger.exception(f"problems running healthcheckr for {service}") | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny typo, not a big deal