Skip to content

Commit

Permalink
Merge pull request #635 from prey/Fix-loadhooks-each-connection
Browse files Browse the repository at this point in the history
load hooks after each connection
  • Loading branch information
SoraKenji authored Jun 9, 2022
2 parents 96123b6 + abcf3b7 commit 2b6e32a
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions lib/agent/plugins/control-panel/websockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2b6e32a

Please sign in to comment.