From 26429f69685c5060ad515497fba2d6156997f3e5 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Mon, 21 Dec 2020 08:14:15 -0400 Subject: [PATCH] Created tasks --- server/src/cronTasks.js | 46 ++++++++++++++++++++--------------------- server/src/index.js | 5 +++-- server/src/tasks.js | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 server/src/tasks.js diff --git a/server/src/cronTasks.js b/server/src/cronTasks.js index 60e3663..8b0ce22 100644 --- a/server/src/cronTasks.js +++ b/server/src/cronTasks.js @@ -13,35 +13,35 @@ logInfo({ message: 'Scheduling tasks...' }) -cron.schedule('*/1 * * * *', async () => { - logInfo({ - tags: ['cron', 'wifiDevices'], - message: 'updateDevicesInfo started' - }) +// cron.schedule('*/1 * * * *', async () => { +// logInfo({ +// tags: ['cron', 'wifiDevices'], +// message: 'updateDevicesInfo started' +// }) - const devices = await updateDevicesInfo() +// const devices = await updateDevicesInfo() - logInfo({ - tags: ['cron', 'wifiDevices'], - message: `updateDevicesInfo updated ${devices} devices` - }) +// logInfo({ +// tags: ['cron', 'wifiDevices'], +// message: `updateDevicesInfo updated ${devices} devices` +// }) - let mappings = 0 +// let mappings = 0 - if (devices > 0) { - logInfo({ - tags: ['cron', 'user-id'], - message: `updateUserIdMappings started` - }) +// if (devices > 0) { +// logInfo({ +// tags: ['cron', 'user-id'], +// message: `updateUserIdMappings started` +// }) - mappings = await updateUserIdMappings() +// mappings = await updateUserIdMappings() - logInfo({ - tags: ['cron', 'user-id'], - message: `updateUserIdMappings updated ${mappings} user-id mappings` - }) - } -}) +// logInfo({ +// tags: ['cron', 'user-id'], +// message: `updateUserIdMappings updated ${mappings} user-id mappings` +// }) +// } +// }) cron.schedule( '0 0 0 * * *', diff --git a/server/src/index.js b/server/src/index.js index 583916b..ffcf515 100755 --- a/server/src/index.js +++ b/server/src/index.js @@ -4,8 +4,6 @@ import './utils/capitalize' import { logInfo, logSuccess } from './lib/logger' import { server } from './server' -import './cronTasks' - logInfo({ tags: ['server'], level: 'SUCCESS', @@ -15,6 +13,9 @@ logInfo({ : 'Running in development' }) +import './tasks' +import './cronTasks' + server.listen().then(options => { logSuccess({ tags: ['server'], diff --git a/server/src/tasks.js b/server/src/tasks.js new file mode 100644 index 0000000..0de8614 --- /dev/null +++ b/server/src/tasks.js @@ -0,0 +1,39 @@ +import { logError, logInfo } from './lib/logger' +import { updateUserIdMappings } from './lib/paloalto' +import { updateDevicesInfo } from './lib/wifiDevices' + +const SLEEP_IN_MILLISECONDS = 10000 + +logInfo({ + tags: ['task'], + message: 'Starting tasks...' +}) + +async function updateDevicesTask() { + try { + logInfo({ + tags: ['task', 'wifiDevices'], + message: 'updateDevicesTask started' + }) + + const devices = await updateDevicesInfo() + + let mappings = 0 + + if (devices > 0) { + mappings = await updateUserIdMappings() + } + } catch (e) { + logError({ + tags: ['task', 'wifiDevices', 'user-id'], + message: `Error executing task.`, + data: e + }) + } finally { + setTimeout(() => { + updateDevicesTask() + }, SLEEP_IN_MILLISECONDS) + } +} + +updateDevicesTask()