forked from medic/couch2pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
55 lines (49 loc) · 1.53 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
54
55
const CurlRequest = require('curl-request')
const knex = require('knex')
const wait = ms => new Promise((r, j)=>setTimeout(r, ms))
const WAIT_TIME = 5000
const curl = new CurlRequest()
const waitForCouchToBecomeAvailable = async (url, timeout, maxRetries) => {
let retries = 0
while(true) {
if(retries ++ >= maxRetries) {
console.log(`****** ERROR: Unable to connect to couch [${url}]!`)
process.exit(1)
}
try {
const {statusCode} = await curl.get(url)
if(statusCode === 200) {
console.log(`- Couch [${url}] is now avaliable.`)
break
}
} catch(err) {}
console.log(`====> Waiting on couch [${url}] to become available.`)
await wait(timeout)
}
}
const waitForPgToBecomeAvailable = async (url, timeout, maxRetries) => {
const conn = knex({client: 'pg', connection: url})
let retries = 0
while(true) {
if(retries ++ >= maxRetries) {
console.log(`****** ERROR: Unable to connect to pg [${url}]!`)
process.exit(1)
}
try {
await conn.raw('SELECT * FROM pg_catalog.pg_tables')
console.log(`- pg [${url}] is now avaliable.`)
await conn.destroy()
break
} catch(err) {
if(err.code !== 'ECONNREFUSED') {
process.exit(1)
}
console.log(`====> Waiting on pg [${url}] to become available.`)
await wait(timeout)
}
}
}
module.exports = async () => {
await waitForCouchToBecomeAvailable(process.env.TEST_COUCH_URL, WAIT_TIME, 10)
await waitForPgToBecomeAvailable(process.env.TEST_PG_URL, WAIT_TIME, 10)
}