|
| 1 | +import { |
| 2 | + findProjectCatalogById, |
| 3 | + findProjectCatalogPendingOnboarding, |
| 4 | + updateProjectCatalog, |
| 5 | +} from '@crowd/data-access-layer' |
| 6 | +import { IDbProjectCatalog } from '@crowd/data-access-layer/src/project-catalog/types' |
| 7 | +import { pgpQx } from '@crowd/data-access-layer/src/queryExecutor' |
| 8 | +import { getServiceLogger } from '@crowd/logging' |
| 9 | + |
| 10 | +import { svc } from '../main' |
| 11 | +import { onboardProject } from '../onboarder/onboarder' |
| 12 | + |
| 13 | +const log = getServiceLogger() |
| 14 | + |
| 15 | +export async function fetchProjectsPendingOnboarding( |
| 16 | + batchSize: number, |
| 17 | +): Promise<IDbProjectCatalog[]> { |
| 18 | + const qx = pgpQx(svc.postgres.reader.connection()) |
| 19 | + |
| 20 | + const projects = await findProjectCatalogPendingOnboarding(qx, { limit: batchSize }) |
| 21 | + |
| 22 | + log.info({ count: projects.length, batchSize }, 'Fetched projects pending onboarding.') |
| 23 | + |
| 24 | + return projects |
| 25 | +} |
| 26 | + |
| 27 | +export async function onboardAndUpdateProject(project: IDbProjectCatalog): Promise<void> { |
| 28 | + const qx = pgpQx(svc.postgres.writer.connection()) |
| 29 | + const startTime = Date.now() |
| 30 | + |
| 31 | + // Guard: fetch fresh state to ensure the API is called at most once per project. |
| 32 | + // Uses the writer connection to avoid replica lag missing a just-written onboardedAt. |
| 33 | + const fresh = await findProjectCatalogById(qx, project.id) |
| 34 | + if (fresh?.onboardedAt) { |
| 35 | + log.info( |
| 36 | + { id: project.id, repoUrl: project.repoUrl, onboardedAt: fresh.onboardedAt }, |
| 37 | + 'Project already onboarded, skipping API call.', |
| 38 | + ) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + log.info({ id: project.id, repoUrl: project.repoUrl }, 'Starting onboarding.') |
| 43 | + |
| 44 | + const result = await onboardProject({ |
| 45 | + id: project.id, |
| 46 | + repoUrl: project.repoUrl, |
| 47 | + repoName: project.repoName, |
| 48 | + projectSlug: project.projectSlug, |
| 49 | + }) |
| 50 | + |
| 51 | + if (result.outcome === 'error') { |
| 52 | + throw new Error(result.error ?? 'Unknown onboarding error') |
| 53 | + } |
| 54 | + |
| 55 | + await updateProjectCatalog(qx, project.id, { |
| 56 | + onboardedAt: new Date().toISOString(), |
| 57 | + }) |
| 58 | + |
| 59 | + const elapsedSeconds = ((Date.now() - startTime) / 1000).toFixed(1) |
| 60 | + |
| 61 | + log.info( |
| 62 | + { id: project.id, repoUrl: project.repoUrl, segmentId: result.segmentId, elapsedSeconds }, |
| 63 | + 'Onboarding complete.', |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +export async function markProjectOnboardingFailed( |
| 68 | + projectId: string, |
| 69 | + reason: string, |
| 70 | +): Promise<void> { |
| 71 | + const qx = pgpQx(svc.postgres.writer.connection()) |
| 72 | + |
| 73 | + await updateProjectCatalog(qx, projectId, { |
| 74 | + action: 'error', |
| 75 | + onboardingError: reason, |
| 76 | + }) |
| 77 | + |
| 78 | + log.error({ id: projectId, reason }, 'Onboarding permanently failed, marked as error.') |
| 79 | +} |
0 commit comments