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