Skip to content

Commit 7d954f4

Browse files
GitHKAndrei Neagu
andauthored
adds container cleanup before starting and after closing (ITISFoundation#3153)
Co-authored-by: Andrei Neagu <[email protected]>
1 parent ec774ac commit 7d954f4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/api/containers.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
from ..core.docker_utils import docker_client
3131
from ..core.rabbitmq import RabbitMQ
3232
from ..core.settings import DynamicSidecarSettings
33-
from ..core.shared_handlers import remove_the_compose_spec, write_file_and_run_command
33+
from ..core.shared_handlers import (
34+
cleanup_containers_and_volumes,
35+
remove_the_compose_spec,
36+
write_file_and_run_command,
37+
)
3438
from ..core.utils import assemble_container_names
3539
from ..core.validation import (
3640
InvalidComposeSpec,
@@ -61,11 +65,14 @@ async def _task_docker_compose_up(
6165
) -> None:
6266
# building is a security risk hence is disabled via "--no-build" parameter
6367
await send_message(rabbitmq, "starting service containers")
64-
command = (
65-
"docker-compose --project-name {project} --file {file_path} "
66-
"up --no-build --detach"
67-
)
68+
6869
with directory_watcher_disabled(app):
70+
await cleanup_containers_and_volumes(shared_store, settings)
71+
72+
command = (
73+
"docker-compose --project-name {project} --file {file_path} "
74+
"up --no-build --detach"
75+
)
6976
finished_without_errors, stdout = await write_file_and_run_command(
7077
settings=settings,
7178
file_content=shared_store.compose_spec,

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/core/shared_handlers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010
logger = logging.getLogger(__name__)
1111

1212

13+
async def cleanup_containers_and_volumes(
14+
shared_store: SharedStore, settings: DynamicSidecarSettings
15+
) -> None:
16+
cleanup_command = (
17+
"docker-compose --project-name {project} --file {file_path} rm --force -v"
18+
)
19+
finished_without_errors, stdout = await write_file_and_run_command(
20+
settings=settings,
21+
file_content=shared_store.compose_spec,
22+
command=cleanup_command,
23+
command_timeout=None,
24+
)
25+
if not finished_without_errors:
26+
logger.warning(
27+
"Unexpected error while running command\n%s:\n%s", cleanup_command, stdout
28+
)
29+
30+
1331
async def write_file_and_run_command(
1432
settings: DynamicSidecarSettings,
1533
file_content: Optional[str],
@@ -37,6 +55,8 @@ async def remove_the_compose_spec(
3755
if stored_compose_content is None:
3856
return True, "No started spec to remove was found"
3957

58+
await cleanup_containers_and_volumes(shared_store, settings)
59+
4060
command = (
4161
"docker-compose -p {project} -f {file_path} "
4262
"down --volumes --remove-orphans -t {stop_and_remove_timeout}"

0 commit comments

Comments
 (0)