Improved updateDevicesInfo() efficiency

This commit is contained in:
Douglas Barone 2021-10-27 11:40:27 -04:00
parent f1c37b8731
commit b755d756d5
3 changed files with 40 additions and 44 deletions

View File

@ -10,7 +10,7 @@ import { server } from './server'
import { runTasks } from './tasks' import { runTasks } from './tasks'
import './cronTasks' import './cronTasks'
server.listen().then(options => { server.listen().then(async options => {
logSuccess({ logSuccess({
tags: ['server'], tags: ['server'],
message: `Servidor pronto!`, message: `Servidor pronto!`,
@ -23,6 +23,6 @@ server.listen().then(options => {
: 'Rodando em modo de desenvolvimento' : 'Rodando em modo de desenvolvimento'
} }
}) })
})
runTasks() runTasks()
})

View File

@ -7,7 +7,6 @@ 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'
const DEBOUNCE_TIME_MS = 3000
const RECENT_THRESHOLD_IN_MINUTES = 5 const RECENT_THRESHOLD_IN_MINUTES = 5
let working = false let working = false
@ -69,8 +68,8 @@ function mockHostName({ mac, oui }) {
} }
async function updateDB(onlineDevices) { async function updateDB(onlineDevices) {
for (const device of onlineDevices) { onlineDevices.map(device => {
if (!device.user) await forceUserDisconnect(device.mac) if (!device.user) forceUserDisconnect(device.mac)
else device.user = device.user.replace('IFMS\\', '') else device.user = device.user.replace('IFMS\\', '')
const user = device.user const user = device.user
@ -79,8 +78,8 @@ async function updateDB(onlineDevices) {
const hostname = device.hostname || mockHostName(device) const hostname = device.hostname || mockHostName(device)
try { prisma.wifiDevice
await prisma.wifiDevice.upsert({ .upsert({
where: { mac: device.mac }, where: { mac: device.mac },
create: { create: {
...device, ...device,
@ -94,40 +93,40 @@ async function updateDB(onlineDevices) {
user user
} }
}) })
} catch (e) { .catch(async e => {
// If is a binding problem, probably the device has an user outside of AD, so save it anyway... // If is a binding problem, probably the device has an user outside of AD, so save it anyway...
if (e.code == 'P2016') if (e.code == 'P2016')
try { try {
await forceUserDisconnect(device.mac) await forceUserDisconnect(device.mac)
await prisma.wifiDevice.upsert({ await prisma.wifiDevice.upsert({
where: { mac: device.mac }, where: { mac: device.mac },
create: { create: {
...device, ...device,
hostname, hostname,
firstSeen: device.firstSeen || new Date(), firstSeen: device.firstSeen || new Date(),
user: undefined user: undefined
}, },
update: { update: {
...device, ...device,
hostname, hostname,
user: undefined user: undefined
} }
}) })
} catch (e) { } 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({ logError({
tags: ['wifiDevices'], 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 } data: { error: e, device }
}) })
} })
else })
logError({
tags: ['wifiDevices'],
message: `Erro tentando adicionar o dispositivo "${device.mac}".`,
data: { error: e, device }
})
}
}
} }
async function updateDevicesInfo() { async function updateDevicesInfo() {
@ -162,9 +161,7 @@ async function updateDevicesInfo() {
return 0 return 0
} finally { } finally {
setTimeout(() => { working = false
working = false
}, DEBOUNCE_TIME_MS)
} }
} }

View File

@ -13,9 +13,8 @@ async function updateDevicesTask() {
try { try {
const devicesQnt = await updateDevicesInfo() const devicesQnt = await updateDevicesInfo()
if (devicesQnt > 0) { if (devicesQnt > 0) updateUserIdMappings()
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'],