From b755d756d5881b82daa902b594eb0a55b04d3060 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Wed, 27 Oct 2021 11:40:27 -0400 Subject: [PATCH] Improved updateDevicesInfo() efficiency --- server/src/index.js | 6 +-- server/src/lib/wifiDevices.js | 73 +++++++++++++++++------------------ server/src/tasks.js | 5 +-- 3 files changed, 40 insertions(+), 44 deletions(-) diff --git a/server/src/index.js b/server/src/index.js index 7e15750..891dfb4 100755 --- a/server/src/index.js +++ b/server/src/index.js @@ -10,7 +10,7 @@ import { server } from './server' import { runTasks } from './tasks' import './cronTasks' -server.listen().then(options => { +server.listen().then(async options => { logSuccess({ tags: ['server'], message: `Servidor pronto!`, @@ -23,6 +23,6 @@ server.listen().then(options => { : 'Rodando em modo de desenvolvimento' } }) -}) -runTasks() + runTasks() +}) diff --git a/server/src/lib/wifiDevices.js b/server/src/lib/wifiDevices.js index 31517ec..1b5c27e 100644 --- a/server/src/lib/wifiDevices.js +++ b/server/src/lib/wifiDevices.js @@ -7,7 +7,6 @@ import prisma from '../prisma' import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub' import { logError, logSuccess } from './logger' -const DEBOUNCE_TIME_MS = 3000 const RECENT_THRESHOLD_IN_MINUTES = 5 let working = false @@ -69,8 +68,8 @@ function mockHostName({ mac, oui }) { } async function updateDB(onlineDevices) { - for (const device of onlineDevices) { - if (!device.user) await forceUserDisconnect(device.mac) + onlineDevices.map(device => { + if (!device.user) forceUserDisconnect(device.mac) else device.user = device.user.replace('IFMS\\', '') const user = device.user @@ -79,8 +78,8 @@ async function updateDB(onlineDevices) { const hostname = device.hostname || mockHostName(device) - try { - await prisma.wifiDevice.upsert({ + prisma.wifiDevice + .upsert({ where: { mac: device.mac }, create: { ...device, @@ -94,40 +93,40 @@ async function updateDB(onlineDevices) { user } }) - } catch (e) { - // If is a binding problem, probably the device has an user outside of AD, so save it anyway... - if (e.code == 'P2016') - try { - await forceUserDisconnect(device.mac) - await prisma.wifiDevice.upsert({ - where: { mac: device.mac }, - create: { - ...device, - hostname, - firstSeen: device.firstSeen || new Date(), - user: undefined - }, - update: { - ...device, - hostname, - user: undefined - } - }) - } catch (e) { + .catch(async e => { + // If is a binding problem, probably the device has an user outside of AD, so save it anyway... + if (e.code == 'P2016') + try { + await forceUserDisconnect(device.mac) + await prisma.wifiDevice.upsert({ + where: { mac: device.mac }, + create: { + ...device, + hostname, + firstSeen: device.firstSeen || new Date(), + user: undefined + }, + update: { + ...device, + hostname, + user: undefined + } + }) + } catch (e) { + logError({ + tags: ['wifiDevices'], + message: `Erro tentando adicionar o dispositivo "${device.mac}". Ele tinha um usuário fora do AD que foi ignorado, mas falhou mesmo assim`, + data: { error: e, device } + }) + } + else logError({ tags: ['wifiDevices'], - message: `Erro tentando adicionar o dispositivo "${device.mac}". Ele tinha um usuário fora do AD que foi ignorado, mas falhou mesmo assim`, + message: `Erro tentando adicionar o dispositivo "${device.mac}".`, data: { error: e, device } }) - } - else - logError({ - tags: ['wifiDevices'], - message: `Erro tentando adicionar o dispositivo "${device.mac}".`, - data: { error: e, device } - }) - } - } + }) + }) } async function updateDevicesInfo() { @@ -162,9 +161,7 @@ async function updateDevicesInfo() { return 0 } finally { - setTimeout(() => { - working = false - }, DEBOUNCE_TIME_MS) + working = false } } diff --git a/server/src/tasks.js b/server/src/tasks.js index 54a6fd2..c21f28d 100644 --- a/server/src/tasks.js +++ b/server/src/tasks.js @@ -13,9 +13,8 @@ async function updateDevicesTask() { try { const devicesQnt = await updateDevicesInfo() - if (devicesQnt > 0) { - updateUserIdMappings() - } + if (devicesQnt > 0) updateUserIdMappings() + else throw new Error('Já há uma tarefa em andamento.') } catch (e) { logError({ tags: ['task', 'wifiDevices', 'user-id'],