Skip to content
Permalink
Browse files

feat(pubsub): abort script if messages appear to stall

  • Loading branch information...
philbooth authored and jbuck committed Jun 5, 2019
1 parent af90dd2 commit 64f951b663ba8221c63dd19c546c348b69dfd79e
Showing with 14 additions and 0 deletions.
  1. +14 −0 pubsub/index.js
@@ -18,6 +18,8 @@ if (! AMPLITUDE_API_KEY || ! HMAC_KEY || ! PUBSUB_PROJECT || ! PUBSUB_TOPIC || !
process.exit(1)
}

const TIMEOUT_THRESHOLD = parseInt(process.env.TIMEOUT_THRESHOLD || '60000');

const IGNORED_EVENTS = new Map()
if (process.env.IGNORED_EVENTS) {
// process.env.IGNORED_EVENTS is a JSON object of event_type:criteria, e.g.:
@@ -91,7 +93,14 @@ async function main () {
identify: setupCargo(ENDPOINTS.IDENTIFY_API, KEYS.IDENTIFY_API),
}

let timeout;

subscription.on('message', message => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(onTimeout, TIMEOUT_THRESHOLD);

processMessage(cargo, message)
})

@@ -286,3 +295,8 @@ function clearMessages (payload, action, forceAction = false) {
}
})
}

function onTimeout () {
console.log(timestamp(), `Error: no messages received in ${TIMEOUT_THRESHOLD / SECOND} seconds`)
process.exit(1)
}

0 comments on commit 64f951b

Please sign in to comment.
You can’t perform that action at this time.