Improved updateDevicesInfo() efficiency
This commit is contained in:
parent
f1c37b8731
commit
b755d756d5
|
@ -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()
|
||||
})
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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'],
|
||||
|
|
Loading…
Reference in New Issue
Block a user