Decouple updateDevicesInfo: delegate to cronTasks
This commit is contained in:
parent
5de1bb4f0b
commit
87f7ed4cd8
|
@ -1,20 +1,25 @@
|
|||
import cron from 'node-cron'
|
||||
import { updateDBWithOnlineDevices } from './utils/wifiUtils'
|
||||
import { updateDevicesInfo } from './utils/wifiUtils'
|
||||
import { User } from './classes/User'
|
||||
import { format } from 'date-fns'
|
||||
import oui from 'oui'
|
||||
|
||||
import { updateUserIdMappings } from './utils/paloalto'
|
||||
|
||||
function logMsg(msg) {
|
||||
console.log(`[${format(new Date(), 'HH:mm:ss')}] cron: ${msg}`)
|
||||
}
|
||||
|
||||
logMsg('Scheduling tasks')
|
||||
logMsg('Scheduling tasks...')
|
||||
|
||||
cron.schedule('*/1 * * * *', async () => {
|
||||
logMsg('updateDBWithOnlineDevices started.')
|
||||
logMsg('updateDevicesInfo started.')
|
||||
|
||||
const devices = await updateDevicesInfo()
|
||||
const mappings = await updateUserIdMappings()
|
||||
|
||||
logMsg(
|
||||
`updateDBWithOnlineDevices updated ${await updateDBWithOnlineDevices()} devices.`
|
||||
`updateDevicesInfo updated ${devices} devices and ${mappings} user-id mappings.`
|
||||
)
|
||||
})
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import { updateDBWithOnlineDevices } from '../utils/wifiUtils'
|
||||
import { User } from '../classes/User'
|
||||
|
||||
import prisma from '../prisma'
|
||||
|
@ -94,8 +93,6 @@ const Query = {
|
|||
if (identifiedOnly && nonIdentifiedOnly)
|
||||
throw new Error('Invalid combination of filters')
|
||||
|
||||
updateDBWithOnlineDevices()
|
||||
|
||||
return prisma.wifiDevice.findMany({
|
||||
orderBy: [{ lastSeen: 'desc' }],
|
||||
where: identifiedOnly
|
||||
|
@ -117,8 +114,6 @@ const Query = {
|
|||
async userPresence(_, { search }) {
|
||||
if (!search) search = ''
|
||||
|
||||
updateDBWithOnlineDevices()
|
||||
|
||||
const usersWithWifiDevices = await prisma.user.findMany({
|
||||
where: {
|
||||
wifiDevices: { some: { lastSeen: { not: null } } }
|
||||
|
|
|
@ -62,7 +62,7 @@ async function updateUserIdMappings() {
|
|||
return wifiDevices.length
|
||||
} catch (e) {
|
||||
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 {
|
||||
setTimeout(() => {
|
||||
working = false
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
import { getOnlineWifiDevices as getOnlineUnifiDevices } from './unifiController'
|
||||
import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController'
|
||||
|
||||
import { updateUserIdMappings } from './paloalto'
|
||||
|
||||
import prisma from '../prisma'
|
||||
|
||||
const DEBOUNCE_TIME_MS = 10000
|
||||
|
||||
let working = false
|
||||
|
||||
async function updateDBWithOnlineDevices() {
|
||||
async function updateDevicesInfo() {
|
||||
if (working) return -1 // Debounce updates
|
||||
|
||||
working = true
|
||||
|
@ -63,8 +61,6 @@ async function updateDBWithOnlineDevices() {
|
|||
}
|
||||
}
|
||||
|
||||
updateUserIdMappings()
|
||||
|
||||
setTimeout(() => {
|
||||
working = false
|
||||
}, DEBOUNCE_TIME_MS)
|
||||
|
@ -77,4 +73,4 @@ async function updateDBWithOnlineDevices() {
|
|||
}
|
||||
}
|
||||
|
||||
export { updateDBWithOnlineDevices }
|
||||
export { updateDevicesInfo }
|
||||
|
|
Loading…
Reference in New Issue
Block a user