61 lines
1.3 KiB
TypeScript
61 lines
1.3 KiB
TypeScript
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
import { PID_FILE } from '../constants';
|
|
|
|
|
|
export function isServiceRunning(): boolean {
|
|
if (!existsSync(PID_FILE)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const pid = parseInt(readFileSync(PID_FILE, 'utf-8'));
|
|
process.kill(pid, 0);
|
|
return true;
|
|
} catch (e) {
|
|
// Process not running, clean up pid file
|
|
cleanupPidFile();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function savePid(pid: number) {
|
|
writeFileSync(PID_FILE, pid.toString());
|
|
}
|
|
|
|
export function cleanupPidFile() {
|
|
if (existsSync(PID_FILE)) {
|
|
try {
|
|
const fs = require('fs');
|
|
fs.unlinkSync(PID_FILE);
|
|
} catch (e) {
|
|
// Ignore cleanup errors
|
|
}
|
|
}
|
|
}
|
|
|
|
export function getServicePid(): number | null {
|
|
if (!existsSync(PID_FILE)) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const pid = parseInt(readFileSync(PID_FILE, 'utf-8'));
|
|
return isNaN(pid) ? null : pid;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function getServiceInfo() {
|
|
const pid = getServicePid();
|
|
const running = isServiceRunning();
|
|
|
|
return {
|
|
running,
|
|
pid,
|
|
port: 3456,
|
|
endpoint: 'http://127.0.0.1:3456',
|
|
pidFile: PID_FILE
|
|
};
|
|
}
|