From 2b8dee31b8beb4656bb77df09277d91bfb177a58 Mon Sep 17 00:00:00 2001 From: Julien Bisconti Date: Fri, 20 Jul 2018 19:22:56 +0200 Subject: [PATCH] buildMetadata: use env var for fetching frequency --- buildMetadata.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/buildMetadata.js b/buildMetadata.js index c75324a..4ac43fd 100644 --- a/buildMetadata.js +++ b/buildMetadata.js @@ -23,6 +23,8 @@ if (!process.env.TOKEN) { // --- ENV VAR --- const BATCH_SIZE = parseInt(process.env.BATCH_SIZE, 10) || 10; const DELAY = parseInt(process.env.DELAY, 10) || 3000; +const INTERVAL = parseInt(process.env.INTERVAL, 10) || 1; +const INTERVAL_UNIT = process.env.INTERVAL_UNIT || 'days'; // --- FILENAME --- const README = 'README.md'; @@ -105,30 +107,29 @@ async function batchFetchRepoMetadata(githubRepos) { return metadata; } -function shouldUpdate(fileLatestUpdate) { - LOG.debug({ fileLatestUpdate }); - if (!fileLatestUpdate) return true; +function shouldUpdate(lastUpdateTime) { + LOG.debug({ lastUpdateTime }); + if (!lastUpdateTime) return true; - const hours = fileLatestUpdate.slice( + const hours = lastUpdateTime.slice( 'data/YYYY-MM-DDT'.length, 'data/YYYY-MM-DDTHH'.length, ); const latestUpdate = dayjs( - fileLatestUpdate.slice('data/'.length, 'data/YYYY-MM-DD'.length), + lastUpdateTime.slice('data/'.length, 'data/YYYY-MM-DD'.length), ).add(hours, 'hour'); LOG.debug({ latestUpdate: latestUpdate.format() }); - const isMoreThanOneDay = dayjs().diff(latestUpdate, 'hours') >= 1; - return isMoreThanOneDay; + return dayjs().diff(latestUpdate, INTERVAL_UNIT) >= INTERVAL; } async function main() { try { - const getLatest = await fs.readFile(LATEST_FILENAME, 'utf8'); + const lastUpdateTime = await fs.readFile(LATEST_FILENAME, 'utf8'); LOG.debug('Checking if updating is needed'); - if (!shouldUpdate(getLatest)) { + if (!shouldUpdate(lastUpdateTime)) { LOG.debug('Last update was less than a day ago 😅. Exiting...'); process.exit(1); }