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