|
| 1 | +"""Feed sync registry — shared by bootstrap command and ops HTTP triggers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import Any, Callable |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | +FeedRunner = Callable[[str | None], dict[str, Any]] |
| 11 | + |
| 12 | +FEED_LABELS: tuple[str, ...] = ('news', 'seismic', 'trails') |
| 13 | + |
| 14 | +FEED_TASK_NAMES: dict[str, str] = { |
| 15 | + 'news': 'news.poll_sources', |
| 16 | + 'seismic': 'seismic.sync_events', |
| 17 | + 'trails': 'trails.sync_open_data', |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def _run_news(island_key: str | None) -> dict[str, Any]: |
| 22 | + from news.services import poll_all_sources |
| 23 | + |
| 24 | + return {'status': 'ok', **poll_all_sources(island_key=island_key)} |
| 25 | + |
| 26 | + |
| 27 | +def _run_seismic(island_key: str | None) -> dict[str, Any]: |
| 28 | + from seismic.services import sync_all_events |
| 29 | + |
| 30 | + return {'status': 'ok', **sync_all_events(island_key=island_key)} |
| 31 | + |
| 32 | + |
| 33 | +def _run_trails(island_key: str | None) -> dict[str, Any]: |
| 34 | + from trails.services import sync_all_open_data |
| 35 | + |
| 36 | + return {'status': 'ok', **sync_all_open_data(island_key=island_key)} |
| 37 | + |
| 38 | + |
| 39 | +FEED_RUNNERS: dict[str, FeedRunner] = { |
| 40 | + 'news': _run_news, |
| 41 | + 'seismic': _run_seismic, |
| 42 | + 'trails': _run_trails, |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +def _queue_news(island_key: str | None): |
| 47 | + from news.tasks import poll_sources_task |
| 48 | + |
| 49 | + return poll_sources_task.delay(island_key=island_key) |
| 50 | + |
| 51 | + |
| 52 | +def _queue_seismic(island_key: str | None): |
| 53 | + from seismic.tasks import sync_events_task |
| 54 | + |
| 55 | + return sync_events_task.delay(island_key=island_key) |
| 56 | + |
| 57 | + |
| 58 | +def _queue_trails(island_key: str | None): |
| 59 | + from trails.tasks import sync_open_data_task |
| 60 | + |
| 61 | + return sync_open_data_task.delay(island_key=island_key) |
| 62 | + |
| 63 | + |
| 64 | +FEED_QUEUERS = { |
| 65 | + 'news': _queue_news, |
| 66 | + 'seismic': _queue_seismic, |
| 67 | + 'trails': _queue_trails, |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +def normalize_feed_param(feed: str) -> list[str]: |
| 72 | + value = (feed or 'all').strip().lower() |
| 73 | + if value == 'all': |
| 74 | + return list(FEED_LABELS) |
| 75 | + if value in FEED_LABELS: |
| 76 | + return [value] |
| 77 | + raise ValueError(f'Unknown feed {feed!r}; use all or one of {", ".join(FEED_LABELS)}') |
| 78 | + |
| 79 | + |
| 80 | +def run_feed_sync(label: str, *, island_key: str | None = None) -> dict[str, Any]: |
| 81 | + runner = FEED_RUNNERS[label] |
| 82 | + return runner(island_key) |
| 83 | + |
| 84 | + |
| 85 | +def queue_feed_sync(label: str, *, island_key: str | None = None) -> dict[str, Any]: |
| 86 | + async_result = FEED_QUEUERS[label](island_key) |
| 87 | + return { |
| 88 | + 'task': FEED_TASK_NAMES[label], |
| 89 | + 'celery_task_id': async_result.id, |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +def trigger_feed_syncs( |
| 94 | + labels: list[str], |
| 95 | + *, |
| 96 | + island_key: str | None = None, |
| 97 | + run_async: bool = False, |
| 98 | +) -> dict[str, Any]: |
| 99 | + results: dict[str, Any] = {} |
| 100 | + for label in labels: |
| 101 | + try: |
| 102 | + if run_async: |
| 103 | + queued = queue_feed_sync(label, island_key=island_key) |
| 104 | + results[label] = {'ok': True, 'mode': 'async', **queued} |
| 105 | + else: |
| 106 | + payload = run_feed_sync(label, island_key=island_key) |
| 107 | + results[label] = {'ok': True, 'mode': 'sync', **payload} |
| 108 | + except Exception as exc: |
| 109 | + logger.exception('feed sync failed label=%s island=%s', label, island_key) |
| 110 | + results[label] = { |
| 111 | + 'ok': False, |
| 112 | + 'mode': 'async' if run_async else 'sync', |
| 113 | + 'task': FEED_TASK_NAMES.get(label), |
| 114 | + 'error': str(exc), |
| 115 | + 'error_type': type(exc).__name__, |
| 116 | + } |
| 117 | + return results |
0 commit comments