mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 01:39:52 +08:00
Return committed trigger IDs and add CLI wait and trigger-specific logs while preserving API, CLI, transaction, deduplication and unknown-result semantics. Move scheduling, lifecycle, settlement, queries and legacy adapters into back/cron; remove obsolete internal import paths and circular dependencies. Apply project formatting across the repository, cover CJS/MJS in formatting hooks, add prettier:check and require formatting checks in AGENTS.md. Validation: backend build; 1398 backend/frontend tests passed (17 platform skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
108 lines
3.1 KiB
TypeScript
108 lines
3.1 KiB
TypeScript
import { Server, ServerCredentials } from '@grpc/grpc-js';
|
|
import { CronService } from '../protos/cron';
|
|
import { HealthService } from '../protos/health';
|
|
import { ApiService } from '../protos/api';
|
|
import { registeredAdd, registeredDelete } from '../schedule/registeredPlans';
|
|
import { setConcurrency } from '../schedule/setConcurrency';
|
|
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';
|
|
import { initGrpcCerts } from '../config/grpcCerts';
|
|
|
|
@Service()
|
|
export class GrpcServerService {
|
|
private server: Server = new Server({ 'grpc.enable_http_proxy': 0 });
|
|
|
|
private formatGrpcAddress(host: string, port: number): string {
|
|
if (host === '::') {
|
|
return `[::]:${port}`;
|
|
}
|
|
return `${host}:${port}`;
|
|
}
|
|
|
|
async initialize() {
|
|
try {
|
|
this.server.addService(HealthService, { check });
|
|
this.server.addService(CronService, {
|
|
addCron: registeredAdd,
|
|
delCron: registeredDelete,
|
|
setConcurrency,
|
|
});
|
|
this.server.addService(ApiService, Api);
|
|
|
|
const tlsConfig = await initGrpcCerts();
|
|
const credentials = ServerCredentials.createSsl(
|
|
Buffer.from(tlsConfig.caCert),
|
|
[
|
|
{
|
|
cert_chain: Buffer.from(tlsConfig.serverCert),
|
|
private_key: Buffer.from(tlsConfig.serverKey),
|
|
},
|
|
],
|
|
true,
|
|
);
|
|
|
|
const grpcPort = config.grpcPort;
|
|
const hostsToTry = [
|
|
config.bindHostGrpc,
|
|
...(config.bindHostGrpc === '::' ? ['0.0.0.0'] : []),
|
|
];
|
|
const bindAsync = promisify(this.server.bindAsync).bind(this.server);
|
|
|
|
let lastError: Error | null = null;
|
|
|
|
for (const host of hostsToTry) {
|
|
try {
|
|
const address = this.formatGrpcAddress(host, grpcPort);
|
|
const boundPort = await bindAsync(address, credentials);
|
|
Logger.debug(
|
|
`[boot] gRPC service started successfully on ${address}`,
|
|
);
|
|
metricsService.record('grpc_service_start', 1, {
|
|
port: grpcPort.toString(),
|
|
host,
|
|
});
|
|
return boundPort;
|
|
} catch (err) {
|
|
lastError = err as Error;
|
|
Logger.warn(
|
|
`Failed to bind gRPC on ${host}:${grpcPort}, trying next...`,
|
|
err,
|
|
);
|
|
}
|
|
}
|
|
|
|
Logger.error('Failed to start gRPC service on all hosts');
|
|
throw lastError || new Error('Failed to start gRPC service');
|
|
} 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;
|
|
}
|
|
}
|