Update timeout added
This commit is contained in:
parent
7f4829438a
commit
f808ec7370
|
@ -7,8 +7,12 @@ import prisma from '../prisma'
|
|||
import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub'
|
||||
import { logError, logSuccess } from './logger'
|
||||
|
||||
import { performance } from 'perf_hooks'
|
||||
|
||||
const RECENT_THRESHOLD_IN_MINUTES = 5
|
||||
|
||||
const TIMEOUT_IN_MILLISECONDS = process.env.TASK_TIMEOUT || 120000
|
||||
|
||||
let working = false
|
||||
|
||||
async function getOnlineDevices() {
|
||||
|
@ -141,29 +145,48 @@ async function updateDB(onlineDevices) {
|
|||
})
|
||||
}
|
||||
|
||||
async function updateDevicesInfo() {
|
||||
if (working) return -1 // Debounce updates
|
||||
|
||||
function updateDevicesInfo() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (working) reject('A última atualização ainda não terminou')
|
||||
else {
|
||||
working = true
|
||||
|
||||
const updateTimeout = setTimeout(() => {
|
||||
reject('A função atingiu seu tempo limite.')
|
||||
}, TIMEOUT_IN_MILLISECONDS)
|
||||
|
||||
try {
|
||||
const startTime = performance.now()
|
||||
|
||||
const onlineDevices = await getOnlineDevices()
|
||||
|
||||
await updateDevicesStatus()
|
||||
|
||||
await updateDB(onlineDevices)
|
||||
|
||||
const endTime = performance.now()
|
||||
|
||||
onlineDevices.length > 0
|
||||
? resolve(
|
||||
`${onlineDevices.length} atualizados em ${Math.floor(
|
||||
endTime - startTime
|
||||
)}ms`
|
||||
)
|
||||
: reject('Não há dispositivos conectados no momento.')
|
||||
|
||||
pubsub.publish(USER_PRESENCE_UPDATED, {
|
||||
userPresenceUpdated: onlineDevices.length
|
||||
})
|
||||
|
||||
logSuccess({
|
||||
tags: ['wifiDevices'],
|
||||
message: `Foram atualizados ${onlineDevices.length} dispositivos Wi-Fi.`,
|
||||
message: `Foram atualizados ${
|
||||
onlineDevices.length
|
||||
} dispositivos Wi-Fi em ${((endTime - startTime) / 1000).toFixed(
|
||||
2
|
||||
)}s.`,
|
||||
data: onlineDevices
|
||||
})
|
||||
|
||||
return onlineDevices.length
|
||||
} catch (e) {
|
||||
logError({
|
||||
tags: ['wifiDevices'],
|
||||
|
@ -171,10 +194,13 @@ async function updateDevicesInfo() {
|
|||
data: e
|
||||
})
|
||||
|
||||
return 0
|
||||
reject('Não foi possível atualizar as informações dos dispositivos.')
|
||||
} finally {
|
||||
working = false
|
||||
clearTimeout(updateTimeout)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export { updateDevicesInfo }
|
||||
|
|
|
@ -11,14 +11,12 @@ async function updateDevicesTask() {
|
|||
})
|
||||
|
||||
try {
|
||||
const devicesQnt = await updateDevicesInfo()
|
||||
|
||||
if (devicesQnt > 0) updateUserIdMappings()
|
||||
else throw new Error('Já há uma tarefa em andamento.')
|
||||
await updateDevicesInfo()
|
||||
updateUserIdMappings()
|
||||
} catch (e) {
|
||||
logError({
|
||||
tags: ['task', 'wifiDevices', 'user-id'],
|
||||
message: `Erro executando tarefa.`,
|
||||
message: `Erro executando tarefa: ${e}`,
|
||||
data: e
|
||||
})
|
||||
} finally {
|
||||
|
|
Loading…
Reference in New Issue
Block a user