tons of cursor-shit cleaning, finished implementing cnxId in observer

This commit is contained in:
STEINNI
2026-06-27 17:24:41 +00:00
parent 4c9e989bda
commit a1dba5060a
28 changed files with 213 additions and 224 deletions
+5 -2
View File
@@ -27,14 +27,17 @@ export function assembleHandlers(modules) {
})
}
export function createDispatchMessage({ eventHandlers, actionRules }) {
export function createDispatchMessage({ eventHandlers, eventRules, actionRules }) {
return(async function dispatchMessage(redisCnx, msg, chan) {
if(msg.action && msg.eventType) {
console.warn(`[${redisCnx.redisId}] Message has both action and eventType on ${chan}`)
return(false)
}
if(msg.action) return(dispatchActions(redisCnx, msg, chan, actionRules(redisCnx)))
if(msg.eventType) return(dispatchEvents(redisCnx, msg, chan, eventHandlers))
if(msg.eventType) {
const handlers = eventRules ? eventRules(redisCnx) : eventHandlers
return(dispatchEvents(redisCnx, msg, chan, handlers))
}
return(false)
})
}
+7 -2
View File
@@ -14,6 +14,7 @@ export async function dispatchActions(redisCnx, msg, chan, rules) {
const action = msg.action
const sender = msg.sender ?? null
const cnxId = msg.cnxId ?? null
const reqid = ('reqid' in msg) ? msg.reqid.substr(0, 50) : null
const roles = Array.isArray(msg.roles) ? msg.roles : ['*']
@@ -21,8 +22,9 @@ export async function dispatchActions(redisCnx, msg, chan, rules) {
if(!sender) return(true)
replyToAction(redisCnx, {
action,
reqid,
sender,
reqid,
cnxId,
success: false,
err: 'Missing or invalid action',
})
@@ -39,6 +41,7 @@ export async function dispatchActions(redisCnx, msg, chan, rules) {
action,
reqid,
sender,
cnxId,
success: false,
err: 'Unauthorized action !',
})
@@ -51,6 +54,7 @@ export async function dispatchActions(redisCnx, msg, chan, rules) {
action,
reqid,
sender,
cnxId,
success: false,
err: `Unknown action: ${action}`,
})
@@ -62,13 +66,14 @@ export async function dispatchActions(redisCnx, msg, chan, rules) {
}
try {
await handler.call(redisCnx, action, ('payload' in msg) ? msg.payload : null, reqid, sender, roles)
await handler.call(redisCnx, action, ('payload' in msg) ? msg.payload : null, reqid, sender, cnxId, roles)
} catch(err) {
console.error(`[${redisCnx.redisId}] Action ${action} failed:`, err)
replyToAction(redisCnx, {
action,
reqid,
sender,
cnxId,
success: false,
err: err.message ?? `${action} failed`,
})
+3 -1
View File
@@ -1,6 +1,8 @@
export function dispatchEvents(redisCnx, msg, chan, eventHandlers) {
const eventType = msg.eventType
const sender = msg.sender ?? null
const cnxId = msg.cnxId ?? null
if(!eventType || typeof(eventType) !== 'string') return(false)
let handled = false
@@ -13,7 +15,7 @@ export function dispatchEvents(redisCnx, msg, chan, eventHandlers) {
for(const handle of handlers) {
try {
handle.call(redisCnx, msg, chan)
handle.call(redisCnx, msg, chan, sender, cnxId)
} catch(err) {
console.error(
`[${redisCnx.redisId}] Event ${eventType} on ${chan} failed:`,
+6 -11
View File
@@ -3,12 +3,9 @@ export function busReplyRoute(daemonBlock, meshName) {
if(!daemonBlock?.senderId) return(null)
const onArena = meshName === 'arena'
const systemReply = daemonBlock.maestroActionsReply
?? daemonBlock.gpsActionsReply
?? daemonBlock.observerActionsReply
const actionsReply = onArena
? (daemonBlock.bus?.arena?.actionsReply ?? systemReply)
: systemReply
? (daemonBlock.bus?.arena?.actionsReply ?? daemonBlock.ActionsReply)
: daemonBlock.ActionsReply
if(!actionsReply) return(null)
@@ -25,12 +22,13 @@ export function publishActionReply(redisCnx, options) {
sender,
reply,
replyChannel,
senderId,
} = options
reply.action = action
reply.sender = senderId
reply.sender = redisCnx.senderId
reply.cnxId = redisCnx.cnxId
if(reqid) reply.reqid = reqid
const chan = replyChannel.replace(/\[UID\]/g, sender)
.replace(/\[CUID\]/g, redisCnx.cnxId)
redisCnx.redisPublish(chan, reply)
}
@@ -43,13 +41,11 @@ export function replyToAction(redisCnx, options) {
payload,
err,
replyChannel,
senderId,
} = options
const routeReplyChannel = replyChannel ?? redisCnx.actionsReply
const routeSenderId = senderId ?? redisCnx.senderId
if(!routeReplyChannel || !routeSenderId) {
if(!routeReplyChannel) {
console.error(`[${redisCnx.redisId}] Cannot resolve action reply route`)
return
}
@@ -63,7 +59,6 @@ export function replyToAction(redisCnx, options) {
reqid,
sender,
replyChannel: routeReplyChannel,
senderId: routeSenderId,
reply,
})
}