Decouple updateDevicesInfo: delegate to cronTasks

This commit is contained in:
Douglas Barone 2020-12-02 15:54:49 -04:00
parent 5de1bb4f0b
commit 87f7ed4cd8
4 changed files with 12 additions and 16 deletions

View File

@ -1,20 +1,25 @@
import cron from 'node-cron' import cron from 'node-cron'
import { updateDBWithOnlineDevices } from './utils/wifiUtils' import { updateDevicesInfo } from './utils/wifiUtils'
import { User } from './classes/User' import { User } from './classes/User'
import { format } from 'date-fns' import { format } from 'date-fns'
import oui from 'oui' import oui from 'oui'
import { updateUserIdMappings } from './utils/paloalto'
function logMsg(msg) { function logMsg(msg) {
console.log(`[${format(new Date(), 'HH:mm:ss')}] cron: ${msg}`) console.log(`[${format(new Date(), 'HH:mm:ss')}] cron: ${msg}`)
} }
logMsg('Scheduling tasks') logMsg('Scheduling tasks...')
cron.schedule('*/1 * * * *', async () => { cron.schedule('*/1 * * * *', async () => {
logMsg('updateDBWithOnlineDevices started.') logMsg('updateDevicesInfo started.')
const devices = await updateDevicesInfo()
const mappings = await updateUserIdMappings()
logMsg( logMsg(
`updateDBWithOnlineDevices updated ${await updateDBWithOnlineDevices()} devices.` `updateDevicesInfo updated ${devices} devices and ${mappings} user-id mappings.`
) )
}) })

View File

@ -1,4 +1,3 @@
import { updateDBWithOnlineDevices } from '../utils/wifiUtils'
import { User } from '../classes/User' import { User } from '../classes/User'
import prisma from '../prisma' import prisma from '../prisma'
@ -94,8 +93,6 @@ const Query = {
if (identifiedOnly && nonIdentifiedOnly) if (identifiedOnly && nonIdentifiedOnly)
throw new Error('Invalid combination of filters') throw new Error('Invalid combination of filters')
updateDBWithOnlineDevices()
return prisma.wifiDevice.findMany({ return prisma.wifiDevice.findMany({
orderBy: [{ lastSeen: 'desc' }], orderBy: [{ lastSeen: 'desc' }],
where: identifiedOnly where: identifiedOnly
@ -117,8 +114,6 @@ const Query = {
async userPresence(_, { search }) { async userPresence(_, { search }) {
if (!search) search = '' if (!search) search = ''
updateDBWithOnlineDevices()
const usersWithWifiDevices = await prisma.user.findMany({ const usersWithWifiDevices = await prisma.user.findMany({
where: { where: {
wifiDevices: { some: { lastSeen: { not: null } } } wifiDevices: { some: { lastSeen: { not: null } } }

View File

@ -62,7 +62,7 @@ async function updateUserIdMappings() {
return wifiDevices.length return wifiDevices.length
} catch (e) { } catch (e) {
console.log('Error updating user-id mappings:', e.message) console.log('Error updating user-id mappings:', e.message)
return "Não foi possível atualizar. Veja o log do servidor" return 'Não foi possível atualizar. Veja o log do servidor'
} finally { } finally {
setTimeout(() => { setTimeout(() => {
working = false working = false

View File

@ -1,15 +1,13 @@
import { getOnlineWifiDevices as getOnlineUnifiDevices } from './unifiController' import { getOnlineWifiDevices as getOnlineUnifiDevices } from './unifiController'
import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController' import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController'
import { updateUserIdMappings } from './paloalto'
import prisma from '../prisma' import prisma from '../prisma'
const DEBOUNCE_TIME_MS = 10000 const DEBOUNCE_TIME_MS = 10000
let working = false let working = false
async function updateDBWithOnlineDevices() { async function updateDevicesInfo() {
if (working) return -1 // Debounce updates if (working) return -1 // Debounce updates
working = true working = true
@ -63,8 +61,6 @@ async function updateDBWithOnlineDevices() {
} }
} }
updateUserIdMappings()
setTimeout(() => { setTimeout(() => {
working = false working = false
}, DEBOUNCE_TIME_MS) }, DEBOUNCE_TIME_MS)
@ -77,4 +73,4 @@ async function updateDBWithOnlineDevices() {
} }
} }
export { updateDBWithOnlineDevices } export { updateDevicesInfo }