Repository navigation
feat(events)!: Replace CRAWLER_STATUS with STATUS_MESSAGE
#2542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,8 +43,8 @@ | |
| from crawlee.events import ( | ||
| Event, | ||
| EventAbortingData, | ||
| EventCrawlerStatusData, | ||
| EventMigratingData, | ||
| EventStatusMessageData, | ||
| LocalEventManager, | ||
| ) | ||
| from crawlee.http_clients import HttpClient, HttpResponse, ImpitHttpClient | ||
|
|
@@ -3718,23 +3718,93 @@ async def handler(context: BasicCrawlingContext) -> None: | |
|
|
||
|
|
||
| async def test_status_message_emit() -> None: | ||
| """Periodic status messages are emitted as non-terminal `STATUS_MESSAGE` events with the logged level.""" | ||
| event_manager = service_locator.get_event_manager() | ||
|
|
||
| status_message_listener = Mock() | ||
|
|
||
| def listener(event_data: EventCrawlerStatusData) -> None: | ||
| # A sync listener runs in a thread, which doesn't keep the order of the events. | ||
| async def listener(event_data: EventStatusMessageData) -> None: | ||
| status_message_listener(event_data) | ||
|
|
||
| event_manager.on(event=Event.CRAWLER_STATUS, listener=listener) | ||
| event_manager.on(event=Event.STATUS_MESSAGE, listener=listener) | ||
|
|
||
| crawler = BasicCrawler(request_handler=AsyncMock(), id='my-crawler') | ||
|
|
||
| await crawler.run(['https://a.placeholder.com']) | ||
|
|
||
| event_manager.off(event=Event.CRAWLER_STATUS, listener=listener) | ||
| event_manager.off(event=Event.STATUS_MESSAGE, listener=listener) | ||
|
|
||
| assert status_message_listener.called | ||
| assert status_message_listener.call_args.args[0].crawler_id == 'my-crawler' | ||
| first_event = status_message_listener.call_args_list[0].args[0] | ||
| assert first_event.crawler_id == 'my-crawler' | ||
| assert first_event.message.startswith('Crawled ') | ||
| assert first_event.is_status_message_terminal is False | ||
| assert first_event.level == 'INFO' | ||
|
|
||
|
|
||
| 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.""" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Please add a test for the callback paths: ✍️ Drafted by Claude Code
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added Written with Claude Code (AI). |
||
| event_manager = service_locator.get_event_manager() | ||
|
|
||
| status_message_listener = Mock() | ||
|
|
||
| # A sync listener runs in a thread, which doesn't keep the order of the events. | ||
| async def listener(event_data: EventStatusMessageData) -> None: | ||
| status_message_listener(event_data) | ||
|
|
||
| event_manager.on(event=Event.STATUS_MESSAGE, listener=listener) | ||
|
|
||
| async def handler(context: BasicCrawlingContext) -> None: | ||
| if 'fail' in context.request.url: | ||
| raise RuntimeError('Arbitrary crash for testing purposes') | ||
|
|
||
| crawler = BasicCrawler(request_handler=handler, max_request_retries=0, id='my-crawler') | ||
|
|
||
| await crawler.run(['https://a.placeholder.com', 'https://b.placeholder.com', 'https://fail.placeholder.com']) | ||
|
|
||
| event_manager.off(event=Event.STATUS_MESSAGE, listener=listener) | ||
|
|
||
| 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]] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: ✍️ Drafted by Claude Code
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, both tests now use an Written with Claude Code (AI). |
||
| assert events[-1] == EventStatusMessageData( | ||
| crawler_id='my-crawler', | ||
| message='Finished! Total 3 requests: 2 succeeded, 1 failed.', | ||
| is_status_message_terminal=True, | ||
| level='INFO', | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ('callback_result', 'expected_events'), | ||
| [ | ||
| pytest.param('Custom status.', [('Custom status.', 'INFO')], id='message'), | ||
| pytest.param(None, [], id='none'), | ||
| ], | ||
| ) | ||
| async def test_status_message_emit_with_callback( | ||
| callback_result: str | None, expected_events: list[tuple[str, str]] | ||
| ) -> None: | ||
| """A status update emits the message that the callback returns, and nothing when it returns `None`.""" | ||
| event_manager = service_locator.get_event_manager() | ||
|
|
||
| status_message_listener = Mock() | ||
|
|
||
| async def listener(event_data: EventStatusMessageData) -> None: | ||
| status_message_listener(event_data) | ||
|
|
||
| crawler = BasicCrawler(status_message_callback=AsyncMock(return_value=callback_result)) | ||
| crawler._last_run = Mock(previous_state=None) | ||
| crawler._statistics = Mock(state=StatisticsState()) | ||
|
|
||
| async with event_manager: | ||
| event_manager.on(event=Event.STATUS_MESSAGE, listener=listener) | ||
| await crawler._crawler_state_task() | ||
| await event_manager.wait_for_all_listeners_to_complete() | ||
| event_manager.off(event=Event.STATUS_MESSAGE, listener=listener) | ||
|
|
||
| events = [call.args[0] for call in status_message_listener.call_args_list] | ||
| assert [(event.message, event.level) for event in events] == expected_events | ||
|
|
||
|
|
||
| async def test_status_message_reports_failed_request_count() -> None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
Finished! ...is logged beforeError analysis,The crawl was interrupted...andFinal 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep it here. The
Error analysis, interrupted and final statistics lines are logged inrun(), after_run_crawlerhas exited the event manager, whereemitraises unless someone else keeps it active. Moving the message there would mean restructuring how the contexts are entered.Written with Claude Code (AI).