buildMetadata: use env var for fetching frequency

This commit is contained in:
Julien Bisconti 2018-07-20 19:22:56 +02:00
parent f5c411eb0c
commit 2b8dee31b8
No known key found for this signature in database
GPG Key ID: 62772C6698F736CB

View File

@ -23,6 +23,8 @@ if (!process.env.TOKEN) {
// --- ENV VAR --- // --- ENV VAR ---
const BATCH_SIZE = parseInt(process.env.BATCH_SIZE, 10) || 10; const BATCH_SIZE = parseInt(process.env.BATCH_SIZE, 10) || 10;
const DELAY = parseInt(process.env.DELAY, 10) || 3000; 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 --- // --- FILENAME ---
const README = 'README.md'; const README = 'README.md';
@ -105,30 +107,29 @@ async function batchFetchRepoMetadata(githubRepos) {
return metadata; return metadata;
} }
function shouldUpdate(fileLatestUpdate) { function shouldUpdate(lastUpdateTime) {
LOG.debug({ fileLatestUpdate }); LOG.debug({ lastUpdateTime });
if (!fileLatestUpdate) return true; if (!lastUpdateTime) return true;
const hours = fileLatestUpdate.slice( const hours = lastUpdateTime.slice(
'data/YYYY-MM-DDT'.length, 'data/YYYY-MM-DDT'.length,
'data/YYYY-MM-DDTHH'.length, 'data/YYYY-MM-DDTHH'.length,
); );
const latestUpdate = dayjs( const latestUpdate = dayjs(
fileLatestUpdate.slice('data/'.length, 'data/YYYY-MM-DD'.length), lastUpdateTime.slice('data/'.length, 'data/YYYY-MM-DD'.length),
).add(hours, 'hour'); ).add(hours, 'hour');
LOG.debug({ latestUpdate: latestUpdate.format() }); LOG.debug({ latestUpdate: latestUpdate.format() });
const isMoreThanOneDay = dayjs().diff(latestUpdate, 'hours') >= 1; return dayjs().diff(latestUpdate, INTERVAL_UNIT) >= INTERVAL;
return isMoreThanOneDay;
} }
async function main() { async function main() {
try { try {
const getLatest = await fs.readFile(LATEST_FILENAME, 'utf8'); const lastUpdateTime = await fs.readFile(LATEST_FILENAME, 'utf8');
LOG.debug('Checking if updating is needed'); LOG.debug('Checking if updating is needed');
if (!shouldUpdate(getLatest)) { if (!shouldUpdate(lastUpdateTime)) {
LOG.debug('Last update was less than a day ago 😅. Exiting...'); LOG.debug('Last update was less than a day ago 😅. Exiting...');
process.exit(1); process.exit(1);
} }