Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from pydantic import TypeAdapter, field_validator


def timedelta_try_convert_str_to_float(field: str):
def validate_numeric_string_as_timedelta(field: str):
"""Transforms a float/int number into a valid datetime as it used to work in the past"""

def _try_convert_str_to_float_or_return(
def _numeric_string_as_timedelta(
v: datetime.timedelta | str | float,
) -> datetime.timedelta | str | float:
if isinstance(v, str):
Expand All @@ -32,4 +32,4 @@ def _try_convert_str_to_float_or_return(
return v
return v

return field_validator(field, mode="before")(_try_convert_str_to_float_or_return)
return field_validator(field, mode="before")(_numeric_string_as_timedelta)
4 changes: 2 additions & 2 deletions packages/common-library/tests/test_pydantic_validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta

import pytest
from common_library.pydantic_validators import timedelta_try_convert_str_to_float
from common_library.pydantic_validators import validate_numeric_string_as_timedelta
from faker import Faker
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
Expand All @@ -15,7 +15,7 @@ class Settings(BaseSettings):
APP_NAME: str
REQUEST_TIMEOUT: timedelta = Field(default=timedelta(seconds=40))

_try_convert_request_timeout = timedelta_try_convert_str_to_float(
_validate_request_timeout = validate_numeric_string_as_timedelta(
"REQUEST_TIMEOUT"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class NodeGet(OutputSchema):
"service_basepath": "/x/E1O2E-LAH",
"service_state": "pending",
"service_message": "no suitable node (insufficient resources on 1 node)",
"user_id": 123,
"user_id": "123",
},
# dynamic
{
Expand All @@ -120,7 +120,7 @@ class NodeGet(OutputSchema):
"service_basepath": "/x/E1O2E-LAH",
"service_state": "pending",
"service_message": "no suitable node (insufficient resources on 1 node)",
"user_id": 123,
"user_id": "123",
},
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@


async def http_exception_as_json_response(
request: Request, exc: HTTPException
request: Request, exc: Exception
) -> JSONResponse:
assert isinstance(exc, HTTPException) # nosec
assert request # nosec

error = DefaultApiError.from_status_code(exc.status_code)

error_detail = error.detail or ""
Expand Down
12 changes: 6 additions & 6 deletions services/agent/src/simcore_service_agent/core/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import timedelta

from common_library.pydantic_validators import timedelta_try_convert_str_to_float
from common_library.pydantic_validators import validate_numeric_string_as_timedelta
from models_library.basic_types import BootModeEnum, LogLevel
from pydantic import AliasChoices, AnyHttpUrl, Field, field_validator
from settings_library.base import BaseCustomSettings
Expand Down Expand Up @@ -76,17 +76,17 @@ class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
auto_default_from_env=True, description="settings for service/rabbitmq"
)

_try_convert_agent_volumes_cleanup_interval = timedelta_try_convert_str_to_float(
_validate_agent_volumes_cleanup_interval = validate_numeric_string_as_timedelta(
"AGENT_VOLUMES_CLEANUP_INTERVAL"
)

_try_convert_agent_volumes_cleanup_book_keeping_interval = (
timedelta_try_convert_str_to_float(
_validate_agent_volumes_cleanup_book_keeping_interval = (
validate_numeric_string_as_timedelta(
"AGENT_VOLUMES_CLEANUP_BOOK_KEEPING_INTERVAL"
)
)
_try_convert_agent_volumes_cleanup_remove_volumes_inactive_for = (
timedelta_try_convert_str_to_float(
_validate_agent_volumes_cleanup_remove_volumes_inactive_for = (
validate_numeric_string_as_timedelta(
"AGENT_VOLUMES_CLEANUP_REMOVE_VOLUMES_INACTIVE_FOR"
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
from typing import Final

from common_library.pydantic_validators import timedelta_try_convert_str_to_float
from common_library.pydantic_validators import validate_numeric_string_as_timedelta
from models_library.projects_networks import DockerNetworkName
from pydantic import Field, NonNegativeInt, PositiveFloat
from settings_library.base import BaseCustomSettings
Expand Down Expand Up @@ -168,11 +168,11 @@ class DynamicServicesSchedulerSettings(BaseCustomSettings):
timedelta(0), description="time to sleep before removing a container"
)

_try_convert_director_v2_dynamic_scheduler_interval = (
timedelta_try_convert_str_to_float("DIRECTOR_V2_DYNAMIC_SCHEDULER_INTERVAL")
_validate_director_v2_dynamic_scheduler_interval = (
validate_numeric_string_as_timedelta("DIRECTOR_V2_DYNAMIC_SCHEDULER_INTERVAL")
)
_try_convert_director_v2_dynamic_sidecar_sleep_after_container_removal = (
timedelta_try_convert_str_to_float(
_validate_director_v2_dynamic_sidecar_sleep_after_container_removal = (
validate_numeric_string_as_timedelta(
"DIRECTOR_V2_DYNAMIC_SIDECAR_SLEEP_AFTER_CONTAINER_REMOVAL"
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import datetime
from functools import cached_property

from common_library.pydantic_validators import timedelta_try_convert_str_to_float
from common_library.pydantic_validators import validate_numeric_string_as_timedelta
from models_library.basic_types import (
BootModeEnum,
BuildTargetEnum,
Expand Down Expand Up @@ -236,6 +236,6 @@ def _validate_loglevel(cls, value: str) -> str:
log_level: str = cls.validate_log_level(value)
return log_level

_try_convert_service_tracking_heartbeat = timedelta_try_convert_str_to_float(
_validate_service_tracking_heartbeat = validate_numeric_string_as_timedelta(
"SERVICE_TRACKING_HEARTBEAT"
)
Loading
Loading