Last active
August 4, 2024 16:28
-
-
Save tiagoandrepro/20ccdf79c2ddc5ca5bf1b358d4f81dcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /utils/redisQueue.js | |
const redis = require('redis'); | |
const { v4: uuidv4 } = require('uuid'); | |
const phpSerialize = require('php-serialize'); | |
const pushToQueue = async (webhook) => { | |
const redisClient = redis.createClient({ | |
host: process.env.REDIS_HOST || 'localhost', | |
port: process.env.REDIS_PORT || 6379, | |
password: process.env.REDIS_PASSWORD || '' | |
}); | |
try { | |
await redisClient.connect(); | |
const queueName = 'socialminder_ai_database_queues:webhooks'; | |
const notifyQueueName = 'socialminder_ai_database_queues:webhooks:notify'; | |
const jobClass = "App\\Jobs\\WebhookProcessJob"; | |
const webhookId = uuidv4(); | |
// Serializar o body do webhook usando php-serialize | |
const serializedBody = phpSerialize.serialize(webhook.body); | |
// Criar o comando PHP serializado no formato que o Laravel espera | |
const command = `O:26:"${jobClass}":2:{s:7:"webhook";s:${serializedBody.length}:"${serializedBody}";s:5:"queue";s:8:"webhooks";}`; | |
const jobPayload = { | |
uuid: webhookId, | |
displayName: jobClass, | |
job: "Illuminate\\Queue\\CallQueuedHandler@call", | |
maxTries: null, | |
maxExceptions: null, | |
failOnTimeout: false, | |
backoff: null, | |
timeout: null, | |
retryUntil: null, | |
data: { | |
commandName: jobClass, | |
command: command, // Comando serializado com os dados do webhook | |
}, | |
id: uuidv4(), | |
attempts: 0, | |
}; | |
// Enviar o payload do job para a fila 'webhooks' | |
const reply = await redisClient.lPush(queueName, JSON.stringify(jobPayload)); | |
console.log(`Data pushed to Redis queue "${queueName}". Queue length:`, reply); | |
// Adicionar o valor 1 à lista 'notify' | |
const notifyReply = await redisClient.lPush(notifyQueueName, '1'); | |
console.log(`Notify value added to Redis list "${notifyQueueName}". List length:`, notifyReply); | |
} catch (error) { | |
console.error('Error pushing data to Redis:', error); | |
} finally { | |
await redisClient.quit(); // Fechar a conexão com o Redis | |
} | |
}; | |
module.exports = pushToQueue; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment