From 108b8d4114cdd6c0a6a4ec3f8ad85ffca9e06323 Mon Sep 17 00:00:00 2001 From: Claudio Torres Date: Fri, 7 Jul 2023 11:00:01 -0400 Subject: [PATCH 1/2] Triggers action wait for 8 seconds before doing the http request --- lib/agent/actions/triggers/index.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/agent/actions/triggers/index.js b/lib/agent/actions/triggers/index.js index 9b628243b..95c632bfd 100644 --- a/lib/agent/actions/triggers/index.js +++ b/lib/agent/actions/triggers/index.js @@ -748,23 +748,24 @@ function handle_triggers_succesfully(success, id, cb, triggers = null) { } function refresh_triggers(id, cb) { - logger.debug('retrieving triggers from API'); + logger.info('retrieving triggers from API'); emitter = emitter || new EventEmitter(); - - fetch_triggers((err, res) => { - if (err) { - handle_triggers_succesfully(false, id, cb); - } else { - var fetched_triggers = res.body; - - if (!(fetched_triggers instanceof Array)) { - return done(id, new Error('Triggers list is not an array'), cb); + setTimeout(() => { + fetch_triggers((err, res) => { + if (err) { + handle_triggers_succesfully(false, id, cb); + } else { + var fetched_triggers = res.body; + + if (!(fetched_triggers instanceof Array)) { + return done(id, new Error('Triggers list is not an array'), cb); + } + + handle_triggers_succesfully(true, id, cb, fetched_triggers); } - - handle_triggers_succesfully(true, id, cb, fetched_triggers); - } - }); + }); + }, 8000); } exports.clear_triggers = (cb) => { From 965ae53d686536908205315fc00920cf7b993619 Mon Sep 17 00:00:00 2001 From: Claudio Torres Date: Thu, 20 Jul 2023 10:33:31 -0400 Subject: [PATCH 2/2] clear timeout if refresh_triggers is executed again before settimeout is finished" --- lib/agent/actions/triggers/index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/agent/actions/triggers/index.js b/lib/agent/actions/triggers/index.js index 95c632bfd..a60ce2e0c 100644 --- a/lib/agent/actions/triggers/index.js +++ b/lib/agent/actions/triggers/index.js @@ -13,7 +13,7 @@ var running_triggers = []; var event_triggers = {}; var current_triggers = []; - +let timeoutStartTrigger; let websocket = require('../../plugins/control-panel/websockets'); const events_list = [ @@ -751,17 +751,16 @@ function refresh_triggers(id, cb) { logger.info('retrieving triggers from API'); emitter = emitter || new EventEmitter(); - setTimeout(() => { + if (timeoutStartTrigger) clearTimeout(timeoutStartTrigger); + timeoutStartTrigger = setTimeout(() => { fetch_triggers((err, res) => { if (err) { handle_triggers_succesfully(false, id, cb); } else { var fetched_triggers = res.body; - if (!(fetched_triggers instanceof Array)) { return done(id, new Error('Triggers list is not an array'), cb); } - handle_triggers_succesfully(true, id, cb, fetched_triggers); } });