2021-05-17 19:23:54 +03:00
|
|
|
const schedule = require('node-schedule');
|
|
|
|
const getExternalWeather = require('./getExternalWeather');
|
2021-06-10 02:51:59 +03:00
|
|
|
const clearWeatherData = require('./clearWeatherData');
|
2021-05-19 18:17:24 +03:00
|
|
|
const Sockets = require('../Sockets');
|
2021-06-22 15:49:00 +03:00
|
|
|
const Logger = require('./Logger');
|
|
|
|
const logger = new Logger();
|
2021-05-17 19:23:54 +03:00
|
|
|
|
2021-06-07 14:40:51 +03:00
|
|
|
// Update weather data every 15 minutes
|
2021-05-17 19:23:54 +03:00
|
|
|
const weatherJob = schedule.scheduleJob('updateWeather', '0 */15 * * * *', async () => {
|
|
|
|
try {
|
2021-06-01 15:54:47 +03:00
|
|
|
const weatherData = await getExternalWeather();
|
2021-06-22 15:49:00 +03:00
|
|
|
logger.log('Weather updated');
|
2021-06-01 15:54:47 +03:00
|
|
|
Sockets.getSocket('weather').socket.send(JSON.stringify(weatherData));
|
2021-05-17 19:23:54 +03:00
|
|
|
} catch (err) {
|
2021-06-22 15:49:00 +03:00
|
|
|
logger.log(err.message, 'ERROR');
|
2021-05-17 19:23:54 +03:00
|
|
|
}
|
2021-06-07 14:40:51 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// Clear old weather data every 4 hours
|
2021-06-10 02:51:59 +03:00
|
|
|
const weatherCleanerJob = schedule.scheduleJob('clearWeather', '0 5 */4 * * *', async () => {
|
|
|
|
clearWeatherData();
|
2021-05-17 19:23:54 +03:00
|
|
|
})
|