Adição de funcionalidade para checar internet nas sessoes remotas

pull/21/head
adriano 2023-08-16 08:39:48 -03:00
parent a1cd7e3372
commit bfad9fc0b2
3 changed files with 88 additions and 5 deletions

View File

@ -1,5 +1,5 @@
# NUMBER AND NAME THAT WILL BE DISPLAYED ON CONSOLE # NUMBER AND NAME THAT WILL BE DISPLAYED ON CONSOLE
MOBILEUID=5521995996701 MOBILEUID=5517988310949
MOBILENAME=test - S1 MOBILENAME=test - S1
# PORT NUMBER FOR THIS API # PORT NUMBER FOR THIS API
@ -16,7 +16,7 @@ DB_PASS=strongpassword
DB_PORT=3306 DB_PORT=3306
# WHATSAPP ID OF THE TABLE Whatsapps FROM THE OMNIHIT DATABASE # WHATSAPP ID OF THE TABLE Whatsapps FROM THE OMNIHIT DATABASE
WHATSAPP_ID=229 WHATSAPP_ID=223
# MONGO CONNECTION # MONGO CONNECTION
DB_MONGO_URL=mongodb://localhost:27017 DB_MONGO_URL=mongodb://localhost:27017

View File

@ -40,6 +40,8 @@ const qrcode = require('qrcode-terminal')
const omnihit = require('./funcs/omnihit.js') const omnihit = require('./funcs/omnihit.js')
const { allowedNodeEnvironmentFlags } = require('process') const { allowedNodeEnvironmentFlags } = require('process')
const checkInternetConnection = require('./helpers/checkIntenet')
const mongo = require('./funcs/mongoConn') const mongo = require('./funcs/mongoConn')
const db = mongo.db(process.env.DB_MONGO_NAME) const db = mongo.db(process.env.DB_MONGO_NAME)
@ -53,6 +55,7 @@ let scheduler_messages_outbound
let scheduler_monitor let scheduler_monitor
let scheduler_monitor_cell let scheduler_monitor_cell
let scheduler_campaign_monitor let scheduler_campaign_monitor
let scheduler_internet_conn
let client let client
// const PORT = 80; // const PORT = 80;
@ -64,6 +67,7 @@ const { imageUpload } = require('./helpers/image-uploader')
var QRCODE = "0" var QRCODE = "0"
var mobileuid var mobileuid
var destroy var destroy
var changeInternetConn = true
let asking_qrcode = false let asking_qrcode = false
@ -991,7 +995,7 @@ async function monitor() {
_nextime = 10000 _nextime = 10000
} }
console.log(`WHATSAPP_ID: ${process.env.WHATSAPP_ID} | CLIENT MOBILEUID: ${mobileuid} | NAME: ${process.env.MOBILENAME} | ENV MOBILEUID: ${process.env.MOBILEUID} | STATUS: ${stat}`) console.log(`WHATSAPP_ID: ${process.env.WHATSAPP_ID} | CLIENT MOBILEUID: ${mobileuid} | NAME: ${process.env.MOBILENAME} | ENV MOBILEUID: ${process.env.MOBILEUID} | STATUS: ${stat} | INTERNET: ${changeInternetConn}`)
if (stat && stat === 'CONNECTED') { if (stat && stat === 'CONNECTED') {
@ -1019,7 +1023,24 @@ const sendCampaignMessage = async () => {
try { try {
clearInterval(scheduler_campaign_monitor) clearInterval(scheduler_campaign_monitor)
if (mobileuid != undefined) {
let internetConnection = await checkInternetConnection()
if (!internetConnection) {
changeInternetConn = false
} else {
changeInternetConn = true
}
// let stat
// try {
// stat = await client.getState()
// } catch (err) {
// stat = (err?.message?.search('Session closed') > -1 ? 'SESSIONCLOSED' : 'UNKNOWN')
// }
// console.log('===========> stat: ', stat)
if (mobileuid != undefined && internetConnection) {
auxCampaignMessage = '' auxCampaignMessage = ''
@ -1137,6 +1158,25 @@ async function monitorCell() {
} }
async function internetMonitor() {
try {
clearInterval(scheduler_internet_conn)
if (!changeInternetConn) {
console.log('INTERNET IS OFFLINE. THE PM2 WILL RESTORE THE PROCESS')
process.exit()
}
} catch (error) {
console.log(`Error on try kill the process from internetMonitor function`)
} finally {
scheduler_internet_conn = setInterval(internetMonitor, 60000)
}
}
function comercialBuss(until_hour) { function comercialBuss(until_hour) {
const _hour = new Date().getHours() const _hour = new Date().getHours()
@ -1155,6 +1195,8 @@ scheduler_monitor = setInterval(monitor, 10000)
scheduler_campaign_monitor = setInterval(sendCampaignMessage, 3000) scheduler_campaign_monitor = setInterval(sendCampaignMessage, 3000)
scheduler_internet_conn = setInterval(internetMonitor, 60000)
app.listen(process.env.PORT || 8003, function () { app.listen(process.env.PORT || 8003, function () {
console.log("\u26A1[server]: Server is running at Port ::: " + process.env.PORT || 8003) console.log("\u26A1[server]: Server is running at Port ::: " + process.env.PORT || 8003)
}) })

View File

@ -0,0 +1,41 @@
const http = require('http')
const checkInternetConnection = async () => {
const options = {
hostname: 'www.google.com',
port: 80,
method: 'HEAD'
}
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
if (res.statusCode === 200) {
resolve(true)
} else {
resolve(false)
}
req.abort()
})
req.on('error', (err) => {
resolve(false)
})
req.end()
})
};
// (async () => {
// try {
// const isConnected = await checkInternetConnection()
// if (isConnected) {
// console.log('Internet connection is available.')
// } else {
// console.log('Internet connection is not available.')
// }
// } catch (error) {
// console.error('Error checking internet connection:', error)
// }
// })()
module.exports = checkInternetConnection