|
11 | 11 | from asyncio import CancelledError |
12 | 12 | from collections.abc import AsyncGenerator, Awaitable, Callable, Iterable, Sequence |
13 | 13 | from contextlib import AsyncExitStack, suppress |
14 | | -from datetime import datetime, timedelta, timezone |
| 14 | +from datetime import timedelta |
15 | 15 | from functools import partial |
16 | 16 | from pathlib import Path |
17 | 17 | from typing import TYPE_CHECKING, Any, Generic, Literal, cast |
@@ -740,20 +740,11 @@ async def _run_crawler(self) -> None: |
740 | 740 | for context in contexts_to_enter: |
741 | 741 | await exit_stack.enter_async_context(context) # type: ignore[arg-type] |
742 | 742 |
|
743 | | - self._crawler_state_rec_task.start() |
744 | | - try: |
| 743 | + async with self._crawler_state_rec_task: |
745 | 744 | await self._autoscaled_pool.run() |
746 | | - finally: |
747 | | - await self._crawler_state_rec_task.stop() |
748 | 745 |
|
749 | 746 | # Emit PERSIST_STATE event when crawler is finishing to allow listeners to persist their state if needed |
750 | | - if not self.statistics.state.crawler_last_started_at: |
751 | | - raise RuntimeError('Statistics.state.crawler_last_started_at not set.') |
752 | | - run_duration = datetime.now(timezone.utc) - self.statistics.state.crawler_last_started_at |
753 | | - self._statistics.state.crawler_runtime = self.statistics.state.crawler_runtime + run_duration |
754 | | - self._service_locator.get_event_manager().emit( |
755 | | - event=Event.PERSIST_STATE, event_data=EventPersistStateData(is_migrating=False) |
756 | | - ) |
| 747 | + event_manager.emit(event=Event.PERSIST_STATE, event_data=EventPersistStateData(is_migrating=False)) |
757 | 748 |
|
758 | 749 | async def add_requests( |
759 | 750 | self, |
|
0 commit comments