-
Notifications
You must be signed in to change notification settings - Fork 32
🎨 EFS Guardian: adding size monitoring #6502
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
Merged
matusdrobuliak66
merged 18 commits into
ITISFoundation:master
from
matusdrobuliak66:improve-efs-2
Oct 10, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e638627
efs improvements
matusdrobuliak66 1ee9895
efs improvements
matusdrobuliak66 5d74505
efs improvements
matusdrobuliak66 b3d43ae
modify Dockerfile
matusdrobuliak66 ee25042
modify Dockerfile
matusdrobuliak66 6372817
Merge branch 'master' into improve-efs-2
matusdrobuliak66 f301fbb
daily work
matusdrobuliak66 f09d4b9
Merge branch 'master' into improve-efs-2
matusdrobuliak66 6a75a26
Merge branch 'master' into improve-efs-2
matusdrobuliak66 d386338
adding tests
matusdrobuliak66 ca3035c
remove queue name
matusdrobuliak66 6642e80
improvements
matusdrobuliak66 7e35e3f
Merge branch 'master' into improve-efs-2
matusdrobuliak66 6980cb2
improvements
matusdrobuliak66 c0abb93
Merge branch 'improve-efs-2' of github.com:matusdrobuliak66/osparc-si…
matusdrobuliak66 a6c55de
improvements
matusdrobuliak66 94952b9
review @pcrespov
matusdrobuliak66 bd5eb5e
review @pcrespov
matusdrobuliak66 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
matusdrobuliak66 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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: | ||
matusdrobuliak66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # 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 hidden or 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) | ||
matusdrobuliak66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.