Skip to content

Commit b44501b

Browse files
authored
feat: add time_remaining_secs property to MIGRATING event data (#940)
The Apify platform now sends a `timeRemainingSecs` property in the `MIGRATING` event data, this adds it to the event data model.
1 parent ae0d328 commit b44501b

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/crawlee/_utils/models.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def _timedelta_to_ms(td: timedelta | None) -> float | None:
1717
return int(round(td.total_seconds() * 1000))
1818

1919

20+
def _timedelta_to_secs(td: timedelta | None) -> float | None:
21+
if td == timedelta.max:
22+
return float('inf')
23+
if td is None:
24+
return td
25+
return td.total_seconds()
26+
27+
2028
_number_parser = TypeAdapter(float)
2129

2230

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

3745

46+
def _timedelta_from_secs(
47+
value: float | timedelta | Any | None,
48+
handler: Callable[[Any], timedelta],
49+
) -> timedelta | None:
50+
if value == float('inf'):
51+
return timedelta.max
52+
53+
# If the value is a string-encoded number, decode it
54+
if isinstance(value, str):
55+
with suppress(ValidationError):
56+
value = _number_parser.validate_python(value)
57+
58+
if not isinstance(value, (int, float)):
59+
return handler(value)
60+
61+
return timedelta(seconds=value)
62+
63+
3864
timedelta_ms = Annotated[timedelta, PlainSerializer(_timedelta_to_ms), WrapValidator(_timedelta_from_ms)]
65+
timedelta_secs = Annotated[timedelta, PlainSerializer(_timedelta_to_secs), WrapValidator(_timedelta_from_secs)]

src/crawlee/events/_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pydantic import BaseModel, ConfigDict, Field
88

99
from crawlee._utils.docs import docs_group
10+
from crawlee._utils.models import timedelta_secs
1011
from crawlee._utils.system import CpuInfo, MemoryUsageInfo
1112

1213

@@ -59,6 +60,10 @@ class EventMigratingData(BaseModel):
5960

6061
model_config = ConfigDict(populate_by_name=True)
6162

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

6368
@docs_group('Event payloads')
6469
class EventAbortingData(BaseModel):

0 commit comments

Comments
 (0)