Skip to content

Commit

Permalink
watchdog
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
mrq1911 committed Jan 11, 2025
1 parent a580885 commit d0bb11a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
13 changes: 11 additions & 2 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import stableswap from "./handlers/stableswap.js";
import router from "./handlers/router.js";
import transfers from "./handlers/transfers.js";
import otc from "./handlers/otc.js";
import dca from "./handlers/dca.js";
import dca, {terminator} from "./handlers/dca.js";
import staking from "./handlers/staking.js";
import referrals from "./handlers/referrals.js";
import borrowing from "./handlers/borrowing.js";
import {initDiscord} from "./discord.js";
import {rpc, sha, token, channel} from "./config.js";
import {currenciesHandler} from "./currencies.js";
import {endpoints} from "./endpoints.js";
import process from "node:process";

async function main() {
console.log('🐍⌚');
Expand Down Expand Up @@ -73,9 +74,17 @@ async function main() {

main().catch(err => {
console.error(err);
process.exit(1);
exit(1);
});

[
'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach((signal, index) => process.on(signal, () => exit(128 + index + 1)));

function exit(code = 0) {
terminator();
setTimeout(() => process.exit(code), 500);
}


1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const grafanaUrl = process.env.GRAFANA;
export const grafanaDatasource = process.env.GRAFANA_DATASOURCE || 10;
export const port = process.env.PORT || 3000;
export const liquidationAlert = Number(process.env.LIQ_ALERT);
export const timeout = Number(process.env.TIMEOUT) || 120;
21 changes: 13 additions & 8 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {api} from './api.js';
import ethers from "ethers";
import {delay} from "./config.js";
import {delay, timeout} from "./config.js";

export class Events {
listeners = [];
Expand Down Expand Up @@ -42,18 +42,23 @@ export class Events {
}

startWatching() {
this.killWatcher = api().query.system.number(head => {
let watchdogTimer;

const resetWatchdog = () => {
clearTimeout(watchdogTimer);

watchdogTimer = setTimeout(() => {
throw new Error(`no block received for ${timeout} seconds`);
}, timeout * 1000);
};

api().query.system.number(head => {
resetWatchdog();
const block = head.toNumber() - delay;
this.emitFromBlock(block);
});
}

stopWatching() {
if (this.killWatcher) {
this.killWatcher();
}
}

async emit(events) {
const listeners = this.listeners;
const callbacks = events.flatMap(e => listeners
Expand Down
10 changes: 1 addition & 9 deletions src/handlers/dca.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,8 @@ function broadcastBuffer(scheduleId) {
}
}

function terminator() {
export function terminator() {
const ids = new Set(buffer.map(({id}) => id));
[...ids].map(broadcastBuffer);
}

['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(sig =>
process.on(sig, () => {
terminator();
setTimeout(() => process.exit(0), 500);
}));

0 comments on commit d0bb11a

Please sign in to comment.