|
| 1 | +import { getServiceLogger } from '@crowd/logging' |
| 2 | + |
| 3 | +import { getPackagesDb } from '../db' |
| 4 | +import { backfillMavenRepositoryUrls } from '../maven/backfillRepositoryUrl' |
| 5 | + |
| 6 | +const log = getServiceLogger() |
| 7 | + |
| 8 | +let shuttingDown = false |
| 9 | + |
| 10 | +// Graceful stop: finish the in-flight batch, then exit. Safe to interrupt — every |
| 11 | +// write is an idempotent UPDATE recomputed from declared_repository_url, so |
| 12 | +// re-running simply reprocesses and skips rows that already match. |
| 13 | +const shutdown = () => { |
| 14 | + if (shuttingDown) return |
| 15 | + shuttingDown = true |
| 16 | + log.info('Shutting down maven repo-url backfill (stopping after the current batch)...') |
| 17 | +} |
| 18 | + |
| 19 | +process.on('SIGINT', shutdown) |
| 20 | +process.on('SIGTERM', shutdown) |
| 21 | + |
| 22 | +const DEFAULT_BATCH_SIZE = 5000 |
| 23 | + |
| 24 | +const main = async () => { |
| 25 | + const dryRun = process.argv.includes('--dry-run') |
| 26 | + const criticalOnly = process.argv.includes('--critical-only') |
| 27 | + const rawBatchSize = process.env.MAVEN_REPO_URL_BACKFILL_BATCH_SIZE |
| 28 | + const parsedBatchSize = rawBatchSize === undefined ? DEFAULT_BATCH_SIZE : Number(rawBatchSize) |
| 29 | + if (!Number.isInteger(parsedBatchSize) || parsedBatchSize <= 0) { |
| 30 | + log.error( |
| 31 | + { MAVEN_REPO_URL_BACKFILL_BATCH_SIZE: rawBatchSize }, |
| 32 | + 'MAVEN_REPO_URL_BACKFILL_BATCH_SIZE must be a positive integer', |
| 33 | + ) |
| 34 | + process.exit(1) |
| 35 | + } |
| 36 | + const batchSize = parsedBatchSize |
| 37 | + |
| 38 | + log.info( |
| 39 | + { dryRun, criticalOnly, batchSize }, |
| 40 | + 'maven repo-url backfill starting (recompute normalizeScmUrl from declared_repository_url, no POM fetch)...', |
| 41 | + ) |
| 42 | + |
| 43 | + const qx = await getPackagesDb() |
| 44 | + await qx.selectOne('SELECT 1') |
| 45 | + log.info('Connected to packages-db.') |
| 46 | + |
| 47 | + const totals = await backfillMavenRepositoryUrls(qx, { |
| 48 | + batchSize, |
| 49 | + dryRun, |
| 50 | + criticalOnly, |
| 51 | + isShuttingDown: () => shuttingDown, |
| 52 | + }) |
| 53 | + |
| 54 | + log.info({ ...totals, dryRun }, 'maven repo-url backfill complete') |
| 55 | + process.exit(0) |
| 56 | +} |
| 57 | + |
| 58 | +main().catch((err) => { |
| 59 | + log.error({ err }, 'maven repo-url backfill fatal error') |
| 60 | + process.exit(1) |
| 61 | +}) |
0 commit comments