- ViewerRuntime.js: convert 6 heavy static imports (abCompare, sideBySide, model3d, scopes, genInfo, frameExport) to dynamic lazy imports with _prewarmViewerModules(); all 8 call sites updated with optional chaining - logging.js: add mjrDbg() export (gated by APP_CONFIG.DEBUG_VERBOSE_ERRORS); replace labeled console.debug() calls in bootstrap.js, useGridLoader.js, entry.js - ttlCache.vitest.mjs: add 4 LRU edge-case tests (10k flood, re-set order, post-expiry pruning) - grid_perf_budget.vitest.mjs: new CI perf-budget suite — 5 tests at 10k+ assets (budget 400ms, actual ~40ms) - backfill_jobs.py: new route module extracted from db_maintenance handler - test_backfill_jobs.py: 61 direct unit tests covering all public functions - test_db_corruption_recovery.py: new recovery scenario tests - auth/fetch test suites: auth_refresh_race, client_auth_token, fetch_utils - js_dist rebuilt Tests: 393 JS passed, 1428 Python passed, 2 skipped
121 lines
3.6 KiB
JavaScript
121 lines
3.6 KiB
JavaScript
/**
|
|
* Bootstrap - Auto-scan and initialization
|
|
*/
|
|
|
|
import { get, post } from "../api/client.js";
|
|
import { ENDPOINTS } from "../api/endpoints.js";
|
|
import { APP_CONFIG } from "./config.js";
|
|
import { mjrDbg } from "../utils/logging.js";
|
|
|
|
// Auto-scan state (once per session)
|
|
let startupScanDone = false;
|
|
let startupScanTimer = null;
|
|
let startupWarmupDone = false;
|
|
let startupWarmupPromise = null;
|
|
|
|
function isExecutionIdle() {
|
|
try {
|
|
const runtime = window?.__MJR_EXECUTION_RUNTIME__;
|
|
return !String(runtime?.active_prompt_id || "").trim();
|
|
} catch (error) {
|
|
console.debug?.(error);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Trigger background scan as soon as the ComfyUI frontend loads (if enabled).
|
|
* This helps warming the DB before the Assets Manager panel is opened.
|
|
*/
|
|
export async function triggerStartupScan(options = {}) {
|
|
if (!APP_CONFIG || typeof APP_CONFIG !== "object") return;
|
|
if (!APP_CONFIG.AUTO_SCAN_ON_STARTUP) return;
|
|
if (startupScanDone) return;
|
|
const delayMs = Math.max(0, Number(options?.delayMs) || 0);
|
|
const idleOnly = options?.idleOnly !== false;
|
|
if (delayMs > 0) {
|
|
if (startupScanTimer) clearTimeout(startupScanTimer);
|
|
startupScanTimer = setTimeout(() => {
|
|
startupScanTimer = null;
|
|
void triggerStartupScan({ ...options, delayMs: 0 });
|
|
}, delayMs);
|
|
return;
|
|
}
|
|
if (idleOnly && !isExecutionIdle()) {
|
|
if (startupScanTimer) clearTimeout(startupScanTimer);
|
|
startupScanTimer = setTimeout(() => {
|
|
startupScanTimer = null;
|
|
void triggerStartupScan({ ...options, delayMs: 0 });
|
|
}, 5000);
|
|
return;
|
|
}
|
|
startupScanDone = true;
|
|
|
|
try {
|
|
mjrDbg("[Majoor] Starting startup scan of output directory...");
|
|
|
|
const result = await post(ENDPOINTS.SCAN, {
|
|
recursive: true,
|
|
incremental: true,
|
|
fast: true,
|
|
background_metadata: true,
|
|
});
|
|
|
|
if (result.ok) {
|
|
const stats = result.data;
|
|
mjrDbg(
|
|
`[Majoor] Startup scan complete — added: ${stats.added}, updated: ${stats.updated}, skipped: ${stats.skipped}`,
|
|
);
|
|
} else {
|
|
console.warn("📂 Majoor [⚠️]: Startup scan failed:", result.error);
|
|
}
|
|
} catch (error) {
|
|
console.error("📂 Majoor [❌]: Startup scan error:", error);
|
|
}
|
|
}
|
|
|
|
async function warmDbAndStatus() {
|
|
try {
|
|
await Promise.allSettled([
|
|
get(ENDPOINTS.HEALTH),
|
|
get(`${ENDPOINTS.HEALTH_COUNTERS}?scope=output`),
|
|
get(ENDPOINTS.HEALTH_DB),
|
|
]);
|
|
} catch (error) {
|
|
console.debug?.(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Run a single explicit startup warmup sequence:
|
|
* 1. warm API/DB/status endpoints
|
|
* 2. schedule a background incremental scan if enabled and runtime is idle
|
|
*/
|
|
export async function runStartupWarmup(options = {}) {
|
|
if (startupWarmupDone) return startupWarmupPromise || Promise.resolve();
|
|
startupWarmupDone = true;
|
|
startupWarmupPromise = (async () => {
|
|
await warmDbAndStatus();
|
|
await triggerStartupScan(options);
|
|
})();
|
|
return startupWarmupPromise;
|
|
}
|
|
|
|
/**
|
|
* Test API connection
|
|
*/
|
|
export async function testAPI() {
|
|
try {
|
|
mjrDbg("[Majoor] Testing API connection...");
|
|
const data = await get(ENDPOINTS.HEALTH);
|
|
|
|
if (data?.ok) {
|
|
mjrDbg("[Majoor] API connection successful, health:", data.data.overall);
|
|
} else {
|
|
console.error("📂 Majoor [❌]: API health check failed:", data?.error);
|
|
}
|
|
} catch (error) {
|
|
console.error("📂 Majoor [❌]: Failed to connect to API:", error);
|
|
}
|
|
}
|