|
1 | 1 | const fs = require('fs'); |
2 | 2 | const run = require('child_process').execSync; |
3 | 3 |
|
4 | | -// read lnd macaroon |
5 | | -const lndmacaroon = fs |
6 | | - .readFileSync('docker/lnd/admin.macaroon') |
7 | | - .toString('hex') |
8 | | - .toUpperCase(); |
| 4 | +// Helper function to retry file reads |
| 5 | +function readFileWithRetry(path, maxRetries = 30, delay = 1000) { |
| 6 | + for (let i = 0; i < maxRetries; i++) { |
| 7 | + try { |
| 8 | + return fs.readFileSync(path); |
| 9 | + } catch (err) { |
| 10 | + if (i === maxRetries - 1) { |
| 11 | + console.error(`Failed to read ${path} after ${maxRetries} attempts`); |
| 12 | + throw err; |
| 13 | + } |
| 14 | + console.error( |
| 15 | + `Attempt ${i + 1}/${maxRetries}: Waiting for ${path}...`, |
| 16 | + ); |
| 17 | + // Sleep synchronously (only works on Unix-like systems) |
| 18 | + run(`sleep ${delay / 1000}`); |
| 19 | + } |
| 20 | + } |
| 21 | +} |
9 | 22 |
|
10 | | -// run command to read clightnng rune |
11 | | -const clightning = run( |
12 | | - 'cd docker; docker compose exec --user clightning clightning lightning-cli createrune --regtest', |
13 | | -); |
14 | | -const lcrune = JSON.parse(clightning).rune; |
| 23 | +// Helper function to retry docker commands |
| 24 | +function runWithRetry(cmd, maxRetries = 30, delay = 1000) { |
| 25 | + for (let i = 0; i < maxRetries; i++) { |
| 26 | + try { |
| 27 | + return run(cmd, { encoding: 'utf8' }); |
| 28 | + } catch (err) { |
| 29 | + if (i === maxRetries - 1) { |
| 30 | + console.error(`Failed to run command after ${maxRetries} attempts: ${cmd}`); |
| 31 | + throw err; |
| 32 | + } |
| 33 | + console.error( |
| 34 | + `Attempt ${i + 1}/${maxRetries}: Command failed, retrying: ${cmd}`, |
| 35 | + ); |
| 36 | + run(`sleep ${delay / 1000}`); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
15 | 40 |
|
16 | | -const context = { lndmacaroon, lcrune }; |
17 | | -const encoded = Buffer.from(JSON.stringify(context)).toString('hex'); |
18 | | -console.log(encoded); |
| 41 | +try { |
| 42 | + // Read lnd macaroon with retry |
| 43 | + console.log('Waiting for LND macaroon...'); |
| 44 | + const lndmacaroon = readFileWithRetry('docker/lnd/admin.macaroon') |
| 45 | + .toString('hex') |
| 46 | + .toUpperCase(); |
| 47 | + console.log('LND macaroon loaded successfully'); |
| 48 | + |
| 49 | + // Run command to read clightning rune with retry |
| 50 | + console.log('Waiting for CLightning to be ready...'); |
| 51 | + const clightning = runWithRetry( |
| 52 | + 'cd docker; docker compose exec --user clightning clightning lightning-cli createrune --regtest', |
| 53 | + ); |
| 54 | + const lcrune = JSON.parse(clightning).rune; |
| 55 | + console.log('CLightning rune generated successfully'); |
| 56 | + |
| 57 | + const context = { lndmacaroon, lcrune }; |
| 58 | + const encoded = Buffer.from(JSON.stringify(context)).toString('hex'); |
| 59 | + console.log(encoded); |
| 60 | +} catch (error) { |
| 61 | + console.error('Failed to prepare test context:', error.message); |
| 62 | + console.error(error.stack); |
| 63 | + process.exit(1); |
| 64 | +} |
0 commit comments