forked from turntayble81/natelogg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseMonitors.js
54 lines (44 loc) · 1.68 KB
/
baseMonitors.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const Tail = require('tail').Tail;
const path = require('path');
const fs = require('fs');
const Monitor = require('./monitor');
class BaseMonitors extends Monitor {
constructor(config, socket) {
super({
resetCount: 0,
emitName: 'baseMonitor',
resetCountThreshold: 20
});
this.resetCount = 0;
this.watchers = fs.readdirSync(config.logDirectory)
.map((log) => {
let tail = new Tail(path.join(config.logDirectory, log), {
fromBeginning: false,
follow: true
});
let context = {
tail: tail,
log: log,
isCrashed: false
};
let lineHandler = (context, data) => {
if (this.isCrashed(data)) {
context.crashed = true;
this.resetCount = 0;
socket.emit(this.emitName, {crash: context.crashed, log: context.log});
} else if (context.crashed && !this.isCrashed(data) && this.isReset()) {
context.crashed = false;
socket.emit(this.emitName, {crash: context.crashed, log: context.log});
} else {
this.resetCount++;
}
};
tail.on('line', lineHandler.bind(this, context));
return context;
});
}
isCrashed(data) {
return data.toLowerCase().indexOf('cannot find module') >= 0;
}
}
module.exports = BaseMonitors;