-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into is1779/search-users
- Loading branch information
Showing
41 changed files
with
699 additions
and
151 deletions.
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
37 changes: 37 additions & 0 deletions
37
packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/agent/containers.py
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,37 @@ | ||
import logging | ||
from datetime import timedelta | ||
from typing import Final | ||
|
||
from models_library.docker import DockerNodeID | ||
from models_library.projects_nodes_io import NodeID | ||
from models_library.rabbitmq_basic_types import RPCMethodName, RPCNamespace | ||
from pydantic import NonNegativeInt, TypeAdapter | ||
from servicelib.logging_utils import log_decorator | ||
from servicelib.rabbitmq import RabbitMQRPCClient | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
_REQUEST_TIMEOUT: Final[NonNegativeInt] = int(timedelta(minutes=60).total_seconds()) | ||
|
||
|
||
@log_decorator(_logger, level=logging.DEBUG) | ||
async def force_container_cleanup( | ||
rabbitmq_rpc_client: RabbitMQRPCClient, | ||
*, | ||
docker_node_id: DockerNodeID, | ||
swarm_stack_name: str, | ||
node_id: NodeID, | ||
) -> None: | ||
result = await rabbitmq_rpc_client.request( | ||
RPCNamespace.from_entries( | ||
{ | ||
"service": "agent", | ||
"docker_node_id": docker_node_id, | ||
"swarm_stack_name": swarm_stack_name, | ||
} | ||
), | ||
TypeAdapter(RPCMethodName).validate_python("force_container_cleanup"), | ||
node_id=node_id, | ||
timeout_s=_REQUEST_TIMEOUT, | ||
) | ||
assert result is None # nosec |
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
20 changes: 20 additions & 0 deletions
20
services/agent/src/simcore_service_agent/api/rpc/_containers.py
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,20 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
from models_library.projects_nodes_io import NodeID | ||
from servicelib.logging_utils import log_context | ||
from servicelib.rabbitmq import RPCRouter | ||
|
||
from ...services.containers_manager import ContainersManager | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
router = RPCRouter() | ||
|
||
|
||
@router.expose() | ||
async def force_container_cleanup(app: FastAPI, *, node_id: NodeID) -> None: | ||
with log_context( | ||
_logger, logging.INFO, f"removing all orphan container for {node_id=}" | ||
): | ||
await ContainersManager.get_from_app_state(app).force_container_cleanup(node_id) |
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
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
71 changes: 71 additions & 0 deletions
71
services/agent/src/simcore_service_agent/services/containers_manager.py
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,71 @@ | ||
import logging | ||
from dataclasses import dataclass, field | ||
|
||
from aiodocker import Docker | ||
from fastapi import FastAPI | ||
from models_library.api_schemas_directorv2.services import ( | ||
DYNAMIC_PROXY_SERVICE_PREFIX, | ||
DYNAMIC_SIDECAR_SERVICE_PREFIX, | ||
) | ||
from models_library.projects_nodes_io import NodeID | ||
from servicelib.fastapi.app_state import SingletonInAppStateMixin | ||
|
||
from .docker_utils import get_containers_with_prefixes, remove_container_forcefully | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class ContainersManager(SingletonInAppStateMixin): | ||
app_state_name: str = "containers_manager" | ||
|
||
docker: Docker = field(default_factory=Docker) | ||
|
||
async def force_container_cleanup(self, node_id: NodeID) -> None: | ||
# compose all possible used container prefixes | ||
proxy_prefix = f"{DYNAMIC_PROXY_SERVICE_PREFIX}_{node_id}" | ||
dy_sidecar_prefix = f"{DYNAMIC_SIDECAR_SERVICE_PREFIX}_{node_id}" | ||
user_service_prefix = f"{DYNAMIC_SIDECAR_SERVICE_PREFIX}-{node_id}" | ||
|
||
orphan_containers = await get_containers_with_prefixes( | ||
self.docker, {proxy_prefix, dy_sidecar_prefix, user_service_prefix} | ||
) | ||
_logger.debug( | ||
"Detected orphan containers for node_id='%s': %s", | ||
node_id, | ||
orphan_containers, | ||
) | ||
|
||
unexpected_orphans = { | ||
orphan | ||
for orphan in orphan_containers | ||
if orphan.startswith(user_service_prefix) | ||
} | ||
if unexpected_orphans: | ||
_logger.warning( | ||
"Unexpected orphans detected for node_id='%s': %s", | ||
node_id, | ||
unexpected_orphans, | ||
) | ||
|
||
# avoids parallel requests to docker engine | ||
for container in orphan_containers: | ||
await remove_container_forcefully(self.docker, container) | ||
|
||
async def shutdown(self) -> None: | ||
await self.docker.close() | ||
|
||
|
||
def get_containers_manager(app: FastAPI) -> ContainersManager: | ||
return ContainersManager.get_from_app_state(app) | ||
|
||
|
||
def setup_containers_manager(app: FastAPI) -> None: | ||
async def _on_startup() -> None: | ||
ContainersManager().set_to_app_state(app) | ||
|
||
async def _on_shutdown() -> None: | ||
await ContainersManager.get_from_app_state(app).shutdown() | ||
|
||
app.add_event_handler("startup", _on_startup) | ||
app.add_event_handler("shutdown", _on_shutdown) |
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.