Skip to content

Commit 6642e80

Browse files
improvements
1 parent ca3035c commit 6642e80

File tree

5 files changed

+91
-46
lines changed

5 files changed

+91
-46
lines changed

services/efs-guardian/src/simcore_service_efs_guardian/services/background_tasks_setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async def _startup() -> None:
4040
get_redis_lock_client(app),
4141
task["task_func"],
4242
task_period=timedelta(seconds=60), # 1 minute
43-
retry_after=timedelta(seconds=60), # 5 minutes
43+
retry_after=timedelta(seconds=300), # 5 minutes
4444
task_name=task["name"],
4545
app=app,
4646
)

services/efs-guardian/src/simcore_service_efs_guardian/services/efs_manager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ async def create_project_specific_data_dir(
5555
) # This gives rwx permissions to user and group, and nothing to others
5656
return _dir_path
5757

58+
async def check_project_node_data_directory_exits(
59+
self, project_id: ProjectID, node_id: NodeID
60+
) -> bool:
61+
_dir_path = (
62+
self._efs_mounted_path
63+
/ self._project_specific_data_base_directory
64+
/ f"{project_id}"
65+
/ f"{node_id}"
66+
)
67+
68+
return _dir_path.exists()
69+
5870
async def get_project_node_data_size(
5971
self, project_id: ProjectID, node_id: NodeID
6072
) -> ByteSize:
Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
11
import asyncio
2+
import logging
23

34
from pydantic import ByteSize
45

6+
_logger = logging.getLogger(__name__)
7+
58

69
async def get_size_bash_async(path) -> ByteSize:
7-
try:
8-
# Create the subprocess
9-
process = await asyncio.create_subprocess_exec(
10-
"du",
11-
"-sb",
12-
path,
13-
stdout=asyncio.subprocess.PIPE,
14-
stderr=asyncio.subprocess.PIPE,
15-
)
16-
17-
# Wait for the subprocess to complete
18-
stdout, stderr = await process.communicate()
19-
20-
if process.returncode == 0:
21-
# Parse the output
22-
size = ByteSize(stdout.decode().split()[0])
23-
return size
24-
else:
25-
print(f"Error: {stderr.decode()}")
26-
raise ValueError
27-
except Exception as e:
28-
raise e
10+
# Create the subprocess
11+
command = ["du", "-sb", path]
12+
process = await asyncio.create_subprocess_exec(
13+
*command,
14+
stdout=asyncio.subprocess.PIPE,
15+
stderr=asyncio.subprocess.PIPE,
16+
)
17+
18+
# Wait for the subprocess to complete
19+
stdout, stderr = await process.communicate()
20+
21+
if process.returncode == 0:
22+
# Parse the output
23+
size = ByteSize(stdout.decode().split()[0])
24+
return size
25+
else:
26+
msg = f"Command {' '.join(command)} failed with error code {process.returncode}: {stderr.decode()}"
27+
_logger.error(msg)
28+
raise RuntimeError(msg)
2929

3030

3131
async def remove_write_permissions_bash_async(path) -> None:
32-
try:
33-
# Create the subprocess
34-
process = await asyncio.create_subprocess_exec(
35-
"chmod",
36-
"-R",
37-
"a-w",
38-
path,
39-
stdout=asyncio.subprocess.PIPE,
40-
stderr=asyncio.subprocess.PIPE,
41-
)
42-
43-
# Wait for the subprocess to complete
44-
stdout, stderr = await process.communicate()
45-
46-
if process.returncode == 0:
47-
return
48-
else:
49-
print(f"Error: {stderr.decode()}")
50-
raise ValueError
51-
except Exception as e:
52-
raise e
32+
# Create the subprocess
33+
command = ["chmod", "-R", "a-w", path]
34+
process = await asyncio.create_subprocess_exec(
35+
*command,
36+
stdout=asyncio.subprocess.PIPE,
37+
stderr=asyncio.subprocess.PIPE,
38+
)
39+
40+
# Wait for the subprocess to complete
41+
_, stderr = await process.communicate()
42+
43+
if process.returncode == 0:
44+
return
45+
else:
46+
msg = f"Command {' '.join(command)} failed with error code {process.returncode}: {stderr.decode()}"
47+
_logger.error(msg)
48+
raise RuntimeError(msg)

services/efs-guardian/src/simcore_service_efs_guardian/services/process_messages.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,46 @@ async def process_dynamic_service_running_message(app: FastAPI, data: bytes) ->
1717
DynamicServiceRunningMessage, data # type: ignore[arg-type]
1818
)
1919
_logger.debug(
20-
"Process dynamic service running msg, project ID: %s node ID: %s",
20+
"Process dynamic service running msg, project ID: %s node ID: %s, current user: %s",
2121
rabbit_message.project_id,
2222
rabbit_message.node_id,
23+
rabbit_message.user_id,
2324
)
2425

2526
settings = get_application_settings(app)
2627
efs_manager: EfsManager = app.state.efs_manager
28+
29+
dir_exists = await efs_manager.check_project_node_data_directory_exits(
30+
rabbit_message.project_id, node_id=rabbit_message.node_id
31+
)
32+
if dir_exists is False:
33+
_logger.debug(
34+
"Directory doesn't exists in EFS, project ID: %s node ID: %s, current user: %s",
35+
rabbit_message.project_id,
36+
rabbit_message.node_id,
37+
rabbit_message.user_id,
38+
)
39+
return True
40+
2741
size = await efs_manager.get_project_node_data_size(
2842
rabbit_message.project_id, node_id=rabbit_message.node_id
2943
)
44+
_logger.debug(
45+
"Current directory size: %s, project ID: %s node ID: %s, current user: %s",
46+
size,
47+
rabbit_message.project_id,
48+
rabbit_message.node_id,
49+
rabbit_message.user_id,
50+
)
3051

3152
if size > settings.EFS_DEFAULT_USER_SERVICE_SIZE_BYTES:
3253
_logger.warning(
33-
"Removing write permissions inside of EFS starts for project ID: %s, node ID: %s, current user: %s",
54+
"Removing write permissions inside of EFS starts for project ID: %s, node ID: %s, current user: %s, size: %s, upper limit: %s",
3455
rabbit_message.project_id,
3556
rabbit_message.node_id,
3657
rabbit_message.user_id,
58+
size,
59+
settings.EFS_DEFAULT_USER_SERVICE_SIZE_BYTES,
3760
)
3861
redis = get_redis_lock_client(app)
3962
async with redis.lock_context(

services/efs-guardian/tests/unit/test_efs_manager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ async def test_remove_write_access_rights(
104104

105105
efs_manager: EfsManager = app.state.efs_manager
106106

107+
assert (
108+
await efs_manager.check_project_node_data_directory_exits(
109+
project_id=_project_id, node_id=_node_id
110+
)
111+
is False
112+
)
113+
107114
with patch(
108115
"simcore_service_efs_guardian.services.efs_manager.os.chown"
109116
) as mocked_chown:
@@ -113,6 +120,13 @@ async def test_remove_write_access_rights(
113120
storage_directory_name=_storage_directory_name,
114121
)
115122

123+
assert (
124+
await efs_manager.check_project_node_data_directory_exits(
125+
project_id=_project_id, node_id=_node_id
126+
)
127+
is True
128+
)
129+
116130
size_before = await efs_manager.get_project_node_data_size(
117131
project_id=_project_id, node_id=_node_id
118132
)

0 commit comments

Comments
 (0)