chore: bg-task optimization

This commit is contained in:
soulteary 2022-01-04 14:18:54 +08:00
parent 6d8ce5361a
commit 0044d265d1
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,19 +6,22 @@ const Logger = require('./Logger');
const loadConfig = require('./loadConfig'); const loadConfig = require('./loadConfig');
const logger = new Logger(); const logger = new Logger();
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 // Update weather data every 15 minutes
const weatherJob = schedule.scheduleJob( const weatherJob = schedule.scheduleJob(
'updateWeather', 'updateWeather',
'0 */15 * * * *', '0 */15 * * * *',
async () => { async () => {
const { WEATHER_API_KEY: secret } = await loadConfig();
try { try {
const weatherData = await getExternalWeather(); 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');
} }
} }
@ -33,3 +36,5 @@ const weatherCleanerJob = schedule.scheduleJob(
clearWeatherData(); clearWeatherData();
} }
); );
}
};