Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: schedule scan based on queue events #128

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions lib/scan.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,11 +12,37 @@ const component = 'scan'
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
let isParseQueueActive = false
let isCargoQueueActive = false

function testScheduleNextScan() {
if (!isParseQueueActive && !isCargoQueueActive && !options.oneShot) {
scheduleNextScan()
}
}

/**
* Utility function that calls initHistory() and startScanner()
*/
function initScanner() {
parseQueue.on('task_queued', () => {
logger.info({ component, message: `parse_queue_task_queued` })
isParseQueueActive = true
})
parseQueue.on('drain', () => {
logger.info({ component, message: `parse_queue_drained` })
isParseQueueActive = false
testScheduleNextScan()
})
cargoQueue.on('task_queued', () => {
logger.info({ component, message: `cargo_queue_task_queued` })
isCargoQueueActive = true
})
cargoQueue.on('drain', () => {
logger.info({ component, message: `cargo_queue_drained` })
isCargoQueueActive = false
testScheduleNextScan()
})
initHistory()
startScanner()
Alarm.on('alarmRaised', onAlarmRaised)
Expand Down Expand Up @@ -53,18 +80,12 @@ async function startScanner() {
//Remove stale files: those in historySet but not found in the current scan
removeStaleFiles(discoveredFiles)
logger.info({ component, message: `scan ended`, path: options.path })
// allow queue event handlers to fire before calling testScheduleNextScan()
setTimeout(testScheduleNextScan, 0)
}
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 })
}
}
}

/**
Expand Down Expand Up @@ -97,19 +118,6 @@ function scheduleNextScan() {
})
}

/**
* Cancels the next scan and logs.
* References options properties {path}.
*/
function cancelNextScan() {
clearTimeout(timeoutId)
logger.info({
component,
message: `scan cancelled`,
path: options.path
})
}

/**
* Returns immediately if options.historyFile is falsy.
* Initializes the history Set by reading it from a file and adding each line to the history set.
Expand Down Expand Up @@ -330,7 +338,8 @@ function onAlarmRaised(alarmType) {
message: `handling raised alarm`,
alarmType
})
cancelNextScan()
parseQueue.pause()
cargoQueue.pause()
}

/**
Expand All @@ -344,7 +353,8 @@ function onAlarmLowered(alarmType) {
message: `handling lowered alarm`,
alarmType
})
startScanner()
parseQueue.resume()
cargoQueue.resume()
}


Expand Down