flexbiz-server
Version:
Flexible Server
13 lines (12 loc) • 5.88 kB
JavaScript
const redis=require("redis"),crypto=require("crypto");
async function eventHandler($flowState$$,$node$$,$initialPayload$$,$abortSignal$$,$engineContext$$){const {updateNodeStatus:$updateNodeStatus$$,triggerDownstreamNodesInternal:$triggerDownstreamNodesInternal$$}=$engineContext$$,$instanceId$$=$flowState$$.instanceId,$nodeId$$=$node$$.id,$nodeLabel$$=$node$$.data?.name||$nodeId$$||"Redis Event Listener";let {redisUrl:$redisUrl$$,channel:$channel$$,outputFormat:$outputFormat$$="json"}=$node$$.data;if(!$channel$$)throw Logger.error(`[${$instanceId$$}] ${$nodeLabel$$}: Missing 'channel' configuration.`),
$updateNodeStatus$$($instanceId$$,$nodeId$$,"failed","Missing 'channel' configuration.",void 0,"initial"),Error("Redis Event Listener: 'channel' is not configured.");$channel$$=`${$channel$$}-${$flowState$$.flowDefinition.id_app}`;Logger.info(`[${$instanceId$$}] ${$nodeLabel$$}: Initializing and subscribing to Redis channel '${$channel$$}' (using Redis v3.x API)...`);const $subscriber$$=$redisUrl$$?redis.createClient($redisUrl$$):redis.createClient();let $isConnected$$=!1,$isSubscribed$$=!1;$subscriber$$.on("error",
$err$$=>{Logger.error(`[${$instanceId$$}] ${$nodeLabel$$}: Redis subscriber error for channel '${$channel$$}':`,$err$$);$isSubscribed$$=$isConnected$$=!1;$abortSignal$$.aborted||$flowState$$.nodes[$nodeId$$]?.status==="failed"||$updateNodeStatus$$($instanceId$$,$nodeId$$,"failed",`Redis connection error: ${$err$$.message}`,void 0,"initial_error")});$subscriber$$.on("connect",()=>{Logger.info(`[${$instanceId$$}] ${$nodeLabel$$}: Successfully connected to Redis for channel '${$channel$$}'.`);$isConnected$$=
!0;$subscriber$$.subscribe($channel$$,($err$$,$count$$)=>{$err$$?(Logger.error(`[${$instanceId$$}] ${$nodeLabel$$}: Failed to subscribe to channel '${$channel$$}':`,$err$$),$updateNodeStatus$$($instanceId$$,$nodeId$$,"failed",`Redis subscribe error: ${$err$$.message}`,void 0,"subscribe_error"),$isSubscribed$$=!1):($isSubscribed$$=!0,Logger.info(`[${$instanceId$$}] ${$nodeLabel$$}: Subscribed to channel '${$channel$$}'. Waiting for messages... (Count: ${$count$$})`),$abortSignal$$.aborted||$flowState$$.nodes[$nodeId$$]?.status===
"active_listening"||$updateNodeStatus$$($instanceId$$,$nodeId$$,"active_listening",void 0,void 0,"initial_listen"))})});$subscriber$$.on("reconnecting",()=>{Logger.info(`[${$instanceId$$}] ${$nodeLabel$$}: Reconnecting to Redis for channel '${$channel$$}'...`);$isSubscribed$$=$isConnected$$=!1;$abortSignal$$.aborted||$flowState$$.nodes[$nodeId$$]?.status==="active_listening"||$updateNodeStatus$$($instanceId$$,$nodeId$$,"active_listening","Reconnecting...",void 0,"reconnecting")});$subscriber$$.on("message",
($shouldRun_subscribedChannel$$,$message$$)=>{if($abortSignal$$.aborted)Logger.info(`[${$instanceId$$}] ${$nodeLabel$$}: Message received on '${$shouldRun_subscribedChannel$$}' but flow is aborted. Ignoring.`);else if(!$isConnected$$||!$isSubscribed$$)Logger.warn(`[${$instanceId$$}] ${$nodeLabel$$}: Message received on '${$shouldRun_subscribedChannel$$}' but Redis connection/subscription is not active. Ignoring.`);else if($shouldRun_subscribedChannel$$===$channel$$){var $triggerId$$=`redis_${$shouldRun_subscribedChannel$$}_${Date.now()}_${crypto.randomBytes(2).toString("hex")}`,
$triggerContext$$=`|${$triggerId$$}`;Logger.info(`[${$instanceId$$}${$triggerContext$$}] ${$nodeLabel$$}: Message received from Redis channel '${$shouldRun_subscribedChannel$$}'.`);if($outputFormat$$==="json")try{var $eventPayload$$=JSON.parse($message$$)}catch($e$$){Logger.warn(`[${$instanceId$$}${$triggerContext$$}] ${$nodeLabel$$}: Failed to parse message as JSON. Using raw string. Error:`,$e$$.message),$eventPayload$$=$message$$}else $eventPayload$$=$message$$;if($node$$?.data?.handle_data_script)try{$eventPayload$$=
(new Function("data","initialPayload",$node$$.data.handle_data_script))($eventPayload$$,$initialPayload$$)}catch($error$$){Logger.error(`[${$instanceId$$}${$triggerContext$$}] ${$nodeLabel$$}: Error in handle_data_script:`,$error$$)}$shouldRun_subscribedChannel$$=!0;if($node$$?.data?.condition_script)try{$shouldRun_subscribedChannel$$=!!(new Function("eventPayload","initialPayload","nodeData",$node$$.data.condition_script))($eventPayload$$,$initialPayload$$,$node$$.data)}catch($error$$){Logger.error(`[${$instanceId$$}${$triggerContext$$}] ${$nodeLabel$$}: Error evaluating condition_script. Defaulting to false to prevent rogue execution. Error:`,
$error$$),$shouldRun_subscribedChannel$$=!1}$shouldRun_subscribedChannel$$?(Logger.info(`[${$instanceId$$}${$triggerContext$$}] ${$nodeLabel$$}: Condition met. Triggering downstream nodes.`),$updateNodeStatus$$($instanceId$$,$nodeId$$,"running",void 0,$eventPayload$$,$triggerId$$),$triggerDownstreamNodesInternal$$($instanceId$$,$nodeId$$,$eventPayload$$,$triggerId$$),$updateNodeStatus$$($instanceId$$,$nodeId$$,"completed",$eventPayload$$,void 0,$triggerId$$)):Logger.info(`[${$instanceId$$}${$triggerContext$$}] ${$nodeLabel$$}: Condition NOT met. Ignoring event.`);
$flowState$$.nodes[$nodeId$$]?.status==="active_listening"||$abortSignal$$.aborted||$updateNodeStatus$$($instanceId$$,$nodeId$$,"active_listening",void 0,void 0,null)}});$abortSignal$$.addEventListener("abort",()=>{Logger.info(`\u26a0\ufe0f [${$instanceId$$}] ${$nodeLabel$$}: Abort signal received. Unsubscribing and quitting Redis for channel '${$channel$$}'.`);try{$subscriber$$.connected&&$isSubscribed$$?$subscriber$$.unsubscribe($channel$$,$err$$=>{$err$$&&Logger.error(`[${$instanceId$$}] ${$nodeLabel$$}: Error during Redis unsubscribe:`,
$err$$);$subscriber$$.quit()}):$subscriber$$.connected&&$subscriber$$.quit()}catch($cleanupError$$){Logger.error(`[${$instanceId$$}] ${$nodeLabel$$}: Exception during Redis cleanup:`,$cleanupError$$)}},{once:!0});return Promise.resolve({repeating:!0,nodeType:"redis_listening"})}module.exports=eventHandler;