Skip to content

Commit

Permalink
make sure scrapers fail when they fail
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Jan 17, 2024
1 parent 96d3ec0 commit 67d9566
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions juniorguru_plucker/actors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from scrapy.crawler import CrawlerProcess
from scrapy.settings import BaseSettings, Settings
from scrapy.spiderloader import SpiderLoader as BaseSpiderLoader
from scrapy.statscollectors import StatsCollector
from scrapy.utils.reactor import install_reactor


Expand All @@ -20,9 +21,22 @@ async def run_actor(settings: Settings, spider_class: Type[Spider]) -> None:


def run_spider(settings: Settings, spider_class: Type[Spider]):
crawler = CrawlerProcess(settings, install_root_handler=False)
crawler.crawl(spider_class)
crawler.start()
crawler_process = CrawlerProcess(settings, install_root_handler=False)
crawler_process.crawl(spider_class)
stats_collector = get_stats_collector(crawler_process)
crawler_process.start()

if exc_count := stats_collector.get_value("spider_exceptions"):
raise RuntimeError(f"Scraping failed with {exc_count} exceptions raised")
if error_count := stats_collector.get_value("log_count/ERROR"):
raise RuntimeError(f"Scraping failed with {error_count} errors logged")


def get_stats_collector(crawler_process: CrawlerProcess) -> StatsCollector:
if len(crawler_process.crawlers) != 1:
raise RuntimeError("Exactly one crawler expected")
crawler = crawler_process.crawlers.pop()
return crawler.stats


def apply_apify_settings(
Expand Down
4 changes: 4 additions & 0 deletions juniorguru_plucker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
# ITEM_PIPELINES = {
# # 'juniorguru_plucker.pipelines.TitleItemPipeline': 123,
# }

CLOSESPIDER_ERRORCOUNT = 1

CLOSESPIDER_TIMEOUT_NO_ITEM = 30 # seconds

0 comments on commit 67d9566

Please sign in to comment.