ifms-pti/server/src/cronTasks.js
2020-12-09 08:30:17 -04:00

43 lines
941 B
JavaScript

import cron from 'node-cron'
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...')
cron.schedule('*/1 * * * *', async () => {
logMsg('updateDevicesInfo started.')
const devices = await updateDevicesInfo()
let mappings = null
if (devices > 0) mappings = await updateUserIdMappings()
logMsg(
`updateDevicesInfo updated ${devices} devices and ${mappings} user-id mappings.`
)
})
cron.schedule(
'0 0 0 * * *',
async () => {
logMsg('User.importAllUsers started')
await User.importAllUsers()
logMsg('User.importAllUsers finished')
},
{}
)
cron.schedule('0 0 0 * * *', async () => {
await oui.update()
logMsg('Oui updated')
})