Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
feat: send connection status to Slack channel (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Feb 18, 2019
1 parent 807a692 commit 714d93d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 160 deletions.
2 changes: 1 addition & 1 deletion bin/boltzm
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const { argv } = require('yargs').options({
type: 'string',
},
'notification.interval': {
describe: 'Interval at which the balances of the backend should be checked in minutes',
describe: 'Interval at which the balances and connection status of the backend should be checked in minutes',
type: 'string',
},
'notification.token': {
Expand Down
72 changes: 57 additions & 15 deletions lib/notifications/NotificationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class NotificationProvider {
private slack: SlackClient;
private timer!: NodeJS.Timer;

// These Sets contains the symbols for which an alert notification was sent
// These Sets contain the symbols for which an alert notification was sent
private walletAlerts = new Set<string>();
private channelAlerts = new Set<string>();

private disconnected = false;
private disconnected = new Set<string>();

constructor(
private logger: Logger,
Expand All @@ -40,8 +40,8 @@ class NotificationProvider {

this.slack = new SlackClient(config.token, config.channel, config.name);

this.listenBoltz();
this.listenCommands();
this.listenToBoltz();
this.listenForCommands();
}

public init = async () => {
Expand All @@ -52,12 +52,17 @@ class NotificationProvider {
await this.slack.sendMessage('Started Boltz instance');
this.logger.verbose('Connected to Slack');

await this.checkBalances();
const check = async () => {
await this.checkConnections();
await this.checkBalances();
};

await check();

this.logger.silly(`Checking balances every ${this.config.interval} minutes`);
this.logger.debug(`Checking balances and connection status every ${this.config.interval} minutes`);

this.timer = setInterval(async () => {
await this.checkBalances();
await check();
}, minutesToMilliseconds(this.config.interval));
} catch (error) {
this.logger.warn(`Could not connect to Slack: ${error}`);
Expand All @@ -68,6 +73,33 @@ class NotificationProvider {
clearInterval(this.timer);
}

private checkConnections = async () => {
const info = await this.boltz.getInfo();

info.chainsMap.forEach(async ([symbol, chain]) => {
await this.checkConnection(`${symbol} node`, chain.chain);
await this.checkConnection(`${symbol} LND`, chain.lnd);
});
}

private checkConnection = async (service: string, object: { error: string } | undefined) => {
if (object) {
if (object.error === '') {
if (this.disconnected.has(service)) {
this.disconnected.delete(service);
await this.sendReconnected(service);
}

return;
}
}

if (!this.disconnected.has(service)) {
this.disconnected.add(service);
await this.sendLostConnection(service);
}
}

private checkBalances = async () => {
const balances = await this.parseBalances();

Expand Down Expand Up @@ -100,27 +132,29 @@ class NotificationProvider {
}
}

private listenBoltz = () => {
private listenToBoltz = () => {
const service = 'backend';

this.boltz.on('status.updated', async (status: ConnectionStatus) => {
switch (status) {
case ConnectionStatus.Connected:
if (this.disconnected) {
this.disconnected = false;
await this.slack.sendMessage('Connected to backend');
if (this.disconnected.has(service)) {
this.disconnected.delete(service);
await this.sendReconnected('backend');
}
break;

case ConnectionStatus.Disconnected:
if (!this.disconnected) {
this.disconnected = true;
await this.slack.sendMessage('*Lost connection to backend*');
if (!this.disconnected.has(service)) {
this.disconnected.add(service);
await this.sendLostConnection('backend');
}
break;
}
});
}

private listenCommands = () => {
private listenForCommands = () => {
this.slack.on('message', async (message: string) => {
switch (message.toLowerCase()) {
case 'getbalance':
Expand Down Expand Up @@ -181,6 +215,14 @@ class NotificationProvider {
await this.slack.sendMessage(message);
}

private sendLostConnection = async (service: string) => {
await this.slack.sendMessage(`*Lost connection to ${service}*`);
}

private sendReconnected = async (service: string) => {
await this.slack.sendMessage(`Reconnected to ${service}`);
}

private formatBalances = (expectedBalance: number, actualBalance: number) => {
return {
expected: satoshisToWholeCoins(expectedBalance),
Expand Down
8 changes: 4 additions & 4 deletions lib/notifications/SlackClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class SlackClient extends EventEmitter {
}

public sendMessage = async (message: string) => {
await this.client.chat.postMessage({
channel: this.channel,
text: `[${this.name}]: ${message}`,
});
await this.rtm.sendMessage(
`[${this.name}]: ${message}`,
this.channelId,
);
}

public listenToMessages = async () => {
Expand Down
36 changes: 16 additions & 20 deletions lib/proto/boltzrpc_pb.d.ts

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

Loading

0 comments on commit 714d93d

Please sign in to comment.