Skip to content

Commit dcd34d4

Browse files
committed
close connections properly
1 parent 9e94722 commit dcd34d4

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

index.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ const StartLedger = typeof process.env.LEDGER === 'undefined' ? 32570 : parseInt
1616
console.log('Fetch XRPL transactions into Google BigQuery')
1717

1818
const Client = new XrplClient(XRPLNodeUrl)
19+
20+
async function safeHalt() {
21+
try {
22+
await Client.close()
23+
} catch(e) {
24+
console.error('ERROR closing connection:', e)
25+
} finally {
26+
process.exit(1)
27+
}
28+
}
1929

2030
Client.ready().then(Connection => {
2131
let Stopped = false
@@ -176,47 +186,34 @@ Client.ready().then(Connection => {
176186

177187
return _Tx
178188
})
179-
180-
// console.dir(Transactions[0], { depth: null })
181-
// process.exit(1)
182189

183190
bigquery.dataset(DATASET_NAME).table(TRANSACTION_TABLE_NAME).insert(Transactions)
184191
.then(r => {
185192
console.log(`Inserted rows`, r)
186193
LastLedger = Result.ledger_index
187-
// process.exit(0)
188194
})
189195
.catch(err => {
190196
if (err && err.name === 'PartialFailureError') {
191197
if (err.errors && err.errors.length > 0) {
192198
console.log('Insert errors:')
193199
err.errors.forEach(err => console.dir(err, { depth: null }))
194-
process.exit(1)
200+
return safeHalt()
195201
}
196202
} else {
197203
console.error('ERROR:', err)
198-
process.exit(1)
204+
return safeHalt()
199205
}
200206
})
201207
}
202208

203-
// retryTimeout = 0
204-
205209
if (Stopped) {
206-
return
210+
return Connection.close()
207211
}
208212

209213
return run(ledger_index + 1)
210214
}).catch(e => {
211215
console.log(e)
212-
process.exit(1)
213-
214-
// retryTimeout += 500
215-
// if (retryTimeout > 5000) retryTimeout = 5000
216-
console.log(`Oops... Retry in ${retryTimeout / 1000} sec.`)
217-
setTimeout(() => {
218-
return run(ledger_index)
219-
}, retryTimeout * 1000)
216+
return safeHalt()
220217
})
221218
}
222219

@@ -240,14 +237,13 @@ Client.ready().then(Connection => {
240237
}
241238
}).catch(e => {
242239
console.log('Google BigQuery Error', e)
243-
process.exit(1)
240+
return safeHalt()
244241
})
245242

246243
process.on('SIGINT', function() {
247244
console.log(`\nGracefully shutting down from SIGINT (Ctrl+C)\n -- Wait for remaining BigQuery inserts and XRPL Connection close...`);
248245

249246
Stopped = true
250-
Connection.close()
251247
if (LastLedger > 0) {
252248
console.log(`\nLast ledger: [ ${LastLedger} ]\n\nRun your next job with ENV: "LEDGER=${LastLedger+1}"\n\n`)
253249
}

ledgerInfo.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ const StartLedger = typeof process.env.LEDGER === 'undefined' ? 32570 : parseInt
1414
console.log('Fetch XRPL Ledger Info into Google BigQuery')
1515

1616
const Client = new XrplClient(XRPLNodeUrl)
17+
18+
async function safeHalt() {
19+
try {
20+
await Client.close()
21+
} catch(e) {
22+
console.error('ERROR closing connection:', e)
23+
} finally {
24+
process.exit(1)
25+
}
26+
}
1727

1828
Client.ready().then(Connection => {
1929
let Stopped = false
@@ -39,7 +49,6 @@ Client.ready().then(Connection => {
3949
const run = (ledger_index) => {
4050
return fetchLedger(ledger_index).then(Result => {
4151
console.log(`${Result.ledger_index}`)
42-
// console.log(Result)
4352
bigquery.dataset(DATASET_NAME).table(LEDGER_TABLE_NAME).insert([{
4453
LedgerIndex: parseInt(Result.ledger.ledger_index),
4554
hash: Result.ledger.hash,
@@ -55,38 +64,28 @@ Client.ready().then(Connection => {
5564
.then(r => {
5665
console.log(`Inserted rows`, r)
5766
LastLedger = Result.ledger_index
58-
// process.exit(0)
5967
})
6068
.catch(err => {
6169
if (err && err.name === 'PartialFailureError') {
6270
if (err.errors && err.errors.length > 0) {
6371
console.log('Insert errors:')
6472
err.errors.forEach(err => console.dir(err, { depth: null }))
65-
process.exit(1)
73+
return safeHalt()
6674
}
6775
} else {
6876
console.error('ERROR:', err)
69-
process.exit(1)
77+
return safeHalt()
7078
}
7179
})
72-
73-
// retryTimeout = 0
7480

7581
if (Stopped) {
76-
return
82+
return Connection.close()
7783
}
7884

7985
return run(ledger_index + 1)
8086
}).catch(e => {
8187
console.log(e)
82-
process.exit(1)
83-
84-
// retryTimeout += 500
85-
// if (retryTimeout > 5000) retryTimeout = 5000
86-
console.log(`Oops... Retry in ${retryTimeout / 1000} sec.`)
87-
setTimeout(() => {
88-
return run(ledger_index)
89-
}, retryTimeout * 1000)
88+
return safeHalt()
9089
})
9190
}
9291

@@ -107,14 +106,13 @@ Client.ready().then(Connection => {
107106
}
108107
}).catch(e => {
109108
console.log('Google BigQuery Error', e)
110-
process.exit(1)
109+
return safeHalt()
111110
})
112111

113112
process.on('SIGINT', function() {
114113
console.log(`\nGracefully shutting down from SIGINT (Ctrl+C)\n -- Wait for remaining BigQuery inserts and XRPL Connection close...`);
115114

116115
Stopped = true
117-
Connection.close()
118116
if (LastLedger > 0) {
119117
console.log(`\nLast ledger: [ ${LastLedger} ]\n\nRun your next job with ENV: "LEDGER=${LastLedger+1}"\n\n`)
120118
}

0 commit comments

Comments
 (0)