Repository navigation
feat(events)!: Replace CRAWLER_STATUS with STATUS_MESSAGE - #2542
arsalankhan786777 wants to merge 2 commits into
Conversation
0d3cc94 to
04eca74
Compare
CRAWLER_STATUS with STATUS_MESSAGE with a terminal flag and levelCRAWLER_STATUS with STATUS_MESSAGE
…terminal flag and level Port the generic status message event from Crawlee JS v4. The crawler emits `Event.STATUS_MESSAGE` with `EventStatusMessageData`, which has `crawler_id`, `message`, `is_status_message_terminal` and `level`. The periodic status messages go through it, and when a run finishes the crawler logs and emits a terminal `Finished! Total N requests: X succeeded, Y failed.` message at INFO, so integrations such as the Apify SDK can set it as the final status of the run. BREAKING CHANGE: `Event.CRAWLER_STATUS` and `EventCrawlerStatusData` are replaced by `Event.STATUS_MESSAGE` and `EventStatusMessageData`. The crawler logs a new INFO message `Finished! Total N requests: X succeeded, Y failed.` when a run finishes. Closes apify#2337 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
04eca74 to
9b23ade
Compare
| self._log_status_message(message, level='INFO') | ||
| self._set_status_message(new_message, level='INFO') | ||
| else: | ||
| self._set_status_message(message, level=None) |
There was a problem hiding this comment.
Question: The status_message_callback docstring says that returning None "suppresses the status message". Here the default message is still emitted, so the Apify SDK would still set it as the run status. In Crawlee JS, the crawler emits nothing when a callback is set. Should we drop this emit, or change the docstring to say that only the log line is suppressed?
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Dropped the emit, so the docstring now holds: on None the update is neither logged nor emitted. The terminal message is still emitted with or without a callback. Note that v2 emitted the default message in this case before this PR too, so I added the change to the upgrading guide. level is now a required LogLevel on the event data, since it is never None.
Written with Claude Code (AI).
|
|
||
|
|
||
| async def test_status_message_terminal_on_finish() -> None: | ||
| """The last `STATUS_MESSAGE` event of a run is a terminal summary of the finished requests.""" |
There was a problem hiding this comment.
Suggestion: Please add a test for the callback paths: level='INFO' when the callback returns a message, and level=None when it returns None.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Added test_status_message_emit_with_callback: a returned message is emitted with level='INFO'. Since None no longer emits, that case asserts there is no event.
Written with Claude Code (AI).
|
|
||
| events = [call.args[0] for call in status_message_listener.call_args_list] | ||
| terminal_events = [event for event in events if event.is_status_message_terminal] | ||
| assert terminal_events == [events[-1]] |
There was a problem hiding this comment.
Suggestion: EventManager runs sync listeners through asyncio.to_thread, so the Mock call order is not guaranteed to match the emit order. An async def listener keeps the order fixed. The same applies to call_args_list[0] in test_status_message_emit.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Done, both tests now use an async def listener.
Written with Claude Code (AI).
| # message can follow the terminal one. | ||
| state = self._statistics.state | ||
| total = state.requests_finished + state.requests_failed | ||
| self._set_status_message( |
There was a problem hiding this comment.
Nit: Finished! ... is logged before Error analysis, The crawl was interrupted... and Final request statistics. In Crawlee JS it is the last line. After Ctrl+C, the log says "Finished!" and then "interrupted". Fine to keep if emitting later is not practical.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
I'd keep it here. The Error analysis, interrupted and final statistics lines are logged in run(), after _run_crawler has exited the event manager, where emit raises unless someone else keeps it active. Moving the message there would mean restructuring how the contexts are entered.
Written with Claude Code (AI).
| self, | ||
| message: str, | ||
| *, | ||
| level: LogLevel | None = 'DEBUG', |
There was a problem hiding this comment.
Nit: No caller uses the 'DEBUG' default. level can be a required keyword argument.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Done.
Written with Claude Code (AI).
| message: str | ||
| """A message describing the current status of the crawler.""" | ||
|
|
||
| is_status_message_terminal: bool | None = None |
There was a problem hiding this comment.
Nit: The crawler never emits False here, so bool = False would be simpler for consumers than bool | None. Fine to keep if you want to match the optional JS field.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Done, in the model and in _set_status_message; the upgrading guide says False for periodic messages now.
Written with Claude Code (AI).
| ) | ||
|
|
||
|
|
||
| async def test_status_message_data_wire_names() -> None: |
There was a problem hiding this comment.
Nit: This test has no await, so it can be a sync def. It tests only the model, so tests/unit/events/ fits it better.
✍️ Drafted by Claude Code
There was a problem hiding this comment.
Done, it is a sync test in tests/unit/events/test_types.py now.
Written with Claude Code (AI).
…s `None` Address the review of the `STATUS_MESSAGE` event. A `status_message_callback` that returns `None` now suppresses the event as well as the log line, as its docstring says. `level` is therefore always set, and `is_status_message_terminal` defaults to `False`. BREAKING CHANGE: No status event is emitted when `status_message_callback` returns `None`. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Description
Ports the generic status message event from Crawlee JS v4 (apify/crawlee#3818). Base branch:
v2.Event.CRAWLER_STATUSis replaced byEvent.STATUS_MESSAGE('statusMessage', same value as in JS).EventCrawlerStatusDatais replaced byEventStatusMessageDatawithcrawler_id,message,is_status_message_terminalandlevel. Wire names are camelCase (crawlerId,isStatusMessageTerminal), like the JS payload._set_status_messagehelper that logs and emits them.levelis the level the message was logged with (INFO). When astatus_message_callbackreturnsNone, nothing is logged or emitted.Finished! Total N requests: X succeeded, Y failed.at INFO withis_status_message_terminal=True, like JS. It is sent in thefinallyarounddispatch_requests(), while the event manager is still active and right before the recurring status task is cancelled, so no periodic message follows it.Breaking:
Event.CRAWLER_STATUSandEventCrawlerStatusDataare gone, and there is a new INFO log message at the end of a run, and astatus_message_callbackthat returnsNonenow also suppresses the event. The Apify SDK listens toEvent.CRAWLER_STATUSin_actor.py, so it needs a follow-up that switches toSTATUS_MESSAGEand forwardsis_status_message_terminal.Issues
Testing
test_status_message_emit: periodic events are non-terminal and carrylevel='INFO'.test_status_message_terminal_on_finish: the last event of a run (2 succeeded, 1 failed) is the only terminal one and has the expected message and level.test_status_message_data_wire_names(intests/unit/events/test_types.py): the model reads and writes the camelCase names.test_status_message_emit_with_callback: a returned message is emitted atINFO;Noneemits nothing.EventStatusMessageDatadoesn't exist). After:uv run pytest tests/unit/crawlers/_basic tests/unit/eventsgives 298 passed, 1 skipped.ruff check,ruff format --checkandty checkpass on the touched files.Written with Claude Code (AI); tested locally with
uv run pytest tests/unit/crawlers/_basic,uv run pytest tests/unit/events,uv run ruff check,uv run ruff format --checkanduv run ty check.Checklist
🤖 Generated with Claude Code