This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for miner offline/recovered mails
- Loading branch information
1 parent
0b90cce
commit 6400c9e
Showing
6 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters