|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * CLI command to start the management server for health checks and metrics |
| 4 | + */ |
| 5 | +import { Command } from 'commander'; |
| 6 | +import { ModuleSystem } from '../module-system'; |
| 7 | +import { loadConfig } from '../config'; |
| 8 | +import { StructuredLoggerFactory } from '../module-system/structured-logger'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as fs from 'fs'; |
| 11 | + |
| 12 | +const logger = StructuredLoggerFactory.getLogger('serve-command'); |
| 13 | + |
| 14 | +export function createServeCommand(): Command { |
| 15 | + const cmd = new Command('serve'); |
| 16 | + |
| 17 | + cmd |
| 18 | + .description('Start the management server for health checks and metrics') |
| 19 | + .option('-p, --port <port>', 'Port to listen on', '8080') |
| 20 | + .option('-c, --config <file>', 'Path to configuration file') |
| 21 | + .option('--production', 'Enable production mode with all safety features') |
| 22 | + .option('--json', 'Use structured JSON logging') |
| 23 | + .action(async options => { |
| 24 | + try { |
| 25 | + // Load configuration |
| 26 | + const config = options.config |
| 27 | + ? JSON.parse(fs.readFileSync(path.resolve(options.config), 'utf8')) |
| 28 | + : loadConfig(process.cwd()); |
| 29 | + |
| 30 | + const port = parseInt(options.port, 10); |
| 31 | + const isProduction = options.production || process.env.NODE_ENV === 'production'; |
| 32 | + |
| 33 | + // Configure structured logging if requested |
| 34 | + if (options.json) { |
| 35 | + StructuredLoggerFactory.configure({ format: 'json' }); |
| 36 | + } |
| 37 | + |
| 38 | + logger.info('Starting management server', { |
| 39 | + port, |
| 40 | + production: isProduction, |
| 41 | + jsonLogging: options.json, |
| 42 | + }); |
| 43 | + |
| 44 | + // Create module system with production features |
| 45 | + const moduleSystem = new ModuleSystem({ |
| 46 | + resolution: { |
| 47 | + baseUrl: process.cwd(), |
| 48 | + ...config.moduleSystem?.resolution, |
| 49 | + }, |
| 50 | + metrics: true, |
| 51 | + circuitBreakers: true, |
| 52 | + logger: true, |
| 53 | + managementServer: true, |
| 54 | + managementPort: port, |
| 55 | + resourceLimits: { |
| 56 | + maxMemoryBytes: 1024 * 1024 * 1024, // 1GB |
| 57 | + maxFileHandles: 1000, |
| 58 | + maxCachedModules: 10000, |
| 59 | + ...config.moduleSystem?.resourceLimits, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + // Start the management server |
| 64 | + const actualPort = await moduleSystem.startManagementServer(port); |
| 65 | + |
| 66 | + if (actualPort) { |
| 67 | + logger.info(`Management server started successfully`, { |
| 68 | + port: actualPort, |
| 69 | + endpoints: { |
| 70 | + health: `http://localhost:${actualPort}/health`, |
| 71 | + ready: `http://localhost:${actualPort}/health/ready`, |
| 72 | + metrics: `http://localhost:${actualPort}/metrics`, |
| 73 | + prometheus: `http://localhost:${actualPort}/metrics/prometheus`, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + // Handle graceful shutdown |
| 78 | + const shutdown = async () => { |
| 79 | + logger.info('Shutting down management server...'); |
| 80 | + await moduleSystem.shutdown(); |
| 81 | + process.exit(0); |
| 82 | + }; |
| 83 | + |
| 84 | + process.on('SIGTERM', shutdown); |
| 85 | + process.on('SIGINT', shutdown); |
| 86 | + process.on('SIGHUP', shutdown); |
| 87 | + |
| 88 | + // Keep the process alive |
| 89 | + process.stdin.resume(); |
| 90 | + } else { |
| 91 | + logger.error('Failed to start management server'); |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + logger.error('Error starting management server', error as Error); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + return cmd; |
| 101 | +} |
0 commit comments