From d0f2feb4adf009f0ecc6d7bb00d81deeaa4b71e9 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Tue, 19 Jan 2021 12:32:39 -0400 Subject: [PATCH] Run user-id updates independently --- server/src/lib/paloalto.js | 11 ++++++++++- server/src/tasks.js | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/server/src/lib/paloalto.js b/server/src/lib/paloalto.js index 0a43526..e3de81d 100644 --- a/server/src/lib/paloalto.js +++ b/server/src/lib/paloalto.js @@ -56,12 +56,18 @@ function createCommand(devices) { ` } +const isWorking = [] + async function updateUserIdMappings() { const allDevices = await getDevicesWithUser() const pAHosts = await prisma.pAHost.findMany() 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)) @@ -82,7 +88,7 @@ async function updateUserIdMappings() { }) logSuccess({ - tags: ['user-id', 'paloalto'], + tags: ['paloalto', 'user-id'], message: `Foram mapeados ${devices.length} user-ids em ${ pAHost.description || pAHost.cidr }`, @@ -100,6 +106,9 @@ async function updateUserIdMappings() { }) return 'Não foi possível atualizar. Veja o log do servidor' + } finally { + const index = isWorking.indexOf(pAHost.id) + if (index > -1) isWorking.splice(index, 1) } }) diff --git a/server/src/tasks.js b/server/src/tasks.js index 04717ad..fcbfb65 100644 --- a/server/src/tasks.js +++ b/server/src/tasks.js @@ -7,14 +7,14 @@ const SLEEP_IN_MILLISECONDS = process.env.TASK_SLEEP || 10000 async function updateDevicesTask() { try { logInfo({ - tags: ['task', 'wifiDevices'], + tags: ['task', 'wifiDevices', 'user-id'], message: 'Atualização de dispositivos iniciou.' }) const devicesQnt = await updateDevicesInfo() if (devicesQnt > 0) { - await updateUserIdMappings() + updateUserIdMappings() } } catch (e) { logError({