diff --git a/lib/agent/plugins/control-panel/websockets/index.js b/lib/agent/plugins/control-panel/websockets/index.js index ac2182026..3daef908f 100644 --- a/lib/agent/plugins/control-panel/websockets/index.js +++ b/lib/agent/plugins/control-panel/websockets/index.js @@ -86,7 +86,43 @@ exports.heartbeat = () => { }, exports.pingtime); }; + +const updateTimestamp = () => { + last_time = Date.now(); +} + +const loadHooks = () => { + storage.do('query', { type: 'keys', column: 'id', data: 'last_connection' }, (err, stored) => { + if (err) logger.info("Error getting the last connection data"); + if (stored && stored.length > 0) { + last_stored = stored[0] + last_connection = last_stored; + + } else { + // Just the first time the client starts + last_connection = Math.round(Date.now()/1000); + storage.do('set', { type: 'keys', id: 'last_connection', data: { value: last_connection }}, (err) => { + if (err) logger.info("Error storing the last connection time") + logger.debug("Stored referential first connection time"); + }); + } + }); + + hooks.on('connected', () => { + fileretrieval.check_pending_files(); + triggers.start(); + }); + + setTimeout(() => { + server.create_server((err) => { + if (err) logger.debug(err.message) + setAliveTimeInterval = setInterval(updateTimestamp, startupTimeout); + }); + }, startupTimeout); +} + exports.startWebsocket = () => { + loadHooks(); const proxy = config.get('try_proxy'); let protocol = config.get('protocol'); const host = config.get('host'); @@ -268,48 +304,11 @@ exports.load = function (cb) { common = this; config = common.config; hooks = common.hooks; - loadHooks(); exports.startWebsocket(); if (emitter) return cb(null, emitter); emitter = new EventEmitter(); cb(null, emitter); }; - -const loadHooks = () => { - storage.do('query', { type: 'keys', column: 'id', data: 'last_connection' }, (err, stored) => { - if (err) logger.info("Error getting the last connection data"); - if (stored && stored.length > 0) { - last_stored = stored[0] - last_connection = last_stored; - - } else { - // Just the first time the client starts - last_connection = Math.round(Date.now()/1000); - storage.do('set', { type: 'keys', id: 'last_connection', data: { value: last_connection }}, (err) => { - if (err) logger.info("Error storing the last connection time") - logger.debug("Stored referential first connection time"); - }); - } - }); - - hooks.on('connected', () => { - fileretrieval.check_pending_files(); - triggers.start(); - }); - - setTimeout(() => { - server.create_server((err) => { - if (err) logger.debug(err.message) - setAliveTimeInterval = setInterval(updateTimestamp, startupTimeout); - }); - }, startupTimeout); -} - - -const updateTimestamp = () => { - last_time = Date.now(); -} - const clearIntervals = (aliveTimeReset = false) => { if(notifyActionInterval) clearInterval(notifyActionInterval); if(getStatusInterval) clearInterval(getStatusInterval);