chore: bg-task optimization
This commit is contained in:
parent
6d8ce5361a
commit
0044d265d1
2 changed files with 28 additions and 22 deletions
|
@ -23,6 +23,7 @@ const logger = new Logger();
|
|||
await initApp();
|
||||
await connectDB();
|
||||
await associateModels();
|
||||
await jobs();
|
||||
|
||||
// Create server for Express API and WebSockets
|
||||
const server = http.createServer();
|
||||
|
|
|
@ -6,30 +6,35 @@ const Logger = require('./Logger');
|
|||
const loadConfig = require('./loadConfig');
|
||||
const logger = new Logger();
|
||||
|
||||
// Update weather data every 15 minutes
|
||||
const weatherJob = schedule.scheduleJob(
|
||||
module.exports = async function () {
|
||||
const { WEATHER_API_KEY } = await loadConfig();
|
||||
const FEAT_WHEATHER_ENABLED = WEATHER_API_KEY != '';
|
||||
|
||||
if (FEAT_WHEATHER_ENABLED) {
|
||||
// Update weather data every 15 minutes
|
||||
const weatherJob = schedule.scheduleJob(
|
||||
'updateWeather',
|
||||
'0 */15 * * * *',
|
||||
async () => {
|
||||
const { WEATHER_API_KEY: secret } = await loadConfig();
|
||||
|
||||
try {
|
||||
const weatherData = await getExternalWeather();
|
||||
|
||||
Sockets.getSocket('weather').socket.send(JSON.stringify(weatherData));
|
||||
} catch (err) {
|
||||
if (secret) {
|
||||
if (WEATHER_API_KEY) {
|
||||
logger.log(err.message, 'ERROR');
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
// Clear old weather data every 4 hours
|
||||
const weatherCleanerJob = schedule.scheduleJob(
|
||||
// Clear old weather data every 4 hours
|
||||
const weatherCleanerJob = schedule.scheduleJob(
|
||||
'clearWeather',
|
||||
'0 5 */4 * * *',
|
||||
async () => {
|
||||
clearWeatherData();
|
||||
}
|
||||
);
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue