-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
272 additions
and
93 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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
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,37 @@ | ||
const {wad4human} = require("@decentral.ee/web3-helpers/src/math-utils"); | ||
|
||
const timeout = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
async function trigger (obj, ms) { | ||
await timeout(ms); | ||
await obj.sendReport(); | ||
} | ||
|
||
class NotificationJobs { | ||
constructor(app) { | ||
this.app = app; | ||
} | ||
|
||
async sendReport () { | ||
const healthcheck = await this.app.healthReport.fullReport(); | ||
if(!healthcheck.healthy) { | ||
const healthData = `Healthy: ${healthcheck.healthy}\nChainId: ${healthcheck.network.chainId}`; | ||
this.app.notifier.sendNotification(healthData); | ||
} | ||
} | ||
|
||
async start () { | ||
// run every hour ( value in ms) | ||
this.run(this, 3600*1000); | ||
} | ||
|
||
async run (self, time) { | ||
if (self.app._isShutdown) { | ||
self.app.logger.info(`app.shutdown() - closing NotificationJobs`); | ||
return; | ||
} | ||
await trigger(self, time); | ||
await this.run(self, time); | ||
} | ||
} | ||
|
||
module.exports = NotificationJobs; |
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,9 @@ | ||
const EventEmitter = require('events'); | ||
|
||
class Notifier extends EventEmitter { | ||
sendNotification(message) { | ||
this.emit('notification', `[${process.pid}]: ${message}`); | ||
} | ||
} | ||
|
||
module.exports = Notifier; |
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,38 @@ | ||
const { IncomingWebhook } = require("@slack/webhook"); | ||
|
||
/* | ||
Slack Notifier service | ||
This is not called directly, but is used by the Notifier service | ||
*/ | ||
|
||
class SlackNotifier { | ||
constructor(app, options) { | ||
if (!app.config.SLACK_WEBHOOK_URL) { | ||
throw new Error('Slack webhook url must be set in config'); | ||
} | ||
|
||
// notification service must be initialized | ||
if (!app.notifier) { | ||
throw new Error('Notifier must be initialized before SlackNotifier'); | ||
} | ||
|
||
this.app = app; | ||
this.webhook = new IncomingWebhook(app.config.SLACK_WEBHOOK_URL, options); | ||
this.app.notifier.on('notification', message => { | ||
this.sendNotification(message); | ||
}); | ||
} | ||
|
||
async sendNotification(message) { | ||
try { | ||
await this.webhook.send({ | ||
text: message, | ||
}); | ||
this.app.logger.info(`SlackNotifier: Sent notification to Slack: ${message}`); | ||
} catch (error) { | ||
this.app.logger.error(`SlackNotifier: Error sending notification to Slack: ${error}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports = SlackNotifier; |
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
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