-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
53 lines (47 loc) · 1.36 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const CurlRequest = require('curl-request')
const knex = require('knex')
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
const WAIT_TIME = 5000
const MAX_RETRIES = 10
const curl = new CurlRequest()
const waitForDb = async ({url, fn, retries=0}) => {
if(retries ++ >= MAX_RETRIES) {
console.log(`****** ERROR: Unable to connect: ${url}`)
process.exit(1)
}
if(!await fn()) {
console.log(`====> Waiting on ${url} to become available.`)
await wait(WAIT_TIME)
await waitForDb({url: url, fn: fn, retries: retries})
}
}
const waitForCouch = async (url) => {
await waitForDb({url: url, fn: async () => {
try {
const {statusCode} = await curl.get(url)
if(statusCode === 200) {
console.log(`- Couch [${url}] is now avaliable.`)
return true
}
} catch(err) {}
}})
}
const waitForPg = async (url) => {
const conn = knex({client: 'pg', connection: url})
await waitForDb({url: url, fn: async () => {
try {
await conn.raw('SELECT * FROM pg_catalog.pg_tables')
console.log(`- Postgres [${url}] is now avaliable.`)
await conn.destroy()
return true
} catch(err) {
if(err.code !== 'ECONNREFUSED') {
process.exit(1)
}
}
}})
}
module.exports = async () => {
await waitForCouch(process.env.TEST_COUCH_URL)
await waitForPg(process.env.TEST_PG_URL)
}