98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
import { PID_FILE, REFERENCE_COUNT_FILE } from '../constants';
|
|
import { readConfigFile } from '.';
|
|
import find from 'find-process';
|
|
|
|
export async function isProcessRunning(pid: number): Promise<boolean> {
|
|
try {
|
|
const processes = await find('pid', pid);
|
|
return processes.length > 0;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function incrementReferenceCount() {
|
|
let count = 0;
|
|
if (existsSync(REFERENCE_COUNT_FILE)) {
|
|
count = parseInt(readFileSync(REFERENCE_COUNT_FILE, 'utf-8')) || 0;
|
|
}
|
|
count++;
|
|
writeFileSync(REFERENCE_COUNT_FILE, count.toString());
|
|
}
|
|
|
|
export function decrementReferenceCount() {
|
|
let count = 0;
|
|
if (existsSync(REFERENCE_COUNT_FILE)) {
|
|
count = parseInt(readFileSync(REFERENCE_COUNT_FILE, 'utf-8')) || 0;
|
|
}
|
|
count = Math.max(0, count - 1);
|
|
writeFileSync(REFERENCE_COUNT_FILE, count.toString());
|
|
}
|
|
|
|
export function getReferenceCount(): number {
|
|
if (!existsSync(REFERENCE_COUNT_FILE)) {
|
|
return 0;
|
|
}
|
|
return parseInt(readFileSync(REFERENCE_COUNT_FILE, 'utf-8')) || 0;
|
|
}
|
|
|
|
export async function isServiceRunning(): Promise<boolean> {
|
|
if (!existsSync(PID_FILE)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const pid = parseInt(readFileSync(PID_FILE, 'utf-8'));
|
|
return await isProcessRunning(pid);
|
|
} 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 async function getServiceInfo() {
|
|
const pid = getServicePid();
|
|
const running = await isServiceRunning();
|
|
const config = await readConfigFile();
|
|
const port = config.PORT || 3456;
|
|
|
|
return {
|
|
running,
|
|
pid,
|
|
port,
|
|
endpoint: `http://127.0.0.1:${port}`,
|
|
pidFile: PID_FILE,
|
|
referenceCount: getReferenceCount()
|
|
};
|
|
}
|