ifms-pti/server/src/cronTasks.js

29 lines
656 B
JavaScript
Raw Normal View History

2020-11-06 13:31:28 +00:00
import cron from 'node-cron'
import { updateDBWithOnlineDevices } from './utils/wifiUtils'
import { User } from './classes/User'
import { format } from 'date-fns'
2020-11-06 13:31:28 +00:00
2020-11-11 12:45:04 +00:00
function logMsg(msg) {
2020-11-16 15:14:22 +00:00
console.log(`[${format(new Date(), 'HH:mm:ss')}] cron: ${msg}`)
2020-11-11 12:45:04 +00:00
}
logMsg('Scheduling tasks')
2020-11-06 13:31:28 +00:00
2020-11-11 21:12:37 +00:00
cron.schedule('*/5 * * * *', async () => {
logMsg('updateDBWithOnlineDevices started.')
2020-11-11 21:12:37 +00:00
logMsg(
`updateDBWithOnlineDevices updated ${await updateDBWithOnlineDevices()} devices.`
)
})
2020-11-06 13:31:28 +00:00
cron.schedule(
'0 0 0 * * *',
2020-11-11 12:47:03 +00:00
async () => {
2020-11-11 12:45:04 +00:00
logMsg('User.importAllUsers started')
2020-11-06 13:31:28 +00:00
await User.importAllUsers()
2020-11-11 12:45:04 +00:00
logMsg('User.importAllUsers finished')
2020-11-06 13:31:28 +00:00
},
{}
)