mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-02 05:12:11 +03:00
shouldSkipCloudSyncInitialization(env, argv) forwarded its own parameters in the wrong order to isAutomatedTestProcess(argv, env) — passing env where argv is expected and vice versa. Any real argv array landed in the `env` position (harmless there) but the env object landed in the `argv` position, and argv.some() then threw `TypeError: argv.some is not a function` as soon as a caller passed an explicit, correctly-ordered argv/env pair (tests/unit/model-sync- scheduler.test.ts "initCloudSync skips auto initialization..."). Fixed the call-site argument order. Also hardened isAutomatedTestProcess() to tolerate a non-array argv defensively (return false instead of throwing) since this check gates production background-task startup (auto-backup, migrations, cloud sync) and must never crash the process it's protecting.
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import initializeCloudSync from "@/shared/services/initializeCloudSync";
|
|
import { startBudgetResetJob } from "@/lib/jobs/budgetResetJob";
|
|
import { startModelSyncScheduler } from "@/shared/services/modelSyncScheduler";
|
|
import { isAutomatedTestProcess } from "@/shared/utils/testProcess";
|
|
|
|
// Initialize runtime background sync services once per server process.
|
|
let initialized = false;
|
|
|
|
|
|
export function shouldSkipCloudSyncInitialization(
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
argv: string[] = process.argv
|
|
): boolean {
|
|
if (env.NEXT_PHASE === "phase-production-build") {
|
|
return true;
|
|
}
|
|
|
|
const raw = env.OMNIROUTE_DISABLE_BACKGROUND_SERVICES;
|
|
if (raw && new Set(["1", "true", "yes", "on"]).has(raw.trim().toLowerCase())) {
|
|
return true;
|
|
}
|
|
|
|
return isAutomatedTestProcess(argv, env) && env.OMNIROUTE_ENABLE_RUNTIME_BACKGROUND_TASKS !== "1";
|
|
}
|
|
|
|
export async function ensureCloudSyncInitialized() {
|
|
if (shouldSkipCloudSyncInitialization()) {
|
|
return false;
|
|
}
|
|
if (!initialized) {
|
|
try {
|
|
const { initTokenHealthCheck } = await import("@/lib/tokenHealthCheck");
|
|
initTokenHealthCheck();
|
|
await initializeCloudSync();
|
|
startModelSyncScheduler();
|
|
startBudgetResetJob();
|
|
initialized = true;
|
|
} catch (error) {
|
|
console.error("[ServerInit] Error initializing background sync services:", error);
|
|
}
|
|
}
|
|
return initialized;
|
|
}
|
|
|
|
export default ensureCloudSyncInitialized;
|