Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 1x 1x 25x 25x 25x 25x 1x 1x 1x 1x 1x 23x 23x 23x 2x 1x 1x 2x 2x 21x 23x 2x 1x 1x 2x 2x 19x 19x 23x 1x 1x 160x 160x 160x 160x 160x 160x 160x 18x 18x 160x 142x 142x 160x 1x 1x 19x 6x 6x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 16x 16x 16x 16x 16x 16x 16x 16x 16x 5x 5x 19x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 1x 19x 19x 19x 19x 19x 19x 1x 1x 1x 19x 1x 1x 18x 18x 18x 18x 137x 16x 6x 6x 16x 16x 121x 121x 2x 2x 2x 18x 1x 1x 4x 3x 1x 1x 3x 3x 3x 4x 1x 1x 5x 5x 1x | /**
* Service Manager - Auto-start and lifecycle management
*/
import { spawn, type ChildProcess } from 'child_process';
import type { TTSProviderConfig } from './config.js';
export class ServiceManager {
private config: Required<TTSProviderConfig>;
private baseUrl: string;
private serverProcess: ChildProcess | null = null;
private verbose: boolean;
constructor(config: Required<TTSProviderConfig>) {
this.config = config;
this.baseUrl = `${config.serverUrl}:${config.port}`;
this.verbose = config.verbose;
}
/**
* Ensure the service is running
*/
async ensureRunning(): Promise<boolean> {
const health = await this.checkHealth();
if (health) {
if (this.verbose) {
console.log('[TTSServiceManager] Service already running');
}
return true;
}
if (!this.config.autoStart) {
if (this.verbose) {
console.log('[TTSServiceManager] Service not running, auto-start disabled');
}
return false;
}
return this.startService();
}
private async checkHealth(): Promise<boolean> {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.config.healthCheckTimeout);
const response = await fetch(`${this.baseUrl}/health`, {
signal: controller.signal,
});
clearTimeout(timeoutId);
return response.ok;
} catch {
return false;
}
}
private async startService(): Promise<boolean> {
if (this.verbose) {
console.log('[TTSServiceManager] Starting TTS service...');
}
try {
// Detect service type based on path
const isChatterbox = this.config.servicePath.includes('chatterbox');
let command: string;
let args: string[];
let env: NodeJS.ProcessEnv;
if (isChatterbox) {
// Python Chatterbox service
command = 'python';
args = ['-m', 'uvicorn', 'chatterbox_tts_service.main:app', '--host', '0.0.0.0', '--port', this.config.port.toString()];
env = {
...process.env,
PYTHONPATH: `${this.config.servicePath}/python`,
CHATTERBOX_PORT: this.config.port.toString(),
};
if (this.verbose) {
console.log('[TTSServiceManager] Detected Chatterbox (Python) service');
}
} else {
// Legacy Node.js service
command = 'npm';
args = ['run', 'dev'];
env = {
...process.env,
PORT: this.config.port.toString(),
};
if (this.verbose) {
console.log('[TTSServiceManager] Detected legacy (Node.js) service');
}
}
this.serverProcess = spawn(command, args, {
cwd: this.config.servicePath,
env,
stdio: this.verbose ? 'inherit' : 'pipe',
detached: false,
shell: true,
});
this.serverProcess.on('error', (error) => {
console.error('[TTSServiceManager] Failed to start service:', error);
});
// Chatterbox needs longer startup time for model loading
const timeout = isChatterbox ? 120000 : 30000;
return await this.waitForHealth(timeout);
} catch (error) {
console.error('[TTSServiceManager] Error starting service:', error);
return false;
}
}
private async waitForHealth(timeoutMs: number): Promise<boolean> {
const startTime = Date.now();
const pollInterval = 500;
while (Date.now() - startTime < timeoutMs) {
if (await this.checkHealth()) {
if (this.verbose) {
console.log(`[TTSServiceManager] Service ready (took ${Date.now() - startTime}ms)`);
}
return true;
}
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
console.error('[TTSServiceManager] Timeout waiting for service to start');
return false;
}
async stop(): Promise<void> {
if (this.serverProcess) {
if (this.verbose) {
console.log('[TTSServiceManager] Stopping managed service...');
}
this.serverProcess.kill();
this.serverProcess = null;
}
}
isManaging(): boolean {
return this.serverProcess !== null && !this.serverProcess.killed;
}
}
|