From f7b3529e823076941dd3ea12d8404febf9faae51 Mon Sep 17 00:00:00 2001 From: Josue Date: Tue, 14 Apr 2020 19:15:19 -0400 Subject: [PATCH] Fixed eslint warnings --- package.json | 1 + src/backend/utils/elastic.js | 14 +++++++++----- src/backend/utils/wiki-feed-parser.js | 16 ++++++++++------ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 56fd7d86ed..1c82e82da0 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "pino": "6.2.0", "pino-pretty": "4.0.0", "sanitize-html": "1.23.0", + "set-interval-async": "1.0.32", "stoppable": "1.1.0", "valid-url": "1.0.9" }, diff --git a/src/backend/utils/elastic.js b/src/backend/utils/elastic.js index be36dd4d36..1358181e40 100644 --- a/src/backend/utils/elastic.js +++ b/src/backend/utils/elastic.js @@ -2,6 +2,7 @@ require('../lib/config'); const { ELASTIC_URL, ELASTIC_PORT, ELASTIC_MAX_RESULTS } = process.env; const { Client } = require('@elastic/elasticsearch'); +const { setIntervalAsync, clearIntervalAsync } = require('set-interval-async/dynamic'); const elasticUrl = `${ELASTIC_URL}:${ELASTIC_PORT}` || 'http://localhost:9200'; const esClient = new Client({ node: elasticUrl }); @@ -111,16 +112,19 @@ const waitOnReady = async () => { }); const connectivity = new Promise((resolve) => { - intervalId = setInterval(() => { - checkConnection() - .then(resolve) - .catch(() => logger.info('Attempting to connect to elasticsearch...')); + intervalId = setIntervalAsync(async () => { + try { + await checkConnection(); + resolve(); + } catch (error) { + logger.info('Attempting to connect to elasticsearch...'); + } }, 500); }); await Promise.race([timer, connectivity]); - clearInterval(intervalId); + await clearIntervalAsync(intervalId); clearTimeout(timerId); }; diff --git a/src/backend/utils/wiki-feed-parser.js b/src/backend/utils/wiki-feed-parser.js index 135ccfe9d3..5f756099df 100644 --- a/src/backend/utils/wiki-feed-parser.js +++ b/src/backend/utils/wiki-feed-parser.js @@ -1,6 +1,7 @@ const fetch = require('node-fetch'); const jsdom = require('jsdom'); const { isWebUri } = require('valid-url'); +const { setIntervalAsync, clearIntervalAsync } = require('set-interval-async/dynamic'); require('../lib/config'); const { logger } = require('./logger'); @@ -38,7 +39,8 @@ async function getWikiText(url) { */ module.exports = async function () { let url = process.env.FEED_URL; - const interval = process.env.FEED_URL_INTERVAL_MS || 30000; + // without parseInt(), setIntervalAsync fails, possibly because it takes the value in interval as a string + const interval = parseInt(process.env.FEED_URL_INTERVAL_MS || 30000, 10); if (!url) { url = 'https://wiki.cdot.senecacollege.ca/wiki/Planet_CDOT_Feed_List'; @@ -54,16 +56,18 @@ module.exports = async function () { */ let intervalId; const downloadFeedList = new Promise((resolve) => { - intervalId = setInterval(() => { - getWikiText(url) - .then(resolve) - .catch((error) => logger.info({ error })); + intervalId = setIntervalAsync(async () => { + try { + resolve(await getWikiText(url)); + } catch (error) { + logger.error({ error }); + } }, interval); }); const wikiText = await downloadFeedList; - clearInterval(intervalId); + await clearIntervalAsync(intervalId); const lines = wikiText.split(/\r\n|\r|\n/); const feeds = [];