Advertisement
Guest User

Untitled

a guest
Mar 11th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. const TelegramBot = require('node-telegram-bot-api');
  2. const mysql = require("mysql")
  3. const token = 'token';
  4. const bot = new TelegramBot(token, {polling: true});
  5. const username = `@username`
  6. function connectToDb(callback) {
  7. const connection = mysql.createConnection({
  8. host : 'localhost',
  9. user : 'root',
  10. password : 'root',
  11. // database : 'cloudprint'
  12. });
  13. connection.connect((err) => {
  14. callback(err, connection)
  15. })
  16. }
  17.  
  18. function beginPeriodickCheck() {
  19. connectToDb((err, connection) => {
  20. if(err) {
  21. //fail the app. We assume that db is on good condition when the app is started
  22.  
  23. } else {
  24. //connect to db is ok, begin periodic ping to db
  25. const pingToDb = () => {
  26. connection.ping((err) => {
  27. if(err) {
  28. bot.sendMessage(username, `Db is failing! Error ${err.message}`)
  29. }
  30. setTimeout(pingToDb, 1000 * 60 * 30)
  31. })
  32. }
  33. pingToDb()
  34. }
  35. })
  36. }
  37.  
  38. //main function
  39. function main() {
  40. beginPeriodickCheck()
  41. }
  42. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement