feat: NATS 连接与 JetStream 流引导(BPOS_EVT/CMD/DLQ)+touch 发布
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { connect } from 'nats';
|
||||
import type { NatsConnection } from 'nats';
|
||||
import { config } from '../config';
|
||||
|
||||
let nc: NatsConnection | null = null;
|
||||
|
||||
export async function getNats(): Promise<NatsConnection> {
|
||||
if (nc && !nc.isClosed()) return nc;
|
||||
nc = await connect({ servers: config.natsUrl, name: config.serviceName, timeout: 5000 });
|
||||
return nc;
|
||||
}
|
||||
|
||||
export async function checkNats(): Promise<'ok' | 'fail'> {
|
||||
try {
|
||||
const c = await getNats();
|
||||
await c.flush();
|
||||
return 'ok';
|
||||
} catch {
|
||||
return 'fail';
|
||||
}
|
||||
}
|
||||
|
||||
export async function closeNats(): Promise<void> {
|
||||
if (nc) {
|
||||
await nc.drain().catch(() => {});
|
||||
nc = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user