From fde61c64116aa693478b60883e1428aeb482edb5 Mon Sep 17 00:00:00 2001 From: Timothy Geary Date: Mon, 27 Jan 2020 01:59:40 -0500 Subject: [PATCH] Forgot this logic needed to be in a function and adds comments --- src/utils/ingest.ts | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/utils/ingest.ts b/src/utils/ingest.ts index e16e67b..7a2086d 100644 --- a/src/utils/ingest.ts +++ b/src/utils/ingest.ts @@ -463,12 +463,13 @@ export const ingestData = (raw: UnstructuredText): StructuredData | null => { return {genes, annotations, raw}; }; + const { geneMap, annotations, groupedAnnotations } = readData(); let mostRecentGeneMap = geneMap; let mostRecentAnnotations = annotations; let mostRecentGroupedAnnotations = groupedAnnotations; -const updateData = () => { +export const updateData = () => { const { execSync } = require("child_process"); const stdout = execSync("wget http://current.geneontology.org/annotations/tair.gaf.gz ; gunzip tair.gaf.gz"); @@ -478,22 +479,27 @@ const updateData = () => { mostRecentGroupedAnnotations = groupedAnnotations; }; -let nextDate = new Date(); -if (nextDate.getMinutes() === 0 && nextDate.getSeconds() === 0) { - let interval = process.env["UPDATE_INTERVAL"]; - if (interval) { - const runningInterval = setInterval(updateData, parseInt(interval)); +export const startPeriodicUpdates = () => { + let nextDate = new Date(); + // Calls updateData daily, by default, if its the first minute of the day + if (nextDate.getMinutes() === 0) { + let interval = process.env["UPDATE_INTERVAL"]; + if (interval) { + const runningInterval = setInterval(updateData, parseInt(interval)); + } else { + const runningInterval = setInterval(updateData, (1000 * 60 * 60 * 24)); // Default interval is one day in ms + } } else { - const runningInterval = setInterval(updateData, (1000 * 60 * 60 * 24)); + // Otherwise wait until the start of the next day to begin the periodic updates + nextDate.setDate(nextDate.getDate() + 1); + nextDate.setHours(0); + nextDate.setMinutes(0); + nextDate.setSeconds(0); + let difference = nextDate.getTime() - new Date().getTime(); + setTimeout(startPeriodicUpdates, difference); } -} else { - nextDate.setDate(nextDate.getDate() + 1); - nextDate.setHours(0); - nextDate.setMinutes(0); - nextDate.setSeconds(0); - let difference = nextDate.getTime() - new Date().getTime(); - setTimeout(updateData, difference); -} +}; +startPeriodicUpdates(); export const getGeneMap = (): GeneMap => { return mostRecentGeneMap; };