Skip to content

Commit

Permalink
feat: add time_remaining_secs property to MIGRATING event data (#940
Browse files Browse the repository at this point in the history
)

The Apify platform now sends a `timeRemainingSecs` property in the
`MIGRATING` event data, this adds it to the event data model.
  • Loading branch information
fnesveda authored Jan 29, 2025
1 parent ae0d328 commit b44501b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/crawlee/_utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def _timedelta_to_ms(td: timedelta | None) -> float | None:
return int(round(td.total_seconds() * 1000))


def _timedelta_to_secs(td: timedelta | None) -> float | None:
if td == timedelta.max:
return float('inf')
if td is None:
return td
return td.total_seconds()


_number_parser = TypeAdapter(float)


Expand All @@ -35,4 +43,23 @@ def _timedelta_from_ms(value: float | timedelta | Any | None, handler: Callable[
return timedelta(milliseconds=value)


def _timedelta_from_secs(
value: float | timedelta | Any | None,
handler: Callable[[Any], timedelta],
) -> timedelta | None:
if value == float('inf'):
return timedelta.max

# If the value is a string-encoded number, decode it
if isinstance(value, str):
with suppress(ValidationError):
value = _number_parser.validate_python(value)

if not isinstance(value, (int, float)):
return handler(value)

return timedelta(seconds=value)


timedelta_ms = Annotated[timedelta, PlainSerializer(_timedelta_to_ms), WrapValidator(_timedelta_from_ms)]
timedelta_secs = Annotated[timedelta, PlainSerializer(_timedelta_to_secs), WrapValidator(_timedelta_from_secs)]
5 changes: 5 additions & 0 deletions src/crawlee/events/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pydantic import BaseModel, ConfigDict, Field

from crawlee._utils.docs import docs_group
from crawlee._utils.models import timedelta_secs
from crawlee._utils.system import CpuInfo, MemoryUsageInfo


Expand Down Expand Up @@ -59,6 +60,10 @@ class EventMigratingData(BaseModel):

model_config = ConfigDict(populate_by_name=True)

# The remaining time in seconds before the migration is forced and the process is killed
# Optional because it's not present when the event handler is called manually
time_remaining: Annotated[timedelta_secs | None, Field(alias='timeRemainingSecs')] = None


@docs_group('Event payloads')
class EventAbortingData(BaseModel):
Expand Down

0 comments on commit b44501b

Please sign in to comment.