Skip to content
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

🐛 observation cycle cancellation #3771

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import functools
import logging
from asyncio import sleep
from contextlib import suppress
from dataclasses import dataclass
from typing import Optional
from typing import Optional, Union

from models_library.basic_types import PortInt
from models_library.projects import ProjectID
Expand Down Expand Up @@ -248,6 +249,34 @@ async def mark_service_for_removal(
current.dynamic_sidecar.service_removal_state.mark_to_remove(can_save)
await update_scheduler_data_label(current)

# cancel current observation task
if service_name in self._service_observation_task:
service_task: Optional[
Union[asyncio.Task, object]
GitHK marked this conversation as resolved.
Show resolved Hide resolved
] = self._service_observation_task[service_name]
if isinstance(service_task, asyncio.Task):
service_task.cancel()

async def _wait_task(task: asyncio.Task) -> None:
GitHK marked this conversation as resolved.
Show resolved Hide resolved
await task

with suppress(asyncio.CancelledError):
try:
await asyncio.wait_for(_wait_task(service_task), timeout=10)
pcrespov marked this conversation as resolved.
Show resolved Hide resolved
except asyncio.TimeoutError:
pass
GitHK marked this conversation as resolved.
Show resolved Hide resolved

# recreate new observation
dynamic_sidecar_settings: DynamicSidecarSettings = (
self.app.state.settings.DYNAMIC_SERVICES.DYNAMIC_SIDECAR
)
dynamic_scheduler: DynamicServicesSchedulerSettings = (
self.app.state.settings.DYNAMIC_SERVICES.DYNAMIC_SCHEDULER
)
self._service_observation_task[service_name] = self.__get_observation_task(
dynamic_sidecar_settings, dynamic_scheduler, service_name
)

logger.debug("Service '%s' marked for removal from scheduler", service_name)

async def remove_service_from_observation(self, node_uuid: NodeID) -> None:
Expand Down Expand Up @@ -430,6 +459,32 @@ async def restart_containers(self, node_uuid: NodeID) -> None:
def _enqueue_observation_from_service_name(self, service_name: str) -> None:
self._trigger_observation_queue.put_nowait(service_name)

def __get_observation_task(
self,
dynamic_sidecar_settings: DynamicSidecarSettings,
dynamic_scheduler: DynamicServicesSchedulerSettings,
service_name: str,
) -> asyncio.Task:
scheduler_data: SchedulerData = self._to_observe[service_name]
observation_task = asyncio.create_task(
observing_single_service(
scheduler=self,
service_name=service_name,
scheduler_data=scheduler_data,
dynamic_sidecar_settings=dynamic_sidecar_settings,
dynamic_scheduler=dynamic_scheduler,
),
name=f"observe_{service_name}",
GitHK marked this conversation as resolved.
Show resolved Hide resolved
)
observation_task.add_done_callback(
functools.partial(
lambda s, _: self._service_observation_task.pop(s, None),
service_name,
)
)
logger.debug("created %s for %s", f"{observation_task=}", f"{service_name=}")
return observation_task

async def _run_trigger_observation_queue_task(self) -> None:
"""generates events at regular time interval"""
dynamic_sidecar_settings: DynamicSidecarSettings = (
Expand All @@ -450,27 +505,10 @@ async def _run_trigger_observation_queue_task(self) -> None:
continue

if self._service_observation_task.get(service_name) is None:
scheduler_data: SchedulerData = self._to_observe[service_name]
self._service_observation_task[
service_name
] = observation_task = asyncio.create_task(
observing_single_service(
scheduler=self,
service_name=service_name,
scheduler_data=scheduler_data,
dynamic_sidecar_settings=dynamic_sidecar_settings,
dynamic_scheduler=dynamic_scheduler,
),
name=f"observe_{service_name}",
)
observation_task.add_done_callback(
functools.partial(
lambda s, _: self._service_observation_task.pop(s, None),
service_name,
)
)
logger.debug(
"created %s for %s", f"{observation_task=}", f"{service_name=}"
] = self.__get_observation_task(
dynamic_sidecar_settings, dynamic_scheduler, service_name
)

logger.info("Scheduler 'trigger observation queue task' was shut down")
Expand Down
2 changes: 1 addition & 1 deletion services/director-v2/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def mock_env(


@pytest.fixture(scope="function")
def client(mock_env: EnvVarsDict) -> Iterable[TestClient]:
async def client(mock_env: EnvVarsDict) -> Iterable[TestClient]:
GitHK marked this conversation as resolved.
Show resolved Hide resolved
settings = AppSettings.create_from_envs()
app = init_app(settings)
print("Application settings\n", settings.json(indent=2))
Expand Down