Skip to content
This repository has been archived by the owner on Jul 18, 2021. It is now read-only.

Commit

Permalink
Add support for miner offline/recovered mails
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbrucker committed May 10, 2019
1 parent 0b90cce commit 6400c9e
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 3 deletions.
35 changes: 35 additions & 0 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Proxy {

this.upstreams.forEach(upstream => upstream.recalculateTotalCapacity());
setInterval(this.updateMiners.bind(this), 60 * 1000);
setInterval(this.detectMinerOffline.bind(this), 30 * 1000);
}

getMiningInfo(maxScanTime) {
Expand Down Expand Up @@ -255,6 +256,40 @@ class Proxy {
});
}

detectMinerOffline() {
Object.keys(this.miners).forEach(minerId => {
const miner = this.miners[minerId];
const lastActiveDiffMin = moment().diff(miner.lastTimeActive, 'minutes');
if (lastActiveDiffMin < 5) {
if (miner.offlineSince) {
eventBus.publish('miner/online', minerId, miner.offlineSince);
miner.offlineSince = null;
}
return;
}
const lastBlockActive = miner.lastBlockActive;
const currentBlockHeights = this.upstreams.map(upstream => upstream.miningInfo.height);
const lastActiveWarn = currentBlockHeights.every(height => {
const diff = Math.abs(lastBlockActive - height);

return diff >= 2;
});
if (!lastActiveWarn) {
if (miner.offlineSince) {
eventBus.publish('miner/online', minerId, miner.offlineSince);
miner.offlineSince = null;
}
return;
}
if (miner.offlineSince) {
return;
}

miner.offlineSince = miner.lastTimeActive;
eventBus.publish('miner/offline', minerId, miner);
});
}

getProxyStats() {
const totalCapacity = this.getTotalCapacity();

Expand Down
109 changes: 109 additions & 0 deletions lib/services/mail-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const nodemailer = require('nodemailer');
const moment = require('moment');
const eventBus = require('./event-bus');
const store = require('./store');

class MailService {
async init() {
this.mailSettings = store.getMailSettings();
if (!this.mailSettings) {
return;
}

if (!this.validateMailSettings()) {
return;
}

this.transport = nodemailer.createTransport({
host: this.mailSettings.host,
port: this.mailSettings.port,
secure: this.mailSettings.secure,
auth: {
user: this.mailSettings.user,
pass: this.mailSettings.pass,
},
});

const successful = await this.verifyTransport();
if (!successful) {
this.transport = null;
return;
}

eventBus.subscribe('miner/online', this.onMinerOnline.bind(this));
eventBus.subscribe('miner/offline', this.onMinerOffline.bind(this));

eventBus.publish('log/info', 'Mail | Initialized');
}

async onMinerOnline(minerId, offlineSince) {
await this.sendMail({
from: this.mailSettings.mailFrom || this.mailSettings.user,
to: this.mailSettings.mailTo,
subject: `[BHD-Burst-Proxy] ${minerId} has recovered`,
text: `${minerId} has recovered after ${moment(offlineSince).fromNow(true)} of downtime`,
});
}

async onMinerOffline(minerId, miner) {
await this.sendMail({
from: this.mailSettings.mailFrom || this.mailSettings.user,
to: this.mailSettings.mailTo,
subject: `[BHD-Burst-Proxy] ${minerId} looks offline`,
text: `${minerId} seems to be offline.\nLast active: ${moment(miner.lastTimeActive).fromNow()}\nLast active block: ${miner.lastBlockActive}`,
});
}

validateMailSettings() {
if (!this.mailSettings.host) {
eventBus.publish('log/error', 'Mail | Validation error: host missing');
return false;
}
if (!this.mailSettings.port) {
eventBus.publish('log/error', 'Mail | Validation error: port missing');
return false;
}
if (this.mailSettings.secure === undefined) {
eventBus.publish('log/error', 'Mail | Validation error: useTLS missing');
return false;
}
if (!this.mailSettings.user) {
eventBus.publish('log/error', 'Mail | Validation error: user missing');
return false;
}
if (!this.mailSettings.pass) {
eventBus.publish('log/error', 'Mail | Validation error: pass missing');
return false;
}
if (!this.mailSettings.mailTo) {
eventBus.publish('log/error', 'Mail | Validation error: mailTo missing');
return false;
}

return true;
}

async verifyTransport(){
try {
await this.transport.verify();
} catch(err) {
eventBus.publish('log/error', `Mail | Connection Verification failed: ${err.message}`);
return false;
}

return true;
}

async sendMail(options) {
let result = null;
try {
result = await this.transport.sendMail(options);
} catch(err) {
eventBus.publish('log/error', `Mail | Sending mail failed: ${err.message}`);
}

return result;
}
}

module.exports = new MailService();
8 changes: 8 additions & 0 deletions lib/services/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class Store {
setProxies(proxies) {
this.proxies = proxies;
}

getMailSettings() {
return this.mailSettings;
}

setMailSettings(mailSettings) {
this.mailSettings = mailSettings;
}
}

module.exports = new Store();
3 changes: 3 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const latestVersionService = require('./lib/services/latest-version-service');
const logger = require('./lib/services/logger');
const Proxy = require('./lib/proxy');
const store = require('./lib/services/store');
const mailService = require('./lib/services/mail-service');
const version = require('./lib/version');
const {
HttpSinglePortTransport,
Expand Down Expand Up @@ -73,6 +74,8 @@ store.setLogDir(config.logDir);
if (config.logToFile) {
logger.enableFileLogging();
}
store.setMailSettings(config.config.mail);
mailService.init();

const proxyConfigs = config.proxies.map(proxyConfig => JSON.parse(JSON.stringify(proxyConfig)));

Expand Down
11 changes: 8 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"log-update": "^3.2.0",
"mkdirp": "^0.5.1",
"moment": "^2.24.0",
"nodemailer": "^6.1.1",
"pg": "^7.10.0",
"rotating-file-stream": "^1.4.1",
"semver": "^6.0.0",
Expand Down

0 comments on commit 6400c9e

Please sign in to comment.