From cb1d7b95c8333ed4a5eada4de1fe68932294e154 Mon Sep 17 00:00:00 2001 From: singharaj usai Date: Sun, 13 Oct 2024 13:09:16 -0400 Subject: [PATCH] merge commit --- server/worker.js | 74 +++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/server/worker.js b/server/worker.js index 072e347..a239468 100644 --- a/server/worker.js +++ b/server/worker.js @@ -9,50 +9,48 @@ const MONGODB_URI = process.env.MONGODB_URI; const rccService = new RCCService('http://128.254.193.148:8484'); async function consumeQueue() { - const connection = await amqp.connect('amqp://valk:smoothcriminal@rabbitmq'); - const channel = await connection.createChannel(); - await channel.assertQueue('thumbnail_render_queue', { durable: true }); - - try { - console.log('Attempting to connect to MongoDB...'); - await connectDB(MONGODB_URI); - console.log('Connected to MongoDB'); - } catch (error) { - console.error('Error connecting to database:', error); - throw error; - } - - console.log('Waiting for messages in the thumbnail render queue...'); - - channel.consume( - 'thumbnail_render_queue', - async (message) => { - const content = JSON.parse(message.content.toString()); - const assetId = content.assetId; - const assetType = content.assetType; - - console.log(`Received assetId: ${assetId}. Rendering thumbnail...`); - - try { - const renderedThumbnailUrl = await rccService.renderAssetThumbnail( - assetId, - assetType - ); - - // Update the asset in the MongoDB collection with the new thumbnail URL - await Asset.updateOne({ assetId: assetId }, { ThumbnailLocation: renderedThumbnailUrl }); + const connection = await amqp.connect('amqp://valk:smoothcriminal@rabbitmq'); + const channel = await connection.createChannel(); + await channel.assertQueue('thumbnail_render_queue', { durable: true }); + + try { + console.log('Attempting to connect to MongoDB...'); + await connectDB(MONGODB_URI); + console.log('Connected to MongoDB'); + } catch (error) { + console.error('Error connecting to database:', error); + throw error; + } + + console.log('Waiting for messages in the thumbnail render queue...'); + + channel.consume('thumbnail_render_queue', async (message) => { + const content = JSON.parse(message.content.toString()); + const assetId = content.assetId; + const assetType = content.assetType; + + console.log(`Received assetId: ${assetId}. Rendering thumbnail...`); + + try { + const renderedThumbnailUrl = await rccService.renderAssetThumbnail(assetId, assetType); + + // Update the asset in the MongoDB collection with the new thumbnail URL (improve later) + if(assetType == 'Place') { + await Asset.updateOne({ assetId: assetId }, { ThumbnailLocation: renderedThumbnailUrl }); + await Game.updateOne({ assetId: assetId }, { thumbnailUrl: renderedThumbnailUrl }); + } else { + await Asset.updateOne({ assetId: assetId }, { ThumbnailLocation: renderedThumbnailUrl }); + } console.log(`Asset ${assetId} thumbnail updated with ${renderedThumbnailUrl}`); } catch (error) { console.error(`Failed to process asset ${assetId}:`, error); } - // Acknowledge the message after processing - channel.ack(message); - }, - { noAck: false } - ); + // Acknowledge the message after processing + channel.ack(message); + }, { noAck: false }); } // Start consuming messages from the queue -consumeQueue().catch(console.error); +consumeQueue().catch(console.error); \ No newline at end of file