mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
修改服务启动逻辑
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { Server, ServerCredentials } from '@grpc/grpc-js';
|
||||
import { CronService } from '../protos/cron';
|
||||
import { HealthService } from '../protos/health';
|
||||
import { ApiService } from '../protos/api';
|
||||
import { addCron } from '../schedule/addCron';
|
||||
import { delCron } from '../schedule/delCron';
|
||||
import { check } from '../schedule/health';
|
||||
import * as Api from '../schedule/api';
|
||||
import Logger from '../loaders/logger';
|
||||
import { promisify } from 'util';
|
||||
import config from '../config';
|
||||
import { metricsService } from './metrics';
|
||||
import { Service } from 'typedi';
|
||||
|
||||
@Service()
|
||||
export class GrpcServerService {
|
||||
private server: Server = new Server({ 'grpc.enable_http_proxy': 0 });
|
||||
|
||||
async initialize() {
|
||||
try {
|
||||
this.server.addService(HealthService, { check });
|
||||
this.server.addService(CronService, { addCron, delCron });
|
||||
this.server.addService(ApiService, Api);
|
||||
|
||||
const grpcPort = config.grpcPort;
|
||||
const bindAsync = promisify(this.server.bindAsync).bind(this.server);
|
||||
await bindAsync(
|
||||
`0.0.0.0:${grpcPort}`,
|
||||
ServerCredentials.createInsecure(),
|
||||
);
|
||||
Logger.debug(`✌️ gRPC service started successfully`);
|
||||
|
||||
metricsService.record('grpc_service_start', 1, {
|
||||
port: grpcPort.toString(),
|
||||
});
|
||||
|
||||
return grpcPort;
|
||||
} catch (err) {
|
||||
Logger.error('Failed to start gRPC service:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async shutdown() {
|
||||
try {
|
||||
if (this.server) {
|
||||
await new Promise((resolve) => {
|
||||
this.server.tryShutdown(() => {
|
||||
Logger.debug('gRPC service stopped');
|
||||
metricsService.record('grpc_service_stop', 1);
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error('Error while shutting down gRPC service:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
getServer() {
|
||||
return this.server;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { Service } from 'typedi';
|
||||
import Logger from '../loaders/logger';
|
||||
import { GrpcServerService } from './grpc';
|
||||
import { HttpServerService } from './http';
|
||||
|
||||
interface HealthStatus {
|
||||
status: 'ok' | 'error';
|
||||
services: {
|
||||
http: boolean;
|
||||
grpc: boolean;
|
||||
};
|
||||
metrics: {
|
||||
uptime: number;
|
||||
memory: {
|
||||
used: number;
|
||||
total: number;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@Service()
|
||||
export class HealthService {
|
||||
private startTime = Date.now();
|
||||
|
||||
constructor(
|
||||
private grpcServerService: GrpcServerService,
|
||||
private httpServerService: HttpServerService,
|
||||
) {}
|
||||
|
||||
async check(): Promise<HealthStatus> {
|
||||
const status: HealthStatus = {
|
||||
status: 'ok',
|
||||
services: {
|
||||
http: true,
|
||||
grpc: true,
|
||||
},
|
||||
metrics: {
|
||||
uptime: Math.floor((Date.now() - this.startTime) / 1000),
|
||||
memory: {
|
||||
used: process.memoryUsage().heapUsed,
|
||||
total: process.memoryUsage().heapTotal,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const httpServer = this.httpServerService.getServer();
|
||||
if (!httpServer) {
|
||||
status.services.http = false;
|
||||
status.status = 'error';
|
||||
}
|
||||
} catch (err) {
|
||||
status.services.http = false;
|
||||
status.status = 'error';
|
||||
Logger.error('HTTP server check failed:', err);
|
||||
}
|
||||
|
||||
try {
|
||||
const grpcServer = this.grpcServerService.getServer();
|
||||
if (!grpcServer) {
|
||||
status.services.grpc = false;
|
||||
status.status = 'error';
|
||||
}
|
||||
} catch (err) {
|
||||
status.services.grpc = false;
|
||||
status.status = 'error';
|
||||
Logger.error('gRPC server check failed:', err);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import express from 'express';
|
||||
import Logger from '../loaders/logger';
|
||||
import { metricsService } from './metrics';
|
||||
import { Service } from 'typedi';
|
||||
import { Server } from 'http';
|
||||
|
||||
@Service()
|
||||
export class HttpServerService {
|
||||
private server?: Server = undefined;
|
||||
|
||||
async initialize(expressApp: express.Application, port: number) {
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.server = expressApp.listen(port, '0.0.0.0', () => {
|
||||
Logger.debug(`✌️ HTTP service started successfully`);
|
||||
metricsService.record('http_service_start', 1, {
|
||||
port: port.toString(),
|
||||
});
|
||||
resolve(this.server);
|
||||
});
|
||||
|
||||
this.server.on('error', (err: Error) => {
|
||||
Logger.error('Failed to start HTTP service:', err);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
Logger.error('Failed to start HTTP service:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async shutdown() {
|
||||
try {
|
||||
if (this.server) {
|
||||
await new Promise((resolve) => {
|
||||
this.server?.close(() => {
|
||||
Logger.debug('HTTP service stopped');
|
||||
metricsService.record('http_service_stop', 1);
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error('Error while shutting down HTTP service:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
getServer() {
|
||||
return this.server;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { performance } from 'perf_hooks';
|
||||
import Logger from '../loaders/logger';
|
||||
|
||||
interface Metric {
|
||||
name: string;
|
||||
value: number;
|
||||
timestamp: number;
|
||||
tags?: Record<string, string>;
|
||||
}
|
||||
|
||||
class MetricsService {
|
||||
private metrics: Metric[] = [];
|
||||
private static instance: MetricsService;
|
||||
|
||||
private constructor() {
|
||||
// 定期清理旧数据
|
||||
setInterval(() => {
|
||||
const oneHourAgo = Date.now() - 3600000;
|
||||
this.metrics = this.metrics.filter(m => m.timestamp > oneHourAgo);
|
||||
}, 60000);
|
||||
}
|
||||
|
||||
static getInstance(): MetricsService {
|
||||
if (!MetricsService.instance) {
|
||||
MetricsService.instance = new MetricsService();
|
||||
}
|
||||
return MetricsService.instance;
|
||||
}
|
||||
|
||||
record(name: string, value: number, tags?: Record<string, string>) {
|
||||
this.metrics.push({
|
||||
name,
|
||||
value,
|
||||
timestamp: Date.now(),
|
||||
tags,
|
||||
});
|
||||
}
|
||||
|
||||
measure(name: string, fn: () => void, tags?: Record<string, string>) {
|
||||
const start = performance.now();
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
const duration = performance.now() - start;
|
||||
this.record(name, duration, tags);
|
||||
}
|
||||
}
|
||||
|
||||
async measureAsync(name: string, fn: () => Promise<void>, tags?: Record<string, string>) {
|
||||
const start = performance.now();
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
const duration = performance.now() - start;
|
||||
this.record(name, duration, tags);
|
||||
}
|
||||
}
|
||||
|
||||
getMetrics(name?: string, tags?: Record<string, string>) {
|
||||
let filtered = this.metrics;
|
||||
|
||||
if (name) {
|
||||
filtered = filtered.filter(m => m.name === name);
|
||||
}
|
||||
|
||||
if (tags) {
|
||||
filtered = filtered.filter(m => {
|
||||
if (!m.tags) return false;
|
||||
return Object.entries(tags).every(([key, value]) => m.tags![key] === value);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
count: filtered.length,
|
||||
average: filtered.reduce((acc, curr) => acc + curr.value, 0) / filtered.length,
|
||||
min: Math.min(...filtered.map(m => m.value)),
|
||||
max: Math.max(...filtered.map(m => m.value)),
|
||||
metrics: filtered,
|
||||
};
|
||||
}
|
||||
|
||||
report() {
|
||||
const report = {
|
||||
timestamp: Date.now(),
|
||||
metrics: this.getMetrics(),
|
||||
};
|
||||
Logger.info('性能指标报告:', report);
|
||||
return report;
|
||||
}
|
||||
}
|
||||
|
||||
export const metricsService = MetricsService.getInstance();
|
||||
@@ -357,8 +357,15 @@ export default class SystemService {
|
||||
|
||||
public async reloadSystem(target?: 'system' | 'data') {
|
||||
const cmd = `real_time=true ql reload ${target || ''}`;
|
||||
const cp = spawn(cmd, { shell: '/bin/bash' });
|
||||
const cp = spawn(cmd, {
|
||||
shell: '/bin/bash',
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
});
|
||||
cp.unref();
|
||||
setTimeout(() => {
|
||||
process.exit(0);
|
||||
});
|
||||
return { code: 200 };
|
||||
}
|
||||
|
||||
|
||||
+11
-4
@@ -93,9 +93,9 @@ export default class UserService {
|
||||
}
|
||||
if (username === cUsername && password === cPassword) {
|
||||
const data = createRandomString(50, 100);
|
||||
const expiration = twoFactorActivated ? 60 : 20;
|
||||
let token = jwt.sign({ data }, config.secret as any, {
|
||||
expiresIn: 60 * 60 * 24 * expiration,
|
||||
const expiration = twoFactorActivated ? '60d' : '20d';
|
||||
let token = jwt.sign({ data }, config.jwt.secret, {
|
||||
expiresIn: config.jwt.expiresIn || expiration,
|
||||
algorithm: 'HS384',
|
||||
});
|
||||
|
||||
@@ -131,7 +131,14 @@ export default class UserService {
|
||||
this.getLoginLog();
|
||||
return {
|
||||
code: 200,
|
||||
data: { token, lastip, lastaddr, lastlogon, retries, platform },
|
||||
data: {
|
||||
token,
|
||||
lastip,
|
||||
lastaddr,
|
||||
lastlogon,
|
||||
retries,
|
||||
platform,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
await this.updateAuthInfo(content, {
|
||||
|
||||
Reference in New Issue
Block a user