Skip to content

Commit

Permalink
Merge pull request #171 from harmony-one/monitoring
Browse files Browse the repository at this point in the history
Added monitoring
  • Loading branch information
ahiipsa authored Aug 24, 2023
2 parents 393354d + 7d7bb21 commit fb17d6a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {AppDataSource} from "./database/datasource";
import { text } from "stream/consumers";
import { autoRetry } from "@grammyjs/auto-retry";
import {run} from "@grammyjs/runner";
import {runBotHeartBit} from "./monitoring/monitoring";


const logger = pino({
Expand Down Expand Up @@ -447,3 +448,8 @@ process.once("SIGTERM", stopRunner);

AppDataSource.initialize();

if (config.betteruptime.botHeartBitId) {
const task = runBotHeartBit(runner, config.betteruptime.botHeartBitId);
process.once("SIGINT", () => task.stop());
process.once("SIGTERM", () => task.stop());
}
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,7 @@ export default {
.map((item) => item.toString().toLowerCase()),
creditsAmount: "100",
},
betteruptime: {
botHeartBitId: process.env.BOT_HEARTBIT_ID || ''
}
};
35 changes: 35 additions & 0 deletions src/monitoring/monitoring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import cron from 'node-cron'
import axios from "axios";
import {RunnerHandle} from "@grammyjs/runner";
import {pino} from "pino";

const logger = pino({
name: "monitoring",
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});

export const runBotHeartBit = (runner: RunnerHandle, heartBitId: string) => {

const action = () => {
logger.info('heartbit');
if (!runner.isRunning()) {
logger.error('bot runner is stopped');
return;
}

axios
.post(
`https://uptime.betterstack.com/api/v1/heartbeat/${heartBitId}`
).catch((err) => {
logger.warn(`betteruptime error ${err}`);
})

}

return cron.schedule('*/1 * * * *', action);
}

0 comments on commit fb17d6a

Please sign in to comment.