Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
feat: restart watcher if no events seen for specified period
Browse files Browse the repository at this point in the history
  • Loading branch information
Flydiverny committed Nov 1, 2020
1 parent 95e4819 commit 1303de9
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/external-secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,39 @@ async function startWatcher ({
const jsonStream = new JSONStream()
stream.pipe(jsonStream)

jsonStream.on('data', eventQueue.put)
let timeout
const startTimeout = () => {
if (timeout) {
clearTimeout(timeout)
}

const timeMs = 60000
timeout = setTimeout(() => {
logger.info(`No watch event for ${timeMs} ms, restarting watcher`)
deathQueue.put('RESTART')
}, timeMs)
timeout.unref()
}

jsonStream.on('data', (evt) => {
eventQueue.put(evt)
startTimeout()
})

jsonStream.on('error', (err) => {
logger.warn(err, 'Got error on stream')
deathQueue.put('ERROR')
clearTimeout(timeout)
})

jsonStream.on('end', () => {
deathQueue.put('END')
clearTimeout(timeout)
})

await deathQueue.take()
const deathEvent = await deathQueue.take()

logger.debug('Stopping watch stream')
logger.info('Stopping watch stream due to event: %s', deathEvent)
eventQueue.put({ type: 'DELETED_ALL' })

stream.abort()
Expand Down

0 comments on commit 1303de9

Please sign in to comment.