Compare commits

...

1 Commits
main ... v3

Author SHA1 Message Date
FunctionsAPI
03e81435e2 Automatic push from FunctionsAPI 2025-01-28 11:58:02 +00:00
2 changed files with 79 additions and 2 deletions

View File

@ -1,2 +1 @@
# f9e3a96e7f774500bedaec5cf761dd60 # commonjs hello-world

78
index.js Normal file
View File

@ -0,0 +1,78 @@
const amqp = require('amqplib'); // For RabbitMQ
const { v4: uuidv4 } = require('uuid'); // Generate unique IDs for messages
const winston = require('winston'); // For logging
// Configure logging using winston
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ timestamp, level, message }) => `${timestamp} - ${level.toUpperCase()}: ${message}`)
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'app.log' })
]
});
// RabbitMQ Configuration
const RABBITMQ_URL = 'amqp://localhost';
const EXCHANGE_NAME = 'df.metadata';
const ROUTING_KEY = 'metadata-sink';
// Mock Resource Object
const resourceData = {
columns: [],
dataSetId: "01234567890123456789abcd",
description: "description",
isEnriched: false,
name: "name",
organizationId: "2dceb541-a1c1-4f38-96a4-9052dd69133e",
projectId: "01234567890123456789abcd",
resourceId: "a9aecd83-058e-4d6f-9fb9-f62a0e86246c",
resourceType: "asset",
tags: []
};
async function sendMessageToRabbitMQ() {
let connection;
try {
// Step 1: Connect to RabbitMQ
connection = await amqp.connect(RABBITMQ_URL);
const channel = await connection.createChannel();
// Step 2: Assert the exchange
await channel.assertExchange(EXCHANGE_NAME, 'topic', { durable: true });
// Step 3: Publish the message
const message = {
id: uuidv4(),
timestamp: new Date().toISOString(),
resource: resourceData
};
const messageBuffer = Buffer.from(JSON.stringify(message));
const result = channel.publish(EXCHANGE_NAME, ROUTING_KEY, messageBuffer);
// Logging results
if (result) {
logger.info('Message successfully published to RabbitMQ');
} else {
logger.warn('Message was not confirmed by RabbitMQ');
}
// Close the channel
await channel.close();
} catch (error) {
logger.error(`Error sending message to RabbitMQ: ${error.message}`);
} finally {
if (connection) {
await connection.close();
}
}
}
// Main Execution
sendMessageToRabbitMQ().catch((error) => {
logger.error(`Unexpected error occurred: ${error.message}`);
});