Spaces:
Paused
Paused
File size: 1,603 Bytes
35743bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import initializeCloudSync from "@/shared/services/initializeCloudSync";
import { startBudgetResetJob } from "@/lib/jobs/budgetResetJob";
import { startModelSyncScheduler } from "@/shared/services/modelSyncScheduler";
// Initialize runtime background sync services once per server process.
let initialized = false;
function isAutomatedTestProcess(
env: NodeJS.ProcessEnv = process.env,
argv: string[] = process.argv
): boolean {
return (
env.NODE_ENV === "test" || env.VITEST !== undefined || argv.some((arg) => arg.includes("test"))
);
}
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(env, argv) && 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;
|