floriday/index.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-01-09 10:59:07 +00:00
import moment from 'moment';
import { getClientToken } from './utils.js';
2023-01-09 10:59:07 +00:00
// Check the existence of the .env file
import dotenv from 'dotenv';
dotenv.config();
2022-12-05 12:53:30 +00:00
2023-01-09 10:59:07 +00:00
import models from './models/index.js';
2022-12-05 12:53:30 +00:00
let AccessToken = await getClientToken(models);
2023-01-09 10:59:07 +00:00
// eslint-disable-next-line no-unused-vars
let tokenValue = AccessToken[0];
let tokenExpirationDate = AccessToken[1];
2022-12-05 12:53:30 +00:00
try {
// Every 30 sec query the database
setInterval(async () => {
2023-01-09 10:59:07 +00:00
console.log('Querying the API to check for new data...');
console.log('Current token expiration date: ', tokenExpirationDate);
if (moment().isAfter(tokenExpirationDate)) {
2023-01-09 10:59:07 +00:00
console.log('Token expired, getting a new one...');
AccessToken = await getClientToken(models);
tokenValue = AccessToken[0];
tokenExpirationDate = AccessToken[1];
}
2023-01-09 10:59:07 +00:00
}, process.env.STATUS == 'development' ? 2500 : 5000);
2022-12-05 12:53:30 +00:00
} catch (error) {
2023-01-09 10:59:07 +00:00
console.error('Unable to connect to the database:', error);
2022-12-05 12:53:30 +00:00
}
console.log = function () {
2023-01-09 10:59:07 +00:00
let args = Array.prototype.slice.call(arguments);
args.unshift(new moment().format('HH:mm:ss') + ' -');
console.info.apply(console, args);
};