Change method of fetching hostname to use native os.hostname
This commit is contained in:
38
src/nats.ts
38
src/nats.ts
@@ -1,6 +1,8 @@
|
||||
import { connect, JSONCodec } from "nats";
|
||||
import type { ConnectionOptions, NatsConnection, Payload } from "nats";
|
||||
import { log } from "./logger";
|
||||
import { hostname } from "node:os";
|
||||
import type { MQFileUpdate } from "@owlboard/backend-data-contracts/dist/data-ingress_mq-file-update";
|
||||
|
||||
const jc = JSONCodec();
|
||||
|
||||
@@ -9,7 +11,7 @@ async function getNatsConnection(): Promise<NatsConnection> {
|
||||
|
||||
const options: ConnectionOptions = {
|
||||
servers: serverUrl,
|
||||
name: `${process.env.HOSTNAME}` || 'local',
|
||||
name: hostname(),
|
||||
reconnect: true,
|
||||
maxReconnectAttempts: -1,
|
||||
};
|
||||
@@ -25,4 +27,36 @@ async function getNatsConnection(): Promise<NatsConnection> {
|
||||
return await connect(options)
|
||||
}
|
||||
|
||||
// Send Message Function here to send the message to NATS
|
||||
// Send Message Function here to send the message to NATS
|
||||
export async function sendFileUpdateMessage(path: string, version: string): Promise<boolean> {
|
||||
const serviceName: string = "pis-data-ingress";
|
||||
const serviceId: string = hostname();
|
||||
const message: MQFileUpdate = {
|
||||
service_name: "pis-data-ingress",
|
||||
service_id: serviceId,
|
||||
sent_timestamp: Math.floor(Date.now() / 1000),
|
||||
data_type: "file",
|
||||
data_kind: "pis",
|
||||
payload: {
|
||||
version: version,
|
||||
filepath: path,
|
||||
}
|
||||
};
|
||||
|
||||
let nats: NatsConnection | undefined;
|
||||
|
||||
try {
|
||||
const nats: NatsConnection = await getNatsConnection();
|
||||
|
||||
const subject = `ingress.file.${message.data_kind}`;
|
||||
|
||||
nats.publish(subject, jc.encode(message));
|
||||
|
||||
await nats.drain();
|
||||
return true
|
||||
} catch (err) {
|
||||
log("ERROR", `NATS: Failed to send message: ${err}`);
|
||||
if (nats) {nats.close()}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user