forked from ptarmiganlabs/butler-cw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheartbeat.js
40 lines (34 loc) · 1.13 KB
/
heartbeat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const later = require('@breejs/later');
const axios = require('axios');
const globals = require('./globals');
const callRemoteURL = (remoteURL) => {
axios
.get(remoteURL)
// eslint-disable-next-line no-unused-vars
.then((response) => {
// handle success
globals.logger.debug(`HEARTBEAT: Sent heartbeat to ${remoteURL}`);
})
.catch((error) => {
// handle error
globals.logger.error(`HEARTBEAT: Error sending heartbeat: ${error}`);
});
};
function setupHeartbeatTimer(config) {
try {
globals.logger.debug(
`HEARTBEAT: Setting up heartbeat to remote: ${config.get('heartbeat.remoteURL')}`
);
const sched = later.parse.text(config.get('heartbeat.frequency'));
later.setInterval(() => {
callRemoteURL(config.get('heartbeat.remoteURL'));
}, sched);
// Do an initial ping to the remote URL
callRemoteURL(config.get('heartbeat.remoteURL'));
} catch (err) {
globals.logger.error(`HEARTBEAT: Error ${err}`);
}
}
module.exports = {
setupHeartbeatTimer,
};