稳定测试版
This commit is contained in:
30
main/ipc/mq.js
Normal file
30
main/ipc/mq.js
Normal file
@@ -0,0 +1,30 @@
|
||||
// main/ipc/mq.js
|
||||
const { ipcMain } = require('electron')
|
||||
const mq = require('../../js/rabbitmq-service') // 复用你的文件
|
||||
let currentTenantId = null
|
||||
|
||||
async function startConsumer(emitMessage, tenantId) {
|
||||
await mq.startConsumer(
|
||||
`q.tenant.${tenantId}`,
|
||||
(msg) => emitMessage(msg.json ?? msg.text),
|
||||
{ prefetch: 1, requeueOnError: false, durable: true, assertQueue: true }
|
||||
)
|
||||
}
|
||||
|
||||
function registerMqIpc(emitMessage) {
|
||||
ipcMain.removeHandler('start-mq')
|
||||
ipcMain.handle('start-mq', async (_event, tenantId, userId) => {
|
||||
currentTenantId = tenantId
|
||||
await startConsumer(emitMessage, tenantId)
|
||||
return { ok: true }
|
||||
})
|
||||
|
||||
ipcMain.removeHandler('mq-send')
|
||||
ipcMain.handle('mq-send', async (_event, payload) => {
|
||||
if (!currentTenantId) return { ok: false, error: 'tenant not set' }
|
||||
await mq.publishToQueue(`q.tenant.${currentTenantId}`, payload)
|
||||
return { ok: true }
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { registerMqIpc }
|
||||
Reference in New Issue
Block a user