Skip to content

Commit

Permalink
feat: wait 60s before issueing a registry-change job
Browse files Browse the repository at this point in the history
  • Loading branch information
janl committed Mar 14, 2018
1 parent c4dd59e commit 48d6ce6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const envalid = require('envalid')
const {str, url, json, bool} = envalid
const {str, url, json, bool, num} = envalid

const urlArray = envalid.makeValidator(x => {
const urls = json()._parse(x)
Expand All @@ -16,5 +16,6 @@ module.exports = envalid.cleanEnv(process.env, {
NODE_ENV: str({choices: ['development', 'staging', 'production'], devDefault: 'development'}),
ROLLBAR_TOKEN_CHANGES: str({devDefault: ''}),
STATSD_HOST: str({default: '172.17.0.1'}),
REGISTRY_CHANGE_DELAY: num({default: 1000 * 60, devDefault: 1000}),
IS_ENTERPRISE: bool({default: false})
})
7 changes: 6 additions & 1 deletion lib/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const cleanDoc = require('normalize-registry-metadata')

const env = require('./env')
const rollbar = require('./rollbar')
const { asyncTimeout } = require('./util')

const statsdClient = new StatsD({
host: env.STATSD_HOST,
prefix: 'changes.',
Expand Down Expand Up @@ -75,7 +77,10 @@ async function handleChange ({channel, client, registry}, change) {
}

try {
await channel.sendToQueue(env.QUEUE_NAME, Buffer.from(JSON.stringify(payload)), {priority: 1})
await asyncTimeout(env.REGISTRY_CHANGE_DELAY, async () => {
const payloadBuffer = Buffer.from(JSON.stringify(payload))
await channel.sendToQueue(env.QUEUE_NAME, payloadBuffer, {priority: 1})
})
} catch (err) {
rollbar.error(err)
}
Expand Down
12 changes: 12 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://stackoverflow.com/questions/33289726/combination-of-async-function-await-settimeout#33292942
function sleep (ms) {
return new Promise((resolve, reject) => setTimeout(resolve, ms))
}

async function asyncTimeout (ms, fn, ...args) {
await sleep(ms)
return fn(...args)
}

module.exports.sleep = sleep
module.exports.asyncTimeout = asyncTimeout
14 changes: 13 additions & 1 deletion test/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {test, afterEach, tearDown} = require('tap')
const proxyquire = require('proxyquire')

const env = require('../lib/env')
const { sleep } = require('../lib/util')

class ChangesStream extends EventEmitter {
constructor (opts) {
Expand Down Expand Up @@ -161,7 +162,18 @@ const {
}
}
})
const job = await channel.get(env.QUEUE_NAME)

let job
do {
try {
job = await channel.get(env.QUEUE_NAME)
await sleep(env.REGISTRY_CHANGE_DELAY / 2)
} catch (e) {
t.error(e)
break
}
} while (job === false)

t.same(JSON.parse(job.content.toString()), {
name: 'registry-change',
dependency: 'package',
Expand Down

0 comments on commit 48d6ce6

Please # to comment.