Fix working state

This commit is contained in:
Douglas Barone 2021-01-19 16:44:53 -04:00
parent c6d5916b4a
commit 956deb4c69
3 changed files with 13 additions and 11 deletions

View File

@ -59,6 +59,7 @@ function createCommand(devices) {
const isWorking = []
async function updateUserIdMappings() {
// console.log('updateUserIdMappings', isWorking)
const allDevices = await getDevicesWithUser()
const pAHosts = await prisma.pAHost.findMany()
@ -66,15 +67,17 @@ async function updateUserIdMappings() {
const jobs = pAHosts.map(async pAHost => {
if (isWorking.includes(pAHost.id)) return 0
isWorking.push(pAHost.id)
const net = ip.cidrSubnet(pAHost.cidr)
const devices = allDevices.filter(device => net.contains(device.ip))
try {
// console.log(pAHost.id, pAHost.description, devices.length)
if (devices.length == 0) return 0
isWorking.push(pAHost.id)
const cmd = createCommand(devices)
await axios({
@ -109,6 +112,7 @@ async function updateUserIdMappings() {
} finally {
const index = isWorking.indexOf(pAHost.id)
if (index > -1) isWorking.splice(index, 1)
// console.log('finally', isWorking)
}
})

View File

@ -7,7 +7,7 @@ import prisma from '../prisma'
import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub'
import { logError, logSuccess } from './logger'
const DEBOUNCE_TIME_MS = 10000
const DEBOUNCE_TIME_MS = 3000
const RECENT_THRESHOLD_IN_MINUTES = 5
let working = false

View File

@ -5,12 +5,12 @@ import { updateDevicesInfo } from './lib/wifiDevices'
const SLEEP_IN_MILLISECONDS = process.env.TASK_SLEEP || 10000
async function updateDevicesTask() {
try {
logInfo({
tags: ['task', 'wifiDevices', 'user-id'],
message: 'Atualização de dispositivos iniciou.'
})
logInfo({
tags: ['task', 'wifiDevices', 'user-id'],
message: 'Atualização de dispositivos iniciou.'
})
try {
const devicesQnt = await updateDevicesInfo()
if (devicesQnt > 0) {
@ -23,9 +23,7 @@ async function updateDevicesTask() {
data: e
})
} finally {
setTimeout(() => {
updateDevicesTask()
}, SLEEP_IN_MILLISECONDS)
setTimeout(updateDevicesTask, SLEEP_IN_MILLISECONDS)
}
}