forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 EFS Guardian: adding size monitoring (ITISFoundation#6502)
- Loading branch information
1 parent
9c8d0ec
commit 4d27c32
Showing
17 changed files
with
605 additions
and
27 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
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
18 changes: 18 additions & 0 deletions
18
services/efs-guardian/src/simcore_service_efs_guardian/services/background_tasks.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,18 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
|
||
from ..core.settings import ApplicationSettings | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
async def removal_policy_task(app: FastAPI) -> None: | ||
_logger.info("FAKE Removal policy task started (not yet implemented)") | ||
|
||
# After X days of inactivity remove data from EFS | ||
# Probably use `last_modified_data` in the project DB table | ||
# Maybe lock project during this time lock_project() | ||
|
||
app_settings: ApplicationSettings = app.state.settings | ||
assert app_settings # nosec |
73 changes: 73 additions & 0 deletions
73
services/efs-guardian/src/simcore_service_efs_guardian/services/background_tasks_setup.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,73 @@ | ||
import asyncio | ||
import logging | ||
from collections.abc import Awaitable, Callable | ||
from datetime import timedelta | ||
from typing import TypedDict | ||
|
||
from fastapi import FastAPI | ||
from servicelib.background_task import stop_periodic_task | ||
from servicelib.logging_utils import log_catch, log_context | ||
from servicelib.redis_utils import start_exclusive_periodic_task | ||
|
||
from .background_tasks import removal_policy_task | ||
from .modules.redis import get_redis_lock_client | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class EfsGuardianBackgroundTask(TypedDict): | ||
name: str | ||
task_func: Callable | ||
|
||
|
||
_EFS_GUARDIAN_BACKGROUND_TASKS = [ | ||
EfsGuardianBackgroundTask( | ||
name="efs_removal_policy_task", task_func=removal_policy_task | ||
) | ||
] | ||
|
||
|
||
def _on_app_startup(app: FastAPI) -> Callable[[], Awaitable[None]]: | ||
async def _startup() -> None: | ||
with log_context( | ||
_logger, logging.INFO, msg="Efs Guardian startup.." | ||
), log_catch(_logger, reraise=False): | ||
app.state.efs_guardian_background_tasks = [] | ||
|
||
# Setup periodic tasks | ||
for task in _EFS_GUARDIAN_BACKGROUND_TASKS: | ||
exclusive_task = start_exclusive_periodic_task( | ||
get_redis_lock_client(app), | ||
task["task_func"], | ||
task_period=timedelta(seconds=60), # 1 minute | ||
retry_after=timedelta(seconds=300), # 5 minutes | ||
task_name=task["name"], | ||
app=app, | ||
) | ||
app.state.efs_guardian_background_tasks.append(exclusive_task) | ||
|
||
return _startup | ||
|
||
|
||
def _on_app_shutdown( | ||
_app: FastAPI, | ||
) -> Callable[[], Awaitable[None]]: | ||
async def _stop() -> None: | ||
with log_context( | ||
_logger, logging.INFO, msg="Efs Guardian shutdown.." | ||
), log_catch(_logger, reraise=False): | ||
assert _app # nosec | ||
if _app.state.efs_guardian_background_tasks: | ||
await asyncio.gather( | ||
*[ | ||
stop_periodic_task(task) | ||
for task in _app.state.efs_guardian_background_tasks | ||
] | ||
) | ||
|
||
return _stop | ||
|
||
|
||
def setup(app: FastAPI) -> None: | ||
app.add_event_handler("startup", _on_app_startup(app)) | ||
app.add_event_handler("shutdown", _on_app_shutdown(app)) |
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
46 changes: 46 additions & 0 deletions
46
services/efs-guardian/src/simcore_service_efs_guardian/services/efs_manager_utils.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,46 @@ | ||
import asyncio | ||
import logging | ||
|
||
from pydantic import ByteSize | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
async def get_size_bash_async(path) -> ByteSize: | ||
# Create the subprocess | ||
command = ["du", "-sb", path] | ||
process = await asyncio.create_subprocess_exec( | ||
*command, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
|
||
# Wait for the subprocess to complete | ||
stdout, stderr = await process.communicate() | ||
|
||
if process.returncode == 0: | ||
# Parse the output | ||
size = ByteSize(stdout.decode().split()[0]) | ||
return size | ||
msg = f"Command {' '.join(command)} failed with error code {process.returncode}: {stderr.decode()}" | ||
_logger.error(msg) | ||
raise RuntimeError(msg) | ||
|
||
|
||
async def remove_write_permissions_bash_async(path) -> None: | ||
# Create the subprocess | ||
command = ["chmod", "-R", "a-w", path] | ||
process = await asyncio.create_subprocess_exec( | ||
*command, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
|
||
# Wait for the subprocess to complete | ||
_, stderr = await process.communicate() | ||
|
||
if process.returncode == 0: | ||
return | ||
msg = f"Command {' '.join(command)} failed with error code {process.returncode}: {stderr.decode()}" | ||
_logger.error(msg) | ||
raise RuntimeError(msg) |
29 changes: 29 additions & 0 deletions
29
services/efs-guardian/src/simcore_service_efs_guardian/services/modules/redis.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,29 @@ | ||
import logging | ||
from typing import cast | ||
|
||
from fastapi import FastAPI | ||
from servicelib.redis import RedisClientSDK | ||
from settings_library.redis import RedisDatabase, RedisSettings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup(app: FastAPI) -> None: | ||
async def on_startup() -> None: | ||
app.state.redis_lock_client_sdk = None | ||
settings: RedisSettings = app.state.settings.EFS_GUARDIAN_REDIS | ||
redis_locks_dsn = settings.build_redis_dsn(RedisDatabase.LOCKS) | ||
app.state.redis_lock_client_sdk = lock_client = RedisClientSDK(redis_locks_dsn) | ||
await lock_client.setup() | ||
|
||
async def on_shutdown() -> None: | ||
redis_lock_client_sdk: None | RedisClientSDK = app.state.redis_lock_client_sdk | ||
if redis_lock_client_sdk: | ||
await redis_lock_client_sdk.shutdown() | ||
|
||
app.add_event_handler("startup", on_startup) | ||
app.add_event_handler("shutdown", on_shutdown) | ||
|
||
|
||
def get_redis_lock_client(app: FastAPI) -> RedisClientSDK: | ||
return cast(RedisClientSDK, app.state.redis_lock_client_sdk) |
Oops, something went wrong.