From b6a453526a1824f08b2699d22cce11a28632242b Mon Sep 17 00:00:00 2001 From: cd-rite <61710958+cd-rite@users.noreply.github.com> Date: Mon, 10 Jun 2024 22:06:50 -0400 Subject: [PATCH] always schedule scan, on scan start, check to see if both queues are empty. If empty, run scan, schedule next one. If not empty, skip scan, schedule next one. --- lib/scan.js | 79 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/lib/scan.js b/lib/scan.js index 0b96eea..9727fe0 100644 --- a/lib/scan.js +++ b/lib/scan.js @@ -1,6 +1,7 @@ import { logger } from './logger.js' import { options } from './args.js' import { queue as parseQueue} from './parse.js' +import { cargoQueue } from './cargo.js' import { serializeError } from 'serialize-error' import fg from 'fast-glob' import lineByLine from 'n-readlines' @@ -12,6 +13,7 @@ const historySet = new Set() // in memory history set let isWriteScheduled = false // flag to indicate if there is pending files to write to the history file let timeoutId // id of the active setTimeout + /** * Utility function that calls initHistory() and startScanner() */ @@ -28,43 +30,52 @@ function initScanner() { */ async function startScanner() { const discoveredFiles = new Set() // in memory set of files discovered in the current scan - try { - // scan the path for files - const stream = fg.stream([`${options.path}/**/*.ckl`, `${options.path}/**/*.xml`, `${options.path}/**/*.cklb`], { - dot: !options.ignoreDot, - suppressErrors: true, - ignore: options.ignoreGlob ?? [] - }) - logger.info({ component, message: `scan started`, path: options.path }) - // for each file discovered - for await (const entry of stream) { - discoveredFiles.add(entry) - logger.verbose({ component, message: `discovered file`, file: entry }) - // check if the file is in the history - if (historySet.has(entry)) { - logger.verbose({component, message: `history match`, file: entry}) - } - // if the file is not in the history, add it to the in memory history set. - else { - parseQueue.push(entry) - logger.info({component, message: `queued for parsing`, file: entry}) + + try { + //if the parseQueue and cargoQueue are empty, start a scan + if (parseQueue.length == 0 && cargoQueue.length == 0) { + logger.info({ component, message: `queue empty, starting scan` }) + // scan the path for files + const stream = fg.stream([`${options.path}/**/*.ckl`, `${options.path}/**/*.xml`, `${options.path}/**/*.cklb`], { + dot: !options.ignoreDot, + suppressErrors: true, + ignore: options.ignoreGlob ?? [] + }) + logger.info({ component, message: `scan started`, path: options.path }) + // for each file discovered + for await (const entry of stream) { + discoveredFiles.add(entry) + logger.verbose({ component, message: `discovered file`, file: entry }) + // check if the file is in the history + if (historySet.has(entry)) { + logger.verbose({component, message: `history match`, file: entry}) + } + // if the file is not in the history, add it to the in memory history set. + else { + parseQueue.push(entry) + logger.info({component, message: `queued for parsing`, file: entry}) + } } + //Remove stale files: those in historySet but not found in the current scan + removeStaleFiles(discoveredFiles) + logger.info({ component, message: `scan ended`, path: options.path }) + } + else { + logger.info({ component, message: `scan skipped, queue not empty. parseQueue: ${parseQueue.length} cargoQueue: ${cargoQueue.length}` }) } - //Remove stale files: those in historySet but not found in the current scan - removeStaleFiles(discoveredFiles) - logger.info({ component, message: `scan ended`, path: options.path }) - } - catch (e) { - logger.error({ component, error: serializeError(e) }) - } - finally { - if (!options.oneShot) { - scheduleNextScan() } - else { - logger.info({ component, message: `one-shot scan completed`, path: options.path }) + catch (e) { + logger.error({ component, error: serializeError(e) }) } - } + finally { + // if not a one-shot scan, always schedule the next scan + if (!options.oneShot) { + scheduleNextScan() + } + else { + logger.info({ component, message: `one-shot scan completed`, path: options.path }) + } + } } /** @@ -354,4 +365,4 @@ export { initScanner, addToHistory, removeFromHistory, -} +} \ No newline at end of file