|
| 1 | +/* eslint-disable no-continue */ |
| 2 | +/* eslint-disable no-await-in-loop */ |
| 3 | + |
| 4 | +const { promisify } = require('node:util'); |
| 5 | +const { ConnectionFactory } = require('../test/setup-database'); |
| 6 | +const { catalog, server } = require('../lib'); |
| 7 | + |
| 8 | +const sleep = promisify(setTimeout); |
| 9 | +const conn = ConnectionFactory(); |
| 10 | + |
| 11 | +async function waitForServer() { |
| 12 | + for (let retries = 0; retries < 30; retries += 1) { |
| 13 | + if (retries !== 0) { |
| 14 | + await sleep(2000); |
| 15 | + } |
| 16 | + |
| 17 | + // wait for server to be ready |
| 18 | + const serverResp = await server.status(conn).catch(e => e); |
| 19 | + if (!serverResp.ok || serverResp.status !== 200) { |
| 20 | + console.warn('Server is not ready:'); |
| 21 | + console.warn(serverResp); |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + // wait for catalog to be ready or else catalog tests can fail |
| 26 | + const catalogResp = await catalog.status(conn).catch(e => e); |
| 27 | + if ( |
| 28 | + !catalogResp.ok || |
| 29 | + catalogResp.status !== 200 || |
| 30 | + !catalogResp.body?.providers?.length || |
| 31 | + !catalogResp.body?.jobs?.length |
| 32 | + ) { |
| 33 | + console.warn('Catalog is not ready:'); |
| 34 | + console.warn(catalogResp); |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + console.log('Server and catalog are ready'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + throw new Error('Server did not become ready in time'); |
| 43 | +} |
| 44 | + |
| 45 | +waitForServer().catch(err => { |
| 46 | + console.error('Error:', err); |
| 47 | + process.exit(1); |
| 48 | +}); |
0 commit comments