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 1 commit
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
36 changes: 28 additions & 8 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,11 @@ 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 })
testScheduleNextScan()
}
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