Added cancellation by timeout

This commit is contained in:
Douglas Barone 2021-11-08 08:33:54 -04:00
parent ea0f9083c1
commit d7c4d4be15

View File

@ -1,8 +1,11 @@
import { create } from 'axios' import { create, CancelToken } from 'axios'
import https from 'https' import https from 'https'
import { ouiFinder } from '../utils/ouiFinder' import { ouiFinder } from '../utils/ouiFinder'
import { logError } from './logger' import { logError } from './logger'
const TIMEOUT_IN_MS = 120000
const REQUEST_TIMEOUT_IN_MS = 20000
const httpsAgent = new https.Agent({ const httpsAgent = new https.Agent({
rejectUnauthorized: false, rejectUnauthorized: false,
ciphers: 'AES256-SHA' // That's necessary to connect to a TLS 1.0 server. Run node with --tls-min-v1.0 ciphers: 'AES256-SHA' // That's necessary to connect to a TLS 1.0 server. Run node with --tls-min-v1.0
@ -10,55 +13,71 @@ const httpsAgent = new https.Agent({
const axios = create({ const axios = create({
httpsAgent: httpsAgent, httpsAgent: httpsAgent,
timeout: REQUEST_TIMEOUT_IN_MS,
auth: { auth: {
username: process.env.CISCO_USER, username: process.env.CISCO_USER,
password: process.env.CISCO_PASSWORD password: process.env.CISCO_PASSWORD
} }
}) })
const getUri = (skip, page, take) => const getUrl = (skip, page, take) =>
`https://${process.env.CISCO_HOST}/data/client-table.html?columns=524287&take=${take}&skip=${skip}&page=${page}&pageSize=50&sort[0][field]=ST&sort[0][dir]=desc` `https://${process.env.CISCO_HOST}/data/client-table.html?columns=524287&take=${take}&skip=${skip}&page=${page}&pageSize=50&sort[0][field]=ST&sort[0][dir]=desc`
async function getDevices() { function getDevices() {
let skip = 0 return new Promise(async (resolve, reject) => {
let page = 1 const source = CancelToken.source()
const take = 50
const responsesPromises = [] const timeout = setTimeout(() => {
source.cancel('timeout')
reject(new Error('A operação getDevices foi cancelada pelo tempo limite'))
}, TIMEOUT_IN_MS)
try { let skip = 0
let devices = [] let page = 1
let url = getUri(skip, page, take) const take = 50
const firstResponse = await axios.get(url)
const { total, data: page1devices } = firstResponse.data
devices = devices.concat(page1devices) const responsesPromises = []
do { try {
const firstResponse = await axios.get(getUrl(skip, page, take), {
cancelToken: source.token
})
const { total, data: page1devices } = firstResponse.data
let devices = page1devices
skip += take skip += take
page++
let uri = getUri(skip, page, take) while (total > skip) {
responsesPromises.push(
axios.get(getUrl(skip, page, take), {
cancelToken: source.token
})
)
responsesPromises.push(axios.get(uri)) skip += take
} while (total > skip) page++
}
const responses = await Promise.all(responsesPromises) const responses = await Promise.all(responsesPromises)
responses.forEach( clearTimeout(timeout)
({ data: { data: pageDevices } }) =>
(devices = devices.concat(pageDevices))
)
return devices responses.forEach(
} catch (e) { ({ data: { data: pageDevices } }) =>
logError({ (devices = devices.concat(pageDevices))
tags: ['cisco', 'wifiDevices'], )
message: e.message,
data: { ...e, config: { ...e.config, auth: '*****' } } resolve(devices)
}) } catch (e) {
return [] logError({
} tags: ['cisco', 'wifiDevices'],
message: e.message,
data: { ...e, config: { ...e.config, auth: '*****' } }
})
reject(e)
}
})
} }
export async function getOnlineWifiDevices() { export async function getOnlineWifiDevices() {