Created tasks

This commit is contained in:
Douglas Barone 2020-12-21 08:14:15 -04:00
parent 67d9a30c00
commit 26429f6968
3 changed files with 65 additions and 25 deletions

View File

@ -13,35 +13,35 @@ logInfo({
message: 'Scheduling tasks...' message: 'Scheduling tasks...'
}) })
cron.schedule('*/1 * * * *', async () => { // cron.schedule('*/1 * * * *', async () => {
logInfo({ // logInfo({
tags: ['cron', 'wifiDevices'], // tags: ['cron', 'wifiDevices'],
message: 'updateDevicesInfo started' // message: 'updateDevicesInfo started'
}) // })
const devices = await updateDevicesInfo() // const devices = await updateDevicesInfo()
logInfo({ // logInfo({
tags: ['cron', 'wifiDevices'], // tags: ['cron', 'wifiDevices'],
message: `updateDevicesInfo updated ${devices} devices` // message: `updateDevicesInfo updated ${devices} devices`
}) // })
let mappings = 0 // let mappings = 0
if (devices > 0) { // if (devices > 0) {
logInfo({ // logInfo({
tags: ['cron', 'user-id'], // tags: ['cron', 'user-id'],
message: `updateUserIdMappings started` // message: `updateUserIdMappings started`
}) // })
mappings = await updateUserIdMappings() // mappings = await updateUserIdMappings()
logInfo({ // logInfo({
tags: ['cron', 'user-id'], // tags: ['cron', 'user-id'],
message: `updateUserIdMappings updated ${mappings} user-id mappings` // message: `updateUserIdMappings updated ${mappings} user-id mappings`
}) // })
} // }
}) // })
cron.schedule( cron.schedule(
'0 0 0 * * *', '0 0 0 * * *',

View File

@ -4,8 +4,6 @@ import './utils/capitalize'
import { logInfo, logSuccess } from './lib/logger' import { logInfo, logSuccess } from './lib/logger'
import { server } from './server' import { server } from './server'
import './cronTasks'
logInfo({ logInfo({
tags: ['server'], tags: ['server'],
level: 'SUCCESS', level: 'SUCCESS',
@ -15,6 +13,9 @@ logInfo({
: 'Running in development' : 'Running in development'
}) })
import './tasks'
import './cronTasks'
server.listen().then(options => { server.listen().then(options => {
logSuccess({ logSuccess({
tags: ['server'], tags: ['server'],

39
server/src/tasks.js Normal file
View File

@ -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()