Skip to content

Commit fc84b6e

Browse files
authored
Is1747/remove unused volumes (#1750)
sidecar shows on which host it runs deletes mounted volumes after use gracefully stops
1 parent 4fbb9c1 commit fc84b6e

File tree

12 files changed

+195
-67
lines changed

12 files changed

+195
-67
lines changed

services/docker-compose.devel.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ services:
4848
- ./sidecar:/devel/services/sidecar
4949
- ./storage/client-sdk:/devel/services/storage/client-sdk
5050
- ../packages:/devel/packages
51+
- /etc/hostname:/home/scu/hostname:ro
5152
environment:
5253
# force to start as cpu mode otherwise it will boot in gpu mode in development
5354
- START_AS_MODE_CPU=1
@@ -81,6 +82,7 @@ services:
8182
- ./sidecar:/devel/services/sidecar
8283
- ./storage/client-sdk:/devel/services/storage/client-sdk
8384
- ../packages:/devel/packages
85+
- /etc/hostname:/home/scu/hostname:ro
8486
ports:
8587
- "3007:3000"
8688
environment:
@@ -135,6 +137,7 @@ services:
135137
- ./sidecar:/devel/services/sidecar
136138
- ./storage/client-sdk:/devel/services/storage/client-sdk
137139
- ../packages:/devel/packages
140+
- /etc/hostname:/home/scu/hostname:ro
138141
ports:
139142
- "3008:3000"
140143
environment:

services/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ services:
149149
- output:/home/scu/output
150150
- log:/home/scu/log
151151
- /var/run/docker.sock:/var/run/docker.sock
152+
- /etc/hostname:/home/scu/hostname:ro
152153
environment:
153154
- RABBIT_HOST=${RABBIT_HOST}
154155
- RABBIT_PORT=${RABBIT_PORT}

services/sidecar/Dockerfile

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,44 @@ FROM python:${PYTHON_VERSION}-slim-buster as base
1010

1111
LABEL maintainer=mguidon
1212

13+
RUN set -eux; \
14+
apt-get update; \
15+
apt-get install -y gosu; \
16+
rm -rf /var/lib/apt/lists/*; \
17+
# verify that the binary works
18+
gosu nobody true
19+
1320
# simcore-user uid=8004(scu) gid=8004(scu) groups=8004(scu)
1421
ENV SC_USER_ID=8004 \
15-
SC_USER_NAME=scu \
16-
SC_BUILD_TARGET=base \
17-
SC_BOOT_MODE=default
22+
SC_USER_NAME=scu \
23+
SC_BUILD_TARGET=base \
24+
SC_BOOT_MODE=default
1825

1926
RUN adduser \
20-
--uid ${SC_USER_ID} \
21-
--disabled-password \
22-
--gecos "" \
23-
--shell /bin/sh \
24-
--home /home/${SC_USER_NAME} \
25-
${SC_USER_NAME}
27+
--uid ${SC_USER_ID} \
28+
--disabled-password \
29+
--gecos "" \
30+
--shell /bin/sh \
31+
--home /home/${SC_USER_NAME} \
32+
${SC_USER_NAME}
2633

2734
# Sets utf-8 encoding for Python et al
2835
ENV LANG=C.UTF-8
2936
# Turns off writing .pyc files; superfluous on an ephemeral container.
3037
ENV PYTHONDONTWRITEBYTECODE=1 \
31-
VIRTUAL_ENV=/home/scu/.venv
38+
VIRTUAL_ENV=/home/scu/.venv
3239
# Ensures that the python and pip executables used
3340
# in the image will be those from our virtualenv.
3441
ENV PATH="${VIRTUAL_ENV}/bin:$PATH"
3542

3643
# environment variables
3744
ENV SWARM_STACK_NAME=""\
38-
SIDECAR_SERVICES_MAX_NANO_CPUS=4000000000 \
39-
SIDECAR_SERVICES_MAX_MEMORY_BYTES=2147483648 \
40-
SIDECAR_SERVICES_TIMEOUT_SECONDS=1200 \
41-
SIDECAR_INPUT_FOLDER=/home/scu/input \
42-
SIDECAR_OUTPUT_FOLDER=/home/scu/output \
43-
SIDECAR_LOG_FOLDER=/home/scu/log
45+
SIDECAR_SERVICES_MAX_NANO_CPUS=4000000000 \
46+
SIDECAR_SERVICES_MAX_MEMORY_BYTES=2147483648 \
47+
SIDECAR_SERVICES_TIMEOUT_SECONDS=1200 \
48+
SIDECAR_INPUT_FOLDER=/home/scu/input \
49+
SIDECAR_OUTPUT_FOLDER=/home/scu/output \
50+
SIDECAR_LOG_FOLDER=/home/scu/log
4451

4552
EXPOSE 8080
4653

@@ -54,16 +61,16 @@ FROM base as build
5461
ENV SC_BUILD_TARGET=build
5562

5663
RUN apt-get update &&\
57-
apt-get install -y --no-install-recommends \
58-
build-essential
64+
apt-get install -y --no-install-recommends \
65+
build-essential
5966

6067
# NOTE: python virtualenv is used here such that installed packages may be moved to production image easily by copying the venv
6168
RUN python -m venv ${VIRTUAL_ENV}
6269

6370
RUN pip --no-cache-dir install --upgrade \
64-
pip~=20.2.2 \
65-
wheel \
66-
setuptools
71+
pip~=20.2.2 \
72+
wheel \
73+
setuptools
6774

6875
# copy sidecar and dependencies
6976
COPY --chown=scu:scu packages /build/packages
@@ -95,7 +102,7 @@ RUN pip --no-cache-dir install -r requirements/prod.txt
95102
FROM base as production
96103

97104
ENV SC_BUILD_TARGET=production \
98-
SC_BOOT_MODE=production
105+
SC_BOOT_MODE=production
99106
ENV PYTHONOPTIMIZE=TRUE
100107

101108
WORKDIR /home/scu

services/sidecar/docker/boot.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@ then
2222
pip install --no-cache-dir -r requirements/dev.txt
2323
cd - || exit 1
2424
echo "$INFO" "PIP :"
25-
pip list | sed 's/^/ /'
25+
pip list | sed 's/^/ /'
2626
fi
2727

2828
# RUNNING application ----------------------------------------
2929

3030
# default
3131
CONCURRENCY=1
3232
POOL=prefork
33-
3433
if [ "${SC_BOOT_MODE}" = "debug-ptvsd" ]
3534
then
3635
# NOTE: in this case, remote debugging is only available in development mode!
3736
# FIXME: workaround since PTVSD does not support prefork subprocess debugging: https://github.com/microsoft/ptvsd/issues/943
3837
POOL=solo
39-
watchmedo auto-restart --recursive --pattern="*.py" -- \
38+
exec watchmedo auto-restart --recursive --pattern="*.py" -- \
4039
celery worker \
4140
--app simcore_service_sidecar.celery:app \
4241
--concurrency ${CONCURRENCY} \
@@ -49,5 +48,3 @@ else
4948
--loglevel="${SIDECAR_LOGLEVEL-WARNING}" \
5049
--pool=${POOL}
5150
fi
52-
53-

services/sidecar/docker/entrypoint.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ then
5656

5757
echo "changing $SC_USER_NAME:$SC_USER_NAME ($SC_USER_ID:$SC_USER_ID) to $SC_USER_NAME:$CONT_GROUPNAME ($HOST_USERID:$HOST_GROUPID)"
5858
usermod --uid "$HOST_USERID" --gid "$HOST_GROUPID" "$SC_USER_NAME"
59-
59+
6060
echo "Changing group properties of files around from $SC_USER_ID to group $CONT_GROUPNAME"
6161
find / -path /proc -prune -o -group "$SC_USER_ID" -exec chgrp --no-dereference "$CONT_GROUPNAME" {} \;
6262
# change user property of files already around
@@ -100,8 +100,8 @@ chown -R $USERNAME:"$GROUPNAME" "${SIDECAR_LOG_FOLDER}"
100100
echo "$INFO Starting $* ..."
101101
echo " $SC_USER_NAME rights : $(id "$SC_USER_NAME")"
102102
echo " local dir : $(ls -al)"
103-
echo " input dir : $(ls -al "${SIDECAR_INPUT_FOLDER}")"
103+
echo " input dir : $(ls -al "${SIDECAR_INPUT_FOLDER}")"
104104
echo " output dir : $(ls -al "${SIDECAR_OUTPUT_FOLDER}")"
105105
echo " log dir : $(ls -al "${SIDECAR_LOG_FOLDER}")"
106106

107-
su --command "$*" "$SC_USER_NAME"
107+
exec gosu "$SC_USER_NAME" "$@"
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
from .remote_debug import setup_remote_debugging
1+
import asyncio
2+
3+
from celery.signals import worker_shutting_down
4+
25
from .celery_configurator import get_rabbitmq_config_and_celery_app
6+
from .celery_log_setup import get_task_logger
7+
from .cli import run_sidecar
8+
from .remote_debug import setup_remote_debugging
39

410
setup_remote_debugging()
511

612
rabbit_config, app = get_rabbitmq_config_and_celery_app()
713

14+
log = get_task_logger(__name__)
15+
16+
17+
@worker_shutting_down.connect
18+
def worker_shutting_down_handler(
19+
# pylint: disable=unused-argument
20+
sig,
21+
how,
22+
exitcode,
23+
**kwargs
24+
):
25+
# NOTE: this function shall be adapted when we switch to python 3.7+
26+
log.info("detected worker_shutting_down signal(%s, %s, %s)", sig, how, exitcode)
27+
tasks = asyncio.Task.all_tasks()
28+
for task in tasks:
29+
# pylint: disable=protected-access
30+
if task._coro.__name__ == run_sidecar.__name__:
31+
log.warning("canceling task....................")
32+
task.cancel()
33+
34+
835
__all__ = ["rabbit_config", "app"]

services/sidecar/src/simcore_service_sidecar/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import asyncio
12
import logging
23
from typing import List, Optional, Tuple
34

45
import click
56

6-
from .config import RABBIT_CONFIG
7+
from .config import RABBIT_CONFIG, SIDECAR_LOGLEVEL
78
from .core import inspect
89
from .db import DBContextManager
910
from .rabbitmq import RabbitMQ
@@ -58,6 +59,11 @@ async def run_sidecar(
5859
node_id,
5960
)
6061
return next_task_nodes, None
62+
except asyncio.CancelledError as e:
63+
log.warning(
64+
"Run cancelled", exc_info=True, stack_info=SIDECAR_LOGLEVEL == logging.DEBUG
65+
)
66+
return None, f"Run cancelled: {str(e)}"
6167
except Exception as e: # pylint: disable=broad-except
6268
error_message = f"run_sidecar caught the following exception: {str(e)}"
6369
log.exception(error_message)

services/sidecar/src/simcore_service_sidecar/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
SIDECAR_DOCKER_VOLUME_LOG: str = os.environ.get(
3838
"SIDECAR_DOCKER_VOLUME_LOG", f"{SWARM_STACK_NAME}_log"
3939
)
40+
41+
SIDECAR_HOST_HOSTNAME_PATH: Path = Path(
42+
os.environ.get("SIDECAR_HOST_HOSTNAME_PATH", Path.home() / "hostname")
43+
)
44+
4045
SIDECAR_LOGLEVEL: str = getattr(
4146
logging, os.environ.get("SIDECAR_LOGLEVEL", "WARNING").upper(), logging.WARNING
4247
)

services/sidecar/src/simcore_service_sidecar/core.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33
from typing import Dict, List, Optional, Union
44

5-
import aiodocker
5+
import asyncio
66
import networkx as nx
77
from aiopg.sa import Engine, SAConnection
88
from aiopg.sa.result import RowProxy
@@ -97,7 +97,8 @@ async def _try_get_task_from_db(
9797
comp_tasks.update()
9898
.where(
9999
and_(
100-
comp_tasks.c.node_id == node_id, comp_tasks.c.project_id == project_id,
100+
comp_tasks.c.node_id == node_id,
101+
comp_tasks.c.project_id == project_id,
101102
)
102103
)
103104
.values(job_id=job_request_id, state=RUNNING, start=datetime.utcnow())
@@ -114,7 +115,8 @@ async def _try_get_task_from_db(
114115

115116

116117
async def _get_pipeline_from_db(
117-
db_connection: SAConnection, project_id: str,
118+
db_connection: SAConnection,
119+
project_id: str,
118120
) -> RowProxy:
119121
# get the pipeline
120122
result = await db_connection.execute(
@@ -174,12 +176,19 @@ async def inspect(
174176
run_result = SUCCESS
175177
next_task_nodes = []
176178
try:
177-
sidecar = Executor(
178-
db_engine=db_engine, rabbit_mq=rabbit_mq, task=task, user_id=user_id,
179+
executor = Executor(
180+
db_engine=db_engine,
181+
rabbit_mq=rabbit_mq,
182+
task=task,
183+
user_id=user_id,
179184
)
180-
await sidecar.run()
185+
await executor.run()
181186
next_task_nodes = list(graph.successors(node_id))
182-
except (aiodocker.exceptions.DockerError, exceptions.SidecarException):
187+
except asyncio.CancelledError:
188+
run_result = FAILED
189+
log.warning("Task has been cancelled")
190+
raise
191+
except exceptions.SidecarException:
183192
run_result = FAILED
184193
log.exception("Error during execution")
185194

0 commit comments

Comments
 (0)