Merge pull request #282 from soulteary/chore/background-task-optimization

chore: bg-task optimization
This commit is contained in:
pawelmalak 2022-01-08 12:58:06 +01:00 committed by GitHub
commit e0ecf34ced
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 22 deletions

View file

@ -23,6 +23,7 @@ const logger = new Logger();
await initApp(); await initApp();
await connectDB(); await connectDB();
await associateModels(); await associateModels();
await jobs();
// Create server for Express API and WebSockets // Create server for Express API and WebSockets
const server = http.createServer(); const server = http.createServer();

View file

@ -6,30 +6,35 @@ const Logger = require('./Logger');
const loadConfig = require('./loadConfig'); const loadConfig = require('./loadConfig');
const logger = new Logger(); const logger = new Logger();
// Update weather data every 15 minutes module.exports = async function () {
const weatherJob = schedule.scheduleJob( const { WEATHER_API_KEY } = await loadConfig();
'updateWeather', const FEAT_WHEATHER_ENABLED = WEATHER_API_KEY != '';
'0 */15 * * * *',
async () => {
const { WEATHER_API_KEY: secret } = await loadConfig();
try { if (FEAT_WHEATHER_ENABLED) {
const weatherData = await getExternalWeather(); // Update weather data every 15 minutes
const weatherJob = schedule.scheduleJob(
'updateWeather',
'0 */15 * * * *',
async () => {
try {
const weatherData = await getExternalWeather();
Sockets.getSocket('weather').socket.send(JSON.stringify(weatherData)); Sockets.getSocket('weather').socket.send(JSON.stringify(weatherData));
} catch (err) { } catch (err) {
if (secret) { if (WEATHER_API_KEY) {
logger.log(err.message, 'ERROR'); logger.log(err.message, 'ERROR');
}
}
} }
} );
}
);
// Clear old weather data every 4 hours // Clear old weather data every 4 hours
const weatherCleanerJob = schedule.scheduleJob( const weatherCleanerJob = schedule.scheduleJob(
'clearWeather', 'clearWeather',
'0 5 */4 * * *', '0 5 */4 * * *',
async () => { async () => {
clearWeatherData(); clearWeatherData();
}
);
} }
); };