mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 02:21:07 +08:00
* feat(desktop): add database slimming maintenance Signed-off-by: DavidShen <david@xd.com> * fix(desktop): harden database cleanup workflow Signed-off-by: DavidShen <david@xd.com> * fix(desktop): harden database cleanup recovery Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 收敛数据库清理反馈 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 保留数据库清理恢复标记 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 修复数据库清理索引与交互反馈 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): preserve database cleanup recovery markers Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 保留数据库清理回滚标记 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 保留数据库清理失败标记 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 保留数据库清理调度请求 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 加固数据库清理恢复状态 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 加固数据库清理启动失败处理 Signed-off-by: DavidShen <david@xd.com> * fix(desktop): 等待数据库清理进程确认退出 Signed-off-by: DavidShen <david@xd.com> --------- Signed-off-by: DavidShen <david@xd.com>
153 lines
4.9 KiB
JavaScript
153 lines
4.9 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Internal TTY-preserving runner for restart-desktop-remote.mjs.
|
|
* Electron writes the ready status; this runner records an early pnpm/Forge exit.
|
|
*/
|
|
import { spawn } from 'node:child_process';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { resolvePnpmInvocation, usablePnpmExecPath } from './shared/pnpm-invocation.mjs';
|
|
|
|
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
const mode = process.argv[2];
|
|
if (mode !== 'remote' && mode !== 'local') {
|
|
console.error(`desktop-dev-runner: mode must be remote or local, received ${mode ?? '(empty)'}`);
|
|
process.exit(2);
|
|
}
|
|
|
|
const devScript = mode === 'local' ? 'dev:desktop' : 'dev:desktop:remote';
|
|
// The restart pipeline opens a fresh Windows cmd.exe. That environment does
|
|
// not always carry npm_execpath, and may carry a stale one, so the path is
|
|
// validated before use; resolvePnpmInvocation then decides how to execute it
|
|
// (JS entry via node, native binary directly, command wrapper through PATH),
|
|
// and falls back to PATH/PATHEXT resolution when nothing usable is left.
|
|
const invocation = resolvePnpmInvocation([devScript], {
|
|
npmExecPath: usablePnpmExecPath(process.env.npm_execpath, fs.existsSync),
|
|
});
|
|
const { command, args } = invocation;
|
|
const relaunchSignalPath = path.join(
|
|
os.tmpdir(),
|
|
`cindy-desktop-dev-relaunch-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.json`,
|
|
);
|
|
const restartedRequestIds = new Set();
|
|
|
|
startChild();
|
|
|
|
function startChild() {
|
|
const child = spawn(command, args, {
|
|
cwd: rootDir,
|
|
env: {
|
|
...process.env,
|
|
COREPACK_ENABLE_AUTO_PIN: '0',
|
|
XDT_DESKTOP_DEV_RELAUNCH_SIGNAL_FILE: relaunchSignalPath,
|
|
...(invocation.env ?? {}),
|
|
},
|
|
stdio: 'inherit',
|
|
windowsHide: false,
|
|
shell: invocation.shell,
|
|
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
|
|
});
|
|
|
|
child.once('error', (error) => {
|
|
fs.rmSync(relaunchSignalPath, { force: true });
|
|
writeFailedStatus({ exitCode: null, error: error.message });
|
|
console.error(`desktop-dev-runner: ${error.message}`);
|
|
process.exit(1);
|
|
});
|
|
|
|
child.once('exit', (code, signal) => {
|
|
const relaunchRequest = consumeRelaunchRequest();
|
|
if (relaunchRequest && !restartedRequestIds.has(relaunchRequest.requestId)) {
|
|
restartedRequestIds.add(relaunchRequest.requestId);
|
|
console.log(
|
|
`desktop-dev-runner: restarting Forge/Vite for database cleanup ${relaunchRequest.requestId}`,
|
|
);
|
|
startChild();
|
|
return;
|
|
}
|
|
if (relaunchRequest) {
|
|
console.error(
|
|
`desktop-dev-runner: refused repeated database cleanup relaunch ${relaunchRequest.requestId}`,
|
|
);
|
|
}
|
|
|
|
const statusPath = process.env.XDT_DESKTOP_DEV_STARTUP_STATUS_FILE;
|
|
const existing = statusPath ? readStatus(statusPath) : null;
|
|
if (statusPath) {
|
|
if (existing?.state === 'ready' || existing?.state === 'abandoned' || !existing) {
|
|
fs.rmSync(statusPath, { force: true });
|
|
} else if (existing.state !== 'failed') {
|
|
writeFailedStatus({ exitCode: code, signal });
|
|
}
|
|
}
|
|
|
|
if (signal && process.platform !== 'win32') {
|
|
process.kill(process.pid, signal);
|
|
return;
|
|
}
|
|
process.exit(code ?? 1);
|
|
});
|
|
}
|
|
|
|
function consumeRelaunchRequest() {
|
|
try {
|
|
const value = JSON.parse(fs.readFileSync(relaunchSignalPath, 'utf8'));
|
|
if (
|
|
value?.version !== 1 ||
|
|
typeof value.requestId !== 'string' ||
|
|
value.requestId.length === 0 ||
|
|
value.requestId.length > 128
|
|
) {
|
|
return null;
|
|
}
|
|
return { requestId: value.requestId };
|
|
} catch {
|
|
return null;
|
|
} finally {
|
|
fs.rmSync(relaunchSignalPath, { force: true });
|
|
}
|
|
}
|
|
|
|
function readStatus(statusPath) {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(statusPath, 'utf8'));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function writeFailedStatus(detail) {
|
|
const statusPath = process.env.XDT_DESKTOP_DEV_STARTUP_STATUS_FILE;
|
|
const state = statusPath ? readStatus(statusPath)?.state : null;
|
|
if (!statusPath || state === 'ready' || state === 'abandoned') return;
|
|
const errorMessage = detail.error ? ` Desktop dev command failed: ${detail.error}` : '';
|
|
writeStatus(statusPath, {
|
|
state: 'failed',
|
|
code: 'DEV_PROCESS_EXITED',
|
|
message: `The desktop dev process exited before the main window became ready.${errorMessage}`,
|
|
detail: {
|
|
rootDir,
|
|
command,
|
|
devScript,
|
|
...detail,
|
|
},
|
|
...detail,
|
|
pid: process.pid,
|
|
at: Date.now(),
|
|
});
|
|
}
|
|
|
|
function writeStatus(statusPath, status) {
|
|
const tempPath = `${statusPath}.${process.pid}.tmp`;
|
|
try {
|
|
fs.writeFileSync(tempPath, `${JSON.stringify(status)}\n`, { mode: 0o600 });
|
|
fs.renameSync(tempPath, statusPath);
|
|
} catch (error) {
|
|
fs.rmSync(tempPath, { force: true });
|
|
console.error(`desktop-dev-runner: failed to write startup status: ${error.message}`);
|
|
}
|
|
}
|