Files
admin 3247e9fd15 init: ssw-mvp initial commit
- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
2026-08-05 18:29:12 +08:00

137 lines
4.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import net from 'node:net'
import fs from 'node:fs'
import path from 'node:path'
import { spawn } from 'node:child_process'
import { BE_BASE, BE_PORT, FE_BASE, FE_PORT, LOG_DIR, REPO_DIR } from './config.mjs'
/**
* 服务编排 —— 后端 30000 / 前端 5173。
*
* 原则:**只补位,不接管**。端口已经有人在听,就直接复用,绝不去 kill 或重启别人的实例;
* 只有端口空着时才由我们拉起,并记下 startedByUs,方便结束时只清理自己起的进程。
*/
const started = { be: null, fe: null }
export function isPortOpen(port, host = '127.0.0.1', timeout = 1200) {
return new Promise((resolve) => {
const sock = new net.Socket()
const done = (v) => {
sock.destroy()
resolve(v)
}
sock.setTimeout(timeout)
sock.once('connect', () => done(true))
sock.once('timeout', () => done(false))
sock.once('error', () => done(false))
sock.connect(port, host)
})
}
async function httpAlive(url, timeout = 4000) {
const ac = new AbortController()
const timer = setTimeout(() => ac.abort(), timeout)
try {
// 401/600/404 都算「活着」——只要能应答 HTTP,服务进程就是起来的
await fetch(url, { signal: ac.signal })
return true
} catch {
return false
} finally {
clearTimeout(timer)
}
}
export async function probe() {
const [bePort, fePort] = await Promise.all([isPortOpen(BE_PORT), isPortOpen(FE_PORT)])
const be = bePort ? await httpAlive(`${BE_BASE}/api/oauth/CurrentUser`) : false
const fe = fePort ? await httpAlive(`${FE_BASE}/`) : false
return {
be: { up: be, port: BE_PORT, base: BE_BASE, startedByUs: !!started.be },
fe: { up: fe, port: FE_PORT, base: FE_BASE, startedByUs: !!started.fe }
}
}
function logStream(name) {
fs.mkdirSync(LOG_DIR, { recursive: true })
return fs.openSync(path.join(LOG_DIR, `${name}.log`), 'a')
}
/**
* 拉起后端。命令与 docs/README.md 一致:Redis DB=8、Postgres 15432、端口固定 30000。
* 必须用宿主机网络(这里就是宿主机上直接 spawn),否则连不上映射出来的 Redis/PG 端口。
*/
export async function startBackend(onLog = () => {}) {
if (await isPortOpen(BE_PORT)) {
onLog(`后端 ${BE_PORT} 已在运行,复用现有实例(不重启)`)
return { started: false, reused: true }
}
const jar = path.join(REPO_DIR, 'boot/ssw-admin/target/ssw-admin-7.0.0-RELEASE.jar')
if (!fs.existsSync(jar)) {
throw new Error(`后端 jar 不存在:${jar}(先在 boot/ 下 mvn package)`)
}
const fd = logStream('backend')
onLog(`拉起后端:java -jar ssw-admin-7.0.0-RELEASE.jar(日志 results/logs/backend.log)`)
const child = spawn(
'java',
[
'-DDB_HOST=127.0.0.1', '-DDB_PORT=15432',
'-DREDIS_HOST=127.0.0.1', '-DREDIS_PORT=16379',
`-Dserver.port=${BE_PORT}`,
'-jar', jar
],
{ cwd: path.join(REPO_DIR, 'boot'), detached: true, stdio: ['ignore', fd, fd] }
)
child.unref()
started.be = child.pid
await waitUntil(() => httpAlive(`${BE_BASE}/api/oauth/CurrentUser`), 180_000, 2000,
(s) => onLog(`等待后端就绪…(${s}s)`))
onLog(`后端已就绪 ${BE_BASE}`)
return { started: true, pid: child.pid }
}
export async function startFrontend(onLog = () => {}) {
if (await isPortOpen(FE_PORT)) {
onLog(`前端 ${FE_PORT} 已在运行,复用现有实例(不重启)`)
return { started: false, reused: true }
}
const fd = logStream('frontend')
onLog(`拉起前端:pnpm dev(日志 results/logs/frontend.log)`)
const child = spawn('pnpm', ['dev'], {
cwd: path.join(REPO_DIR, 'web'),
detached: true,
stdio: ['ignore', fd, fd]
})
child.unref()
started.fe = child.pid
await waitUntil(() => httpAlive(`${FE_BASE}/`), 90_000, 1500,
(s) => onLog(`等待前端就绪…(${s}s)`))
onLog(`前端已就绪 ${FE_BASE}`)
return { started: true, pid: child.pid }
}
export async function waitUntil(fn, timeoutMs, intervalMs = 1000, onTick = () => {}) {
const t0 = Date.now()
let ticks = 0
for (;;) {
if (await fn()) return true
if (Date.now() - t0 > timeoutMs) throw new Error(`等待超时(${Math.round(timeoutMs / 1000)}s)`)
if (++ticks % 5 === 0) onTick(Math.round((Date.now() - t0) / 1000))
await new Promise((r) => setTimeout(r, intervalMs))
}
}
/** 只停我们自己起的进程;复用别人的实例时什么都不做 */
export function stopStarted(onLog = () => {}) {
for (const [k, pid] of Object.entries(started)) {
if (!pid) continue
try {
process.kill(-pid, 'SIGTERM')
onLog(`已停止本次自动拉起的 ${k}(pid ${pid})`)
} catch {
/* 已经没了就算了 */
}
started[k] = null
}
}