Skip to content

Commit 9ad9c4d

Browse files
authored
Merge pull request #13 from greenkeeperio/feat/registry-change-delay
feat: wait 60s before issueing a registry-change job
2 parents c4dd59e + 48d6ce6 commit 9ad9c4d

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

lib/env.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const envalid = require('envalid')
2-
const {str, url, json, bool} = envalid
2+
const {str, url, json, bool, num} = envalid
33

44
const urlArray = envalid.makeValidator(x => {
55
const urls = json()._parse(x)
@@ -16,5 +16,6 @@ module.exports = envalid.cleanEnv(process.env, {
1616
NODE_ENV: str({choices: ['development', 'staging', 'production'], devDefault: 'development'}),
1717
ROLLBAR_TOKEN_CHANGES: str({devDefault: ''}),
1818
STATSD_HOST: str({default: '172.17.0.1'}),
19+
REGISTRY_CHANGE_DELAY: num({default: 1000 * 60, devDefault: 1000}),
1920
IS_ENTERPRISE: bool({default: false})
2021
})

lib/follow.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const cleanDoc = require('normalize-registry-metadata')
99

1010
const env = require('./env')
1111
const rollbar = require('./rollbar')
12+
const { asyncTimeout } = require('./util')
13+
1214
const statsdClient = new StatsD({
1315
host: env.STATSD_HOST,
1416
prefix: 'changes.',
@@ -75,7 +77,10 @@ async function handleChange ({channel, client, registry}, change) {
7577
}
7678

7779
try {
78-
await channel.sendToQueue(env.QUEUE_NAME, Buffer.from(JSON.stringify(payload)), {priority: 1})
80+
await asyncTimeout(env.REGISTRY_CHANGE_DELAY, async () => {
81+
const payloadBuffer = Buffer.from(JSON.stringify(payload))
82+
await channel.sendToQueue(env.QUEUE_NAME, payloadBuffer, {priority: 1})
83+
})
7984
} catch (err) {
8085
rollbar.error(err)
8186
}

lib/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// https://stackoverflow.com/questions/33289726/combination-of-async-function-await-settimeout#33292942
2+
function sleep (ms) {
3+
return new Promise((resolve, reject) => setTimeout(resolve, ms))
4+
}
5+
6+
async function asyncTimeout (ms, fn, ...args) {
7+
await sleep(ms)
8+
return fn(...args)
9+
}
10+
11+
module.exports.sleep = sleep
12+
module.exports.asyncTimeout = asyncTimeout

test/follow.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {test, afterEach, tearDown} = require('tap')
77
const proxyquire = require('proxyquire')
88

99
const env = require('../lib/env')
10+
const { sleep } = require('../lib/util')
1011

1112
class ChangesStream extends EventEmitter {
1213
constructor (opts) {
@@ -161,7 +162,18 @@ const {
161162
}
162163
}
163164
})
164-
const job = await channel.get(env.QUEUE_NAME)
165+
166+
let job
167+
do {
168+
try {
169+
job = await channel.get(env.QUEUE_NAME)
170+
await sleep(env.REGISTRY_CHANGE_DELAY / 2)
171+
} catch (e) {
172+
t.error(e)
173+
break
174+
}
175+
} while (job === false)
176+
165177
t.same(JSON.parse(job.content.toString()), {
166178
name: 'registry-change',
167179
dependency: 'package',

0 commit comments

Comments
 (0)