UNPKG

dt-common-device

Version:

A secure and robust device management library for IoT applications

176 lines (175 loc) 6.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initializeWebhookQueue = initializeWebhookQueue; exports.addWebhookToQueue = addWebhookToQueue; exports.checkQueueStatus = checkQueueStatus; exports.getAllQueueStatuses = getAllQueueStatuses; exports.cleanupWebhookQueue = cleanupWebhookQueue; exports.completeWebhookWorkflow = completeWebhookWorkflow; exports.demonstrateErrorHandling = demonstrateErrorHandling; const WebhookQueueFactory_1 = require("../services/WebhookQueueFactory"); /** * Example usage of the Webhook Queue System */ // Configuration for the webhook queue const webhookQueueConfig = { redisHost: process.env.REDIS_HOST || "localhost", redisPort: parseInt(process.env.REDIS_PORT || "6379"), maxRetries: 3, backoffDelay: 2000, jobTimeout: 300000, // 5 minutes removeOnComplete: { age: 3600000, count: 100 }, // 1 hour, max 100 removeOnFail: { age: 7200000, count: 50 }, // 2 hours, max 50 }; // Example webhook processor function async function processWebhook(webhookData, pmsType) { console.log(`Processing webhook for PMS: ${pmsType}`); console.log("Webhook data:", webhookData); // Your webhook processing logic here // This could include: // - Updating database records // - Sending notifications // - Triggering other services // - etc. // Simulate some processing time await new Promise((resolve) => setTimeout(resolve, 1000)); console.log(`Webhook processed successfully for PMS: ${pmsType}`); return { success: true, processedAt: new Date().toISOString() }; } // Example: Initialize webhook queue async function initializeWebhookQueue() { try { // Create webhook queue service const webhookQueue = WebhookQueueFactory_1.WebhookQueueFactory.createWebhookQueue(webhookQueueConfig); // Set webhook processor for different PMS types await webhookQueue.setWebhookProcessor("property123", "CLOUDBEDS", processWebhook); await webhookQueue.setWebhookProcessor("property123", "HOTELKEY", processWebhook); await webhookQueue.setWebhookProcessor("property123", "STAYNTOUCH", processWebhook); console.log("Webhook queue initialized successfully"); return webhookQueue; } catch (error) { console.error("Failed to initialize webhook queue:", error); throw error; } } // Example: Add webhook to queue async function addWebhookToQueue(propertyId, pmsType, webhookData) { try { const webhookQueue = WebhookQueueFactory_1.WebhookQueueFactory.getWebhookQueue(); if (!webhookQueue) { throw new Error("Webhook queue not initialized"); } const jobId = await webhookQueue.addWebhookToQueue(propertyId, pmsType, webhookData); console.log(`Webhook added to queue with job ID: ${jobId}`); return jobId; } catch (error) { console.error("Failed to add webhook to queue:", error); throw error; } } // Example: Check queue status async function checkQueueStatus(propertyId, pmsType) { try { const webhookQueue = WebhookQueueFactory_1.WebhookQueueFactory.getWebhookQueue(); if (!webhookQueue) { throw new Error("Webhook queue not initialized"); } const status = await webhookQueue.getQueueStatus(propertyId, pmsType); console.log(`Queue status for ${propertyId}_${pmsType}_webhook:`, status); return status; } catch (error) { console.error("Failed to check queue status:", error); throw error; } } // Example: Get all queue statuses for a property async function getAllQueueStatuses(propertyId) { try { const webhookQueue = WebhookQueueFactory_1.WebhookQueueFactory.getWebhookQueue(); if (!webhookQueue) { throw new Error("Webhook queue not initialized"); } const statuses = await webhookQueue.getAllPropertyQueueStatuses(propertyId); console.log(`All queue statuses for property ${propertyId}:`, statuses); return statuses; } catch (error) { console.error("Failed to get queue statuses:", error); throw error; } } // Example: Cleanup and shutdown async function cleanupWebhookQueue() { try { await WebhookQueueFactory_1.WebhookQueueFactory.shutdownAll(); console.log("Webhook queue shutdown successfully"); } catch (error) { console.error("Failed to shutdown webhook queue:", error); throw error; } } // Example: Complete workflow async function completeWebhookWorkflow() { try { // 1. Initialize the queue const webhookQueue = await initializeWebhookQueue(); // 2. Add some webhooks const webhookData1 = { eventType: "reservation.created", reservationId: "res_001", guestName: "John Doe", checkIn: "2024-01-15", checkOut: "2024-01-17", }; const webhookData2 = { eventType: "reservation.updated", reservationId: "res_002", guestName: "Jane Smith", checkIn: "2024-01-16", checkOut: "2024-01-18", }; // Add webhooks to different queues await addWebhookToQueue("property123", "CLOUDBEDS", webhookData1); await addWebhookToQueue("property123", "HOTELKEY", webhookData2); // 3. Check queue statuses await checkQueueStatus("property123", "CLOUDBEDS"); await getAllQueueStatuses("property123"); // 4. Wait for processing (in real scenario, this would be handled by workers) console.log("Waiting for webhooks to be processed..."); await new Promise((resolve) => setTimeout(resolve, 5000)); // 5. Check final statuses await checkQueueStatus("property123", "CLOUDBEDS"); console.log("Webhook workflow completed successfully"); } catch (error) { console.error("Webhook workflow failed:", error); throw error; } } // Example: Error handling and retries async function demonstrateErrorHandling() { try { const webhookQueue = WebhookQueueFactory_1.WebhookQueueFactory.getWebhookQueue(); if (!webhookQueue) { throw new Error("Webhook queue not initialized"); } // Add webhook with custom retry options const jobId = await webhookQueue.addWebhookToQueue("property123", "CLOUDBEDS", { test: "error-handling" }, { attempts: 5, backoff: { type: "exponential", delay: 1000, }, }); console.log(`Webhook added with custom retry options, job ID: ${jobId}`); return jobId; } catch (error) { console.error("Failed to demonstrate error handling:", error); throw error; } }