-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmonitoring.js
More file actions
40 lines (33 loc) · 941 Bytes
/
monitoring.js
File metadata and controls
40 lines (33 loc) · 941 Bytes
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
'use strict';
const ActorSystem = require('../system');
const http = require('node:http');
const URL = 'http://localhost:8000/';
const INTERVAL = 2000;
ActorSystem.register(class Monitoring {
constructor() {
console.log('Start actor: Monitoring');
this.prevSuccess = true;
this.timer = setInterval(() => {
this.attempt(URL);
}, INTERVAL);
}
attempt(url) {
http.get(url, (res) => {
const success = res.statusCode === 200;
this.notify({ url, success, status: res.statusCode });
}).on('error', (error) => {
this.notify({ url, success: false, status: error.message });
});
}
notify({ url, success, status }) {
if (this.prevSuccess !== success) {
this.prevSuccess = success;
ActorSystem.send('Renderer', { url, success, status });
}
}
async message() {}
async exit() {
clearInterval(this.timer);
console.log('Stop actor: Monitoring');
}
});