130 lines
3.1 KiB
JavaScript
130 lines
3.1 KiB
JavaScript
|
const { getDBConnection } = require('./file-utils');
|
||
|
|
||
|
// 获取运行中的XNEngine进程
|
||
|
function getRunningXNEngineProcess(pid) {
|
||
|
try {
|
||
|
const db = getDBConnection(true);
|
||
|
|
||
|
// 创建进程表(如果不存在)
|
||
|
db.prepare(`
|
||
|
CREATE TABLE IF NOT EXISTS xnengine_processes (
|
||
|
pid INTEGER PRIMARY KEY,
|
||
|
log_file TEXT,
|
||
|
start_time TEXT,
|
||
|
cmd TEXT,
|
||
|
status TEXT DEFAULT 'running',
|
||
|
scenario_file TEXT
|
||
|
)
|
||
|
`).run();
|
||
|
|
||
|
const process = db.prepare('SELECT * FROM xnengine_processes WHERE pid = ?').get(pid);
|
||
|
|
||
|
return process;
|
||
|
} catch (error) {
|
||
|
console.error('获取XNEngine进程信息失败:', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 保存XNEngine进程信息
|
||
|
function saveXNEngineProcess(processData) {
|
||
|
try {
|
||
|
const db = getDBConnection();
|
||
|
|
||
|
// 创建进程表(如果不存在)
|
||
|
db.prepare(`
|
||
|
CREATE TABLE IF NOT EXISTS xnengine_processes (
|
||
|
pid INTEGER PRIMARY KEY,
|
||
|
log_file TEXT,
|
||
|
start_time TEXT,
|
||
|
cmd TEXT,
|
||
|
status TEXT DEFAULT 'running',
|
||
|
scenario_file TEXT
|
||
|
)
|
||
|
`).run();
|
||
|
|
||
|
const result = db.prepare(`
|
||
|
INSERT INTO xnengine_processes (pid, log_file, start_time, cmd, scenario_file)
|
||
|
VALUES (?, ?, ?, ?, ?)
|
||
|
`).run(
|
||
|
processData.pid,
|
||
|
processData.log_file,
|
||
|
processData.start_time,
|
||
|
processData.cmd,
|
||
|
processData.scenario_file || null
|
||
|
);
|
||
|
|
||
|
return {
|
||
|
success: true,
|
||
|
message: 'XNEngine进程信息保存成功'
|
||
|
};
|
||
|
} catch (error) {
|
||
|
console.error('保存XNEngine进程信息失败:', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 更新XNEngine进程状态
|
||
|
function updateXNEngineProcessStatus(pid, status) {
|
||
|
try {
|
||
|
const db = getDBConnection();
|
||
|
|
||
|
const result = db.prepare(`
|
||
|
UPDATE xnengine_processes
|
||
|
SET status = ?
|
||
|
WHERE pid = ?
|
||
|
`).run(status, pid);
|
||
|
|
||
|
return {
|
||
|
success: true,
|
||
|
message: 'XNEngine进程状态更新成功'
|
||
|
};
|
||
|
} catch (error) {
|
||
|
console.error('更新XNEngine进程状态失败:', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 删除XNEngine进程信息
|
||
|
function deleteXNEngineProcess(pid) {
|
||
|
try {
|
||
|
const db = getDBConnection();
|
||
|
|
||
|
const result = db.prepare('DELETE FROM xnengine_processes WHERE pid = ?').run(pid);
|
||
|
|
||
|
return {
|
||
|
success: true,
|
||
|
message: 'XNEngine进程信息删除成功'
|
||
|
};
|
||
|
} catch (error) {
|
||
|
console.error('删除XNEngine进程信息失败:', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 获取最新的运行中XNEngine进程
|
||
|
function getLatestRunningXNEngineProcess() {
|
||
|
try {
|
||
|
const db = getDBConnection(true);
|
||
|
|
||
|
const process = db.prepare(`
|
||
|
SELECT * FROM xnengine_processes
|
||
|
WHERE status = ?
|
||
|
ORDER BY start_time DESC
|
||
|
LIMIT 1
|
||
|
`).get('running');
|
||
|
|
||
|
return process;
|
||
|
} catch (error) {
|
||
|
console.error('获取最新XNEngine进程信息失败:', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
getRunningXNEngineProcess,
|
||
|
saveXNEngineProcess,
|
||
|
updateXNEngineProcessStatus,
|
||
|
deleteXNEngineProcess,
|
||
|
getLatestRunningXNEngineProcess
|
||
|
};
|