Skip to content

news_fetch pipeline

Active cron task in pipes/tasks/alerts/news_fetch.py.

Task classes

Class Role
NewsFetchScheduler BaseWrapper — loads news_fetch cron rows from MySQL, yields child tasks
NewsFetcher Paginates external news API, filters articles, uploads images, upserts DB

Flow

flowchart TD
    Cron[Cron invokes NewsFetchScheduler] --> MySQL[(cron_settings rows)]
    MySQL --> Eval{eval_cron matches?}
    Eval -- no --> Skip[Skip alert]
    Eval -- yes --> Fetch[NewsFetcher.run]
    Fetch --> API[NewsData API pages]
    API --> Filter[Language/country/length filters]
    Filter --> Images[Upload images to S3]
    Images --> Upsert[Upsert search_content_items]

Cron gating

Each alert row in MySQL includes a cron expression. eval_cron() (in pipes/libs/utils.py) checks whether the current minute falls inside the alert's schedule window (NEWS_FETCH_CRON_WINDOW_MIN, default 5 minutes).

Pagination early-stop

NewsFetcher.fetch_and_upsert_news tracks consecutive pages where all candidate articles already exist in S3 (skipped_exists). After max_empty_pages (default 2, overridable per alert settings JSON) the fetcher stops requesting the next API page.

Expected log lines:

[news_fetch] no new uploads (all existed) empty_pages=1/2
[news_fetch] stopping early: reached pages where everything exists in S3

Download failures or empty pages for other reasons do not increment the counter — only the "all existed" path does.

HTTP layer

API pagination uses pipes/libs/news_fetch_http.py (get_json_with_retry, rate-limit sleeps). Unit tests in tests/test_news_fetch_http.py.

Image upload

model.upload_articles_images_to_s3 downloads each image URL, generates optimized/thumbnail variants, writes to the tasks-scheduler S3 bucket. See Image fallback when publishers block plain HTTP clients.

Maps

  • config/language_map.json — normalize language tokens
  • config/country_map.json — normalize country names (regenerate via scripts/build_country_map.py)

Legacy tasks in repo

Module Status
pipes/tasks/campaign_sync.py Legacy campaign sync via spapi; not scheduled from this repo's crontab
pipes/tasks/series_uploader_google_sheets.py Standalone script; run via execute_gen_python.sh pattern