Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const TelegramBot = require('node-telegram-bot-api');
- const mysql = require("mysql")
- const token = 'token';
- const bot = new TelegramBot(token, {polling: true});
- const username = `@username`
- function connectToDb(callback) {
- const connection = mysql.createConnection({
- host : 'localhost',
- user : 'root',
- password : 'root',
- // database : 'cloudprint'
- });
- connection.connect((err) => {
- callback(err, connection)
- })
- }
- function beginPeriodickCheck() {
- connectToDb((err, connection) => {
- if(err) {
- //fail the app. We assume that db is on good condition when the app is started
- } else {
- //connect to db is ok, begin periodic ping to db
- const pingToDb = () => {
- connection.ping((err) => {
- if(err) {
- bot.sendMessage(username, `Db is failing! Error ${err.message}`)
- }
- setTimeout(pingToDb, 1000 * 60 * 30)
- })
- }
- pingToDb()
- }
- })
- }
- //main function
- function main() {
- beginPeriodickCheck()
- }
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement