"""Celery task that runs company enrichment exactly once, right after a company is created (see company_service.create_company, which only ever enqueues this when NINJAPEAR_API_KEY is configured). Generous time limits since some NinjaPear endpoints are documented as taking up to 5 minutes, and a single company can trigger the company-level calls plus several capped per-leadership-member lookups in series. """ from __future__ import annotations import uuid from structlog.contextvars import bound_contextvars from app.core.config import get_settings from app.core.logging import get_logger from app.db.session import get_sessionmaker from app.enrichment.factory import get_enrichment_provider from app.repositories.company_repository import CompanyRepository from app.services import user_api_key_service from app.services.enrichment_service import enrich_company as enrich_company_service from app.tasks.base import run_async_task from app.tasks.celery_app import celery_app logger = get_logger(__name__) @celery_app.task( bind=True, name="app.tasks.enrichment.enrich_company", max_retries=1, soft_time_limit=1500, time_limit=1600, ) def enrich_company(self, company_id: str) -> None: with bound_contextvars(task_id=self.request.id, company_id=company_id): run_async_task(_enrich_company_async(company_id)) async def _enrich_company_async(company_id: str) -> None: settings = get_settings() session_factory = get_sessionmaker() async with session_factory() as db: company = await CompanyRepository(db).get_by_id(uuid.UUID(company_id)) if company is None: logger.warning("enrichment_company_not_found", company_id=company_id) return settings = await user_api_key_service.get_effective_settings(db, company.user_id, settings) provider = get_enrichment_provider(settings) logger.info( "company_enrichment_started", company_id=company_id, provider=provider.provider_name ) await enrich_company_service(db, settings, provider, company)