fix the issue of multiple calude using one server by claude code

This commit is contained in:
jinhui.li
2025-06-12 09:58:05 +08:00
parent c9059f146d
commit 84e76f24b0
5 changed files with 67 additions and 13 deletions

View File

@@ -1,21 +1,25 @@
import { isServiceRunning, cleanupPidFile } from './processCheck';
import { existsSync, readFileSync } from 'fs';
import { homedir } from 'os';
import { isServiceRunning, cleanupPidFile, getReferenceCount } from './processCheck';
import { readFileSync } from 'fs';
import { HOME_DIR } from '../constants';
import { join } from 'path';
export async function closeService() {
const PID_FILE = join(homedir(), '.claude-code-router.pid');
const PID_FILE = join(HOME_DIR, '.claude-code-router.pid');
if (!isServiceRunning()) {
console.log("No service is currently running.");
return;
}
if (getReferenceCount() > 0) {
return;
}
try {
const pid = parseInt(readFileSync(PID_FILE, 'utf-8'));
process.kill(pid);
cleanupPidFile();
console.log("Service has been successfully stopped.");
console.log("claude code router service has been successfully stopped.");
} catch (e) {
console.log("Failed to stop the service. It may have already been stopped.");
cleanupPidFile();

View File

@@ -1,5 +1,6 @@
import { spawn } from 'child_process';
import { isServiceRunning } from './processCheck';
import { isServiceRunning, incrementReferenceCount, decrementReferenceCount } from './processCheck';
import { closeService } from './close';
export async function executeCodeCommand(args: string[] = []) {
// Service check is now handled in cli.ts
@@ -13,6 +14,9 @@ export async function executeCodeCommand(args: string[] = []) {
API_TIMEOUT_MS: '600000'
};
// Increment reference count when command starts
incrementReferenceCount();
// Execute claude command
const claudeProcess = spawn('claude', args, {
env,
@@ -23,10 +27,13 @@ export async function executeCodeCommand(args: string[] = []) {
claudeProcess.on('error', (error) => {
console.error('Failed to start claude command:', error.message);
console.log('Make sure Claude Code is installed: npm install -g @anthropic-ai/claude-code');
decrementReferenceCount();
process.exit(1);
});
claudeProcess.on('close', (code) => {
decrementReferenceCount();
closeService()
process.exit(code || 0);
});
}

View File

@@ -1,6 +1,32 @@
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { PID_FILE } from '../constants';
const REFERENCE_COUNT_FILE = '/tmp/claude-code-reference-count.txt';
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 function isServiceRunning(): boolean {
if (!existsSync(PID_FILE)) {
@@ -55,6 +81,7 @@ export function getServiceInfo() {
pid,
port: 3456,
endpoint: 'http://127.0.0.1:3456',
pidFile: PID_FILE
pidFile: PID_FILE,
referenceCount: getReferenceCount()
};
}