|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | + |
| 4 | +type ResetEnvironment = "staging" | "prod"; |
| 5 | + |
| 6 | +const WRAPPED_ENV_VAR = "AUTUMN_RESET_V2_WRAPPED"; |
| 7 | +const ENV_FILES: Record<ResetEnvironment, string> = { |
| 8 | + staging: ".env.staging", |
| 9 | + prod: ".env.prod", |
| 10 | +}; |
| 11 | + |
| 12 | +const parseEnvironment = (): ResetEnvironment => { |
| 13 | + const environment = process.argv[2]; |
| 14 | + if (environment === "staging" || environment === "prod") return environment; |
| 15 | + |
| 16 | + console.error("Usage: bun reset-v2 <staging|prod>"); |
| 17 | + process.exit(2); |
| 18 | +}; |
| 19 | + |
| 20 | +const runWithInfisical = async ({ |
| 21 | + environment, |
| 22 | +}: { |
| 23 | + environment: ResetEnvironment; |
| 24 | +}) => { |
| 25 | + const child = spawn( |
| 26 | + "infisical", |
| 27 | + [ |
| 28 | + "run", |
| 29 | + `--env=${environment}`, |
| 30 | + "--recursive", |
| 31 | + "--", |
| 32 | + "bun", |
| 33 | + fileURLToPath(import.meta.url), |
| 34 | + environment, |
| 35 | + ], |
| 36 | + { |
| 37 | + stdio: "inherit", |
| 38 | + env: { |
| 39 | + ...process.env, |
| 40 | + [WRAPPED_ENV_VAR]: "1", |
| 41 | + ENV_FILE: ENV_FILES[environment], |
| 42 | + NODE_ENV: "development", |
| 43 | + }, |
| 44 | + }, |
| 45 | + ); |
| 46 | + |
| 47 | + const exitCode = await new Promise<number>((resolve) => { |
| 48 | + child.on("close", (code) => resolve(code ?? 1)); |
| 49 | + child.on("error", (error) => { |
| 50 | + console.error(`Failed to start Infisical: ${error.message}`); |
| 51 | + resolve(1); |
| 52 | + }); |
| 53 | + }); |
| 54 | + process.exit(exitCode); |
| 55 | +}; |
| 56 | + |
| 57 | +const runResetV2 = async ({ |
| 58 | + environment, |
| 59 | +}: { |
| 60 | + environment: ResetEnvironment; |
| 61 | +}) => { |
| 62 | + await import("../sentry.js"); |
| 63 | + await import("../internal/misc/resetJobV2/resetJobV2Store.js"); |
| 64 | + |
| 65 | + const { initDrizzle } = await import("../db/initDrizzle.js"); |
| 66 | + const { startPgPoolMonitor, stopPgPoolMonitor } = await import( |
| 67 | + "../db/pgPoolMonitor.js" |
| 68 | + ); |
| 69 | + const { logger } = await import("../external/logtail/logtailUtils.js"); |
| 70 | + const { runResetLoopV2 } = await import( |
| 71 | + "../internal/balances/batchReset/runResetLoopV2.js" |
| 72 | + ); |
| 73 | + const { getResetJobV2Config, getResetJobV2ConfigStatus } = await import( |
| 74 | + "../internal/misc/resetJobV2/resetJobV2Store.js" |
| 75 | + ); |
| 76 | + const { startAllEdgeConfigPolling, stopAllEdgeConfigPolling } = await import( |
| 77 | + "../internal/misc/edgeConfig/edgeConfigRegistry.js" |
| 78 | + ); |
| 79 | + |
| 80 | + await startAllEdgeConfigPolling({ logger }); |
| 81 | + |
| 82 | + const { db, client } = initDrizzle({ |
| 83 | + name: "reset-cron-v2", |
| 84 | + maxConnections: 10, |
| 85 | + }); |
| 86 | + startPgPoolMonitor(); |
| 87 | + |
| 88 | + const controller = new AbortController(); |
| 89 | + const loopPromise = runResetLoopV2({ |
| 90 | + ctx: { db, logger }, |
| 91 | + signal: controller.signal, |
| 92 | + }); |
| 93 | + let shuttingDown = false; |
| 94 | + |
| 95 | + // Everything under `data`: the express dataset is at its Axiom column |
| 96 | + // limit, so new top-level (flattened) field names get the whole ingest |
| 97 | + // batch rejected. |
| 98 | + logger.info("[reset-cus-ents-v2] dedicated scanner started", { |
| 99 | + jobName: "reset-cus-ents-v2", |
| 100 | + data: { |
| 101 | + environment, |
| 102 | + config: getResetJobV2Config(), |
| 103 | + configStatus: getResetJobV2ConfigStatus(), |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + // The scanner must never die because telemetry hiccuped (e.g. an Axiom |
| 108 | + // ingest rejection inside the pino transport becomes an unhandled |
| 109 | + // rejection, which kills the process by default). |
| 110 | + process.on("unhandledRejection", (reason) => { |
| 111 | + console.error("[reset-cus-ents-v2] unhandled rejection (ignored):", reason); |
| 112 | + }); |
| 113 | + |
| 114 | + const shutdown = async (signal: string) => { |
| 115 | + if (shuttingDown) return; |
| 116 | + shuttingDown = true; |
| 117 | + logger.info(`[reset-cus-ents-v2] received ${signal}, shutting down`); |
| 118 | + controller.abort(); |
| 119 | + stopPgPoolMonitor(); |
| 120 | + stopAllEdgeConfigPolling(); |
| 121 | + await loopPromise; |
| 122 | + await client.end(); |
| 123 | + }; |
| 124 | + |
| 125 | + process.once("SIGINT", () => void shutdown("SIGINT")); |
| 126 | + process.once("SIGTERM", () => void shutdown("SIGTERM")); |
| 127 | + |
| 128 | + await loopPromise; |
| 129 | +}; |
| 130 | + |
| 131 | +const main = async () => { |
| 132 | + const environment = parseEnvironment(); |
| 133 | + if (process.env[WRAPPED_ENV_VAR] !== "1") { |
| 134 | + await runWithInfisical({ environment }); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + await runResetV2({ environment }); |
| 139 | +}; |
| 140 | + |
| 141 | +await main(); |
0 commit comments