| import asyncio | |
| import json | |
| import sys | |
| from dataclasses import asdict | |
| from app.collectors.registry import ( | |
| get_opportunity_collectors, | |
| ) | |
| from app.db.session import AsyncSessionFactory | |
| from app.services.opportunity_sync import ( | |
| OpportunitySyncService, | |
| ) | |
| async def main() -> None: | |
| collectors = get_opportunity_collectors() | |
| global_results: list[dict] = [] | |
| has_failure = False | |
| for collector in collectors: | |
| async with AsyncSessionFactory() as session: | |
| service = OpportunitySyncService(session) | |
| try: | |
| result = await service.synchronize( | |
| collector | |
| ) | |
| global_results.append( | |
| { | |
| "status": "success", | |
| **asdict(result), | |
| } | |
| ) | |
| except Exception as error: | |
| has_failure = True | |
| global_results.append( | |
| { | |
| "status": "failed", | |
| "source_slug": ( | |
| collector.source_slug | |
| ), | |
| "error_type": ( | |
| type(error).__name__ | |
| ), | |
| "error": str(error), | |
| } | |
| ) | |
| print( | |
| json.dumps( | |
| global_results, | |
| indent=2, | |
| ensure_ascii=False, | |
| ), | |
| flush=True, | |
| ) | |
| if has_failure: | |
| raise SystemExit(1) | |
| if __name__ == "__main__": | |
| try: | |
| asyncio.run(main()) | |
| except KeyboardInterrupt: | |
| print( | |
| "Synchronization interrupted.", | |
| file=sys.stderr, | |
| ) | |
| raise SystemExit(130) |