feat: dev.py 新增 --stop 并支持安装完成后自动切换到开发模式
- 本地 dev 保持前台模型、不引入 PM2;--stop 复用 kill_project_servers 一键停止 - 无 .env 时监听根 .env 出现作为安装成功信号,自动停安装 API 并以正式模式重启 api/client,避开安装中重启 - 安装 API 在 dev 模式下不再退出、迁移失败透传真实原因;前端提交后统一轮询就绪再进后台
This commit is contained in:
@@ -231,6 +231,9 @@
|
||||
"cdnHint": "CDN acceleration URL; leave empty to disable (empty = serve from origin directly).",
|
||||
"failed": "Installation failed",
|
||||
"done": "Initialized. The service is restarting, please wait…",
|
||||
"doneLocalDev": "Setup complete. If it does not open the admin automatically, rerun python scripts/dev.py to enter normal mode.",
|
||||
"emptyResponse": "The server returned no valid response. Retry later or check server logs.",
|
||||
"invalidResponse": "The server returned invalid data",
|
||||
"loadFailed": "Failed to read installation status",
|
||||
"testConnection": "Test",
|
||||
"testOk": "Connected",
|
||||
|
||||
@@ -231,6 +231,9 @@
|
||||
"cdnHint": "CDN 加速地址;如不启用可留空(为空则不开启,直接走源站)。",
|
||||
"failed": "安装失败",
|
||||
"done": "初始化完成,服务正在重启,请稍候…",
|
||||
"doneLocalDev": "初始化完成。若未自动进入后台,请重新运行 python scripts/dev.py 进入正式模式。",
|
||||
"emptyResponse": "服务器未返回有效响应,请稍后重试或查看服务端日志",
|
||||
"invalidResponse": "服务器返回了无效数据",
|
||||
"loadFailed": "读取安装状态失败",
|
||||
"testConnection": "测试连接",
|
||||
"testOk": "连接成功",
|
||||
|
||||
@@ -225,6 +225,41 @@ function goAdmin(): void {
|
||||
location.href = '/admin';
|
||||
}
|
||||
|
||||
interface ApiBody {
|
||||
code: number;
|
||||
message?: string;
|
||||
data?: {
|
||||
installed?: boolean;
|
||||
needsDevRestart?: boolean;
|
||||
message?: string;
|
||||
restartRequired?: boolean;
|
||||
defaults?: Record<string, string | number>;
|
||||
};
|
||||
}
|
||||
|
||||
async function parseApiBody(res: Response): Promise<ApiBody | null> {
|
||||
const text = await res.text();
|
||||
if (!text.trim()) return null;
|
||||
try {
|
||||
return JSON.parse(text) as ApiBody;
|
||||
} catch {
|
||||
throw new Error(tt('invalidResponse'));
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchInstallStatus(): Promise<{ installed: boolean; needsDevRestart: boolean }> {
|
||||
try {
|
||||
const res = await fetch('/api/install/status');
|
||||
const body = await parseApiBody(res);
|
||||
return {
|
||||
installed: Boolean(body?.data?.installed),
|
||||
needsDevRestart: Boolean(body?.data?.needsDevRestart),
|
||||
};
|
||||
} catch {
|
||||
return { installed: false, needsDevRestart: false };
|
||||
}
|
||||
}
|
||||
|
||||
async function post(path: string, payload: Record<string, unknown>): Promise<{ ok: boolean; msg: string }> {
|
||||
try {
|
||||
const res = await fetch(path, {
|
||||
@@ -232,7 +267,8 @@ async function post(path: string, payload: Record<string, unknown>): Promise<{ o
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const body = await res.json();
|
||||
const body = await parseApiBody(res);
|
||||
if (!body) throw new Error(tt('emptyResponse'));
|
||||
if (!res.ok || body.code !== 0) throw new Error(body.message || tt('testFailed'));
|
||||
return { ok: true, msg: body.data?.message || tt('testOk') };
|
||||
} catch (e) {
|
||||
@@ -300,7 +336,8 @@ async function onNext(): Promise<void> {
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/install/status');
|
||||
const body = await res.json();
|
||||
const body = await parseApiBody(res);
|
||||
if (!body) throw new Error(tt('emptyResponse'));
|
||||
installed.value = Boolean(body.data?.installed);
|
||||
const d = body.data?.defaults || {};
|
||||
form.mysql.host = d.mysqlHost || form.mysql.host;
|
||||
@@ -318,18 +355,13 @@ onMounted(async () => {
|
||||
}
|
||||
});
|
||||
|
||||
async function pollUntilReady(): Promise<void> {
|
||||
async function pollUntilReady(): Promise<boolean> {
|
||||
for (let i = 0; i < 30; i += 1) {
|
||||
await new Promise((r) => setTimeout(r, 2000));
|
||||
try {
|
||||
const res = await fetch('/api/install/status');
|
||||
const body = await res.json();
|
||||
if (body.data?.installed) break;
|
||||
} catch {
|
||||
// 服务重启期间请求失败属正常
|
||||
}
|
||||
const st = await fetchInstallStatus();
|
||||
if (st.installed && !st.needsDevRestart) return true;
|
||||
}
|
||||
goAdmin();
|
||||
return false;
|
||||
}
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
@@ -345,10 +377,22 @@ async function submit(): Promise<void> {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
const body = await res.json();
|
||||
if (!res.ok || body.code !== 0) throw new Error(body.message || tt('failed'));
|
||||
const body = await parseApiBody(res);
|
||||
// body 为空 = 安装成功后连接被 API 重启掐断(生产 PM2/Docker 或本地 dev.py 自动切换),视为已提交;
|
||||
// body 有值且非 0 = 安装校验失败(如 MySQL 连接失败),抛出真实原因。
|
||||
if (body && (!res.ok || body.code !== 0)) {
|
||||
throw new Error(body.message || tt('failed'));
|
||||
}
|
||||
installed.value = true;
|
||||
success.value = tt('done');
|
||||
await pollUntilReady();
|
||||
// 统一轮询等待系统就绪:本地 dev.py 会自动停掉安装 API 并以正式模式重启,
|
||||
// 生产由 PM2/Docker 按重启策略拉起;就绪后进入后台。
|
||||
if (await pollUntilReady()) {
|
||||
goAdmin();
|
||||
} else {
|
||||
// 超时兜底:多为 --install-mode 调试模式(写临时 .env,不会自动切换)
|
||||
success.value = tt('doneLocalDev');
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = (e as Error).message;
|
||||
} finally {
|
||||
|
||||
@@ -20,8 +20,12 @@ const ENCRYPTED_PREFIX = 'enc:v1:';
|
||||
export class InstallService {
|
||||
status() {
|
||||
const prefillSecrets = process.env.INSTALL_PREFILL_SECRETS === '1';
|
||||
const envPath = envFilePath();
|
||||
const envReady = existsSync(envPath);
|
||||
return {
|
||||
installed: !isInstallMode(),
|
||||
// 本地 --install-mode 调试:.env 已写入但当前进程仍是 InstallAppModule,需重启 dev.py
|
||||
needsDevRestart: process.env.ARTVERIS_INSTALL_MODE === '1' && envReady,
|
||||
defaults: {
|
||||
mysqlHost: process.env.INSTALL_MYSQL_HOST || '127.0.0.1',
|
||||
mysqlPort: Number(process.env.INSTALL_MYSQL_PORT || 3306),
|
||||
@@ -48,7 +52,11 @@ export class InstallService {
|
||||
this.runPrismaMigrate(databaseUrl);
|
||||
await this.seedInstallData(databaseUrl, dto, traceCodeSalt, settingsEncryptionKey);
|
||||
this.writeEnvFile(dto, databaseUrl, traceCodeSalt, settingsEncryptionKey);
|
||||
setTimeout(() => process.exit(0), 600);
|
||||
// 本地安装向导调试(ARTVERIS_INSTALL_MODE=1)不主动退出,避免响应未返回连接被掐断;
|
||||
// 生产环境由 PM2/Docker restart policy 重启以加载新 .env。
|
||||
if (process.env.ARTVERIS_INSTALL_MODE !== '1') {
|
||||
setTimeout(() => process.exit(0), 2500);
|
||||
}
|
||||
return { installed: true, restartRequired: true };
|
||||
}
|
||||
|
||||
@@ -298,10 +306,14 @@ export class InstallService {
|
||||
|
||||
private runPrismaMigrate(databaseUrl: string): void {
|
||||
const prismaBin = require.resolve('prisma/build/index.js');
|
||||
execFileSync(process.execPath, [prismaBin, 'migrate', 'deploy', '--schema', this.prismaSchemaPath()], {
|
||||
stdio: 'inherit',
|
||||
env: { ...process.env, DATABASE_URL: databaseUrl },
|
||||
});
|
||||
try {
|
||||
execFileSync(process.execPath, [prismaBin, 'migrate', 'deploy', '--schema', this.prismaSchemaPath()], {
|
||||
stdio: 'inherit',
|
||||
env: { ...process.env, DATABASE_URL: databaseUrl },
|
||||
});
|
||||
} catch (e) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `数据库迁移失败:${this.errorMessage(e)}`);
|
||||
}
|
||||
}
|
||||
|
||||
private prismaSchemaPath(): string {
|
||||
|
||||
Reference in New Issue
Block a user