From f808ec7370140637cc9f849e8dc2e2863249550b Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Wed, 3 Nov 2021 11:37:58 -0400 Subject: [PATCH] Update timeout added --- server/src/lib/wifiDevices.js | 78 +++++++++++++++++++++++------------ server/src/tasks.js | 8 ++-- 2 files changed, 55 insertions(+), 31 deletions(-) diff --git a/server/src/lib/wifiDevices.js b/server/src/lib/wifiDevices.js index 0275589..2618689 100644 --- a/server/src/lib/wifiDevices.js +++ b/server/src/lib/wifiDevices.js @@ -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,40 +145,62 @@ 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 - working = true + const updateTimeout = setTimeout(() => { + reject('A função atingiu seu tempo limite.') + }, TIMEOUT_IN_MILLISECONDS) - try { - const onlineDevices = await getOnlineDevices() + try { + const startTime = performance.now() - await updateDevicesStatus() + const onlineDevices = await getOnlineDevices() - await updateDB(onlineDevices) + await updateDevicesStatus() - pubsub.publish(USER_PRESENCE_UPDATED, { - userPresenceUpdated: onlineDevices.length - }) + await updateDB(onlineDevices) - logSuccess({ - tags: ['wifiDevices'], - message: `Foram atualizados ${onlineDevices.length} dispositivos Wi-Fi.`, - data: onlineDevices - }) + const endTime = performance.now() - return onlineDevices.length - } catch (e) { - logError({ - tags: ['wifiDevices'], - message: `Erro atualizando dispositivos Wi-Fi: ${e.message}`, - data: e - }) + onlineDevices.length > 0 + ? resolve( + `${onlineDevices.length} atualizados em ${Math.floor( + endTime - startTime + )}ms` + ) + : reject('Não há dispositivos conectados no momento.') - return 0 - } finally { - working = false - } + pubsub.publish(USER_PRESENCE_UPDATED, { + userPresenceUpdated: onlineDevices.length + }) + + logSuccess({ + tags: ['wifiDevices'], + message: `Foram atualizados ${ + onlineDevices.length + } dispositivos Wi-Fi em ${((endTime - startTime) / 1000).toFixed( + 2 + )}s.`, + data: onlineDevices + }) + } catch (e) { + logError({ + tags: ['wifiDevices'], + message: `Erro atualizando dispositivos Wi-Fi: ${e.message}`, + data: e + }) + + reject('Não foi possível atualizar as informações dos dispositivos.') + } finally { + working = false + clearTimeout(updateTimeout) + } + } + }) } export { updateDevicesInfo } diff --git a/server/src/tasks.js b/server/src/tasks.js index c21f28d..645d62a 100644 --- a/server/src/tasks.js +++ b/server/src/tasks.js @@ -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 {