mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-07 07:58:36 +00:00
perf: lighten gateway watch startup
This commit is contained in:
@@ -5,7 +5,7 @@ import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import process from "node:process";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { isRestartRelevantRunNodePath, runNodeWatchedPaths } from "./run-node.mjs";
|
||||
import { isRestartRelevantRunNodePath, runNodeWatchedPaths } from "./run-node-watch-paths.mjs";
|
||||
|
||||
const WATCH_NODE_RUNNER = "scripts/run-node.mjs";
|
||||
const WATCH_RESTART_SIGNAL = "SIGTERM";
|
||||
@@ -255,19 +255,6 @@ const releaseWatchLock = (lockHandle) => {
|
||||
* }} [params]
|
||||
*/
|
||||
export async function runWatchMain(params = {}) {
|
||||
let createWatcher = params.createWatcher;
|
||||
if (!createWatcher) {
|
||||
try {
|
||||
const chokidarModule = await (params.loadChokidar ?? loadChokidar)();
|
||||
createWatcher = (watchPaths, options) => chokidarModule.watch(watchPaths, options);
|
||||
} catch (err) {
|
||||
if (isInvalidPackageConfigError(err)) {
|
||||
printFriendlyWatchStartupError(err);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const deps = {
|
||||
spawn: params.spawn ?? spawn,
|
||||
process: params.process ?? process,
|
||||
@@ -278,7 +265,8 @@ export async function runWatchMain(params = {}) {
|
||||
sleep: params.sleep ?? sleep,
|
||||
signalProcess: params.signalProcess ?? ((pid, signal) => process.kill(pid, signal)),
|
||||
lockDisabled: params.lockDisabled === true,
|
||||
createWatcher,
|
||||
createWatcher: params.createWatcher,
|
||||
loadChokidar: params.loadChokidar ?? loadChokidar,
|
||||
watchPaths: params.watchPaths ?? runNodeWatchedPaths,
|
||||
};
|
||||
|
||||
@@ -293,7 +281,7 @@ export async function runWatchMain(params = {}) {
|
||||
childEnv.OPENCLAW_WATCH_COMMAND = deps.args.join(" ");
|
||||
}
|
||||
|
||||
return await new Promise((resolve) => {
|
||||
return await new Promise((resolve, reject) => {
|
||||
let settled = false;
|
||||
let shuttingDown = false;
|
||||
let restartRequested = false;
|
||||
@@ -357,6 +345,38 @@ export async function runWatchMain(params = {}) {
|
||||
settle(1);
|
||||
};
|
||||
|
||||
const rejectWatcherStartupError = (err) => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
shuttingDown = true;
|
||||
if (watchProcess && typeof watchProcess.kill === "function") {
|
||||
watchProcess.kill(WATCH_RESTART_SIGNAL);
|
||||
}
|
||||
releaseWatchLock(lockHandle);
|
||||
watcher?.close?.().catch?.(() => {});
|
||||
if (onSigInt) {
|
||||
deps.process.off("SIGINT", onSigInt);
|
||||
}
|
||||
if (onSigTerm) {
|
||||
deps.process.off("SIGTERM", onSigTerm);
|
||||
}
|
||||
reject(err);
|
||||
};
|
||||
|
||||
const resolveCreateWatcher = async () => {
|
||||
try {
|
||||
const chokidarModule = await deps.loadChokidar();
|
||||
return (watchPaths, options) => chokidarModule.watch(watchPaths, options);
|
||||
} catch (err) {
|
||||
if (isInvalidPackageConfigError(err)) {
|
||||
printFriendlyWatchStartupError(err);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
const runAutoDoctorAndRestart = () => {
|
||||
autoDoctorAttempted = true;
|
||||
logWatcher(
|
||||
@@ -405,8 +425,11 @@ export async function runWatchMain(params = {}) {
|
||||
}
|
||||
};
|
||||
|
||||
const startWatcher = () => {
|
||||
watcher = deps.createWatcher(deps.watchPaths, {
|
||||
const attachWatcher = (createWatcher) => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
watcher = createWatcher(deps.watchPaths, {
|
||||
ignoreInitial: true,
|
||||
ignored: (watchPath, stats) =>
|
||||
isIgnoredWatchPath(watchPath, deps.cwd, deps.watchPaths, stats),
|
||||
@@ -417,6 +440,14 @@ export async function runWatchMain(params = {}) {
|
||||
watcher.on("error", handleWatcherError);
|
||||
};
|
||||
|
||||
const startWatcher = () => {
|
||||
if (deps.createWatcher) {
|
||||
attachWatcher(deps.createWatcher);
|
||||
return;
|
||||
}
|
||||
void resolveCreateWatcher().then(attachWatcher).catch(rejectWatcherStartupError);
|
||||
};
|
||||
|
||||
onSigInt = () => {
|
||||
shuttingDown = true;
|
||||
if (watchProcess && typeof watchProcess.kill === "function") {
|
||||
|
||||
Reference in New Issue
Block a user