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 './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()
})

View File

@ -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,7 +93,7 @@ async function updateDB(onlineDevices) {
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 (e.code == 'P2016')
try {
@ -126,8 +125,8 @@ async function updateDB(onlineDevices) {
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)
}
}

View File

@ -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'],