Skip to content

Commit 0a26c76

Browse files
pre-validate timedelta
1 parent 95a4874 commit 0a26c76

File tree

1 file changed

+12
-3
lines changed
  • services/clusters-keeper/src/simcore_service_clusters_keeper/core

1 file changed

+12
-3
lines changed

services/clusters-keeper/src/simcore_service_clusters_keeper/core/settings.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
PositiveInt,
2121
SecretStr,
2222
TypeAdapter,
23+
WrapValidator,
2324
field_validator,
2425
)
2526
from pydantic_settings import SettingsConfigDict
@@ -284,13 +285,13 @@ class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
284285
json_schema_extra={"auto_default_from_env": True}
285286
)
286287

287-
CLUSTERS_KEEPER_TASK_INTERVAL: Annotated[datetime.timedelta, BeforeValidator(int)] = Field(
288+
CLUSTERS_KEEPER_TASK_INTERVAL: datetime.timedelta = Field(
288289
default=datetime.timedelta(seconds=30),
289290
description="interval between each clusters clean check "
290291
"(default to seconds, or see https://pydantic-docs.helpmanual.io/usage/types/#datetime-types for string formating)",
291292
)
292293

293-
SERVICE_TRACKING_HEARTBEAT: Annotated[datetime.timedelta, BeforeValidator(int)] = Field(
294+
SERVICE_TRACKING_HEARTBEAT: datetime.timedelta = Field(
294295
default=datetime.timedelta(seconds=60),
295296
description="Service heartbeat interval (everytime a heartbeat is sent into RabbitMQ) "
296297
"(default to seconds, or see https://pydantic-docs.helpmanual.io/usage/types/#datetime-types for string formating)",
@@ -334,8 +335,16 @@ def LOG_LEVEL(self) -> LogLevel: # noqa: N802
334335

335336
@field_validator("CLUSTERS_KEEPER_LOGLEVEL")
336337
@classmethod
337-
def valid_log_level(cls, value: str) -> str:
338+
def _valid_log_level(cls, value: str) -> str:
338339
return cls.validate_log_level(value)
340+
341+
342+
@field_validator("CLUSTERS_KEEPER_TASK_INTERVAL", "SERVICE_TRACKING_HEARTBEAT", mode="before")
343+
@classmethod
344+
def _validate_interval(cls, value: str | datetime.timedelta) -> int | datetime.timedelta:
345+
if isinstance(value, str):
346+
return int(value)
347+
return value
339348

340349

341350
def get_application_settings(app: FastAPI) -> ApplicationSettings:

0 commit comments

Comments
 (0)