lts-stats-api/app/tasks/sync.js

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-01-06 19:12:43 +01:00
const cron = require('node-cron');
const moment = require('moment');
const { getUsersWhitelist, getUsers } = require('../db/mysql/lsaUsers');
const { RedisClient } = require('../db/redis/redis');
2023-01-15 18:42:26 +01:00
const { getHistoricalSessions } = require('../requests/ivao/session');
const { insertSessions } = require('../db/mongo/mongoSessions');
2023-01-06 19:12:43 +01:00
2023-01-15 18:42:26 +01:00
const canRunTasks = process.env.EXECUTE_TASKS_ON_START !== 'false';
async function taskSyncLSAUsers() {
2023-01-06 19:12:43 +01:00
console.log('Running task', moment().format('HH:mm:ss'));
2023-01-15 18:42:26 +01:00
try {
const users = await getUsers();
const whitelist = await getUsersWhitelist();
RedisClient.setCollection([
['users', users],
['users_whitelist', whitelist],
]);
} catch (err) {
2023-09-09 18:24:57 +02:00
console.log('err :>> ', err);
2023-01-15 18:42:26 +01:00
console.log('ERR executing taskSyncLSAUsers');
}
}
async function taskSyncPrevioudDaySessions(callsign) {
try {
const from = moment().utc().subtract(1, 'day').startOf('day').format();
const to = moment().utc().subtract(1, 'day').endOf('day').format();
console.log(moment().format('DD HH:mm:ss'), 'taskSyncPrevioudDaySessions', from, to);
const params = {
callsign,
from,
to,
};
const data = await getHistoricalSessions(params);
insertSessions(data);
} catch (err) {
console.log('ERR executing taskSyncPrevioudDaySessions');
}
2023-01-06 19:12:43 +01:00
}
2023-09-09 18:24:57 +02:00
module.exports = {
sync: function() {
console.log('canRunTasks :>> ', canRunTasks);
if (canRunTasks) {
cron.schedule(process.env.SYNC_TASK_SCHEDULE, () => {
taskSyncPrevioudDaySessions('LTS');
});
// cron.schedule(process.env.USERS_TASK_SCHEDULE, async() => {
// taskSyncLSAUsers();
// });
console.log('Tasks started.');
} else {
console.log('Tasks skipped.');
}
},
taskSyncLSAUsers,
taskSyncPrevioudDaySessions,
};