From 64341c04911ef1120dbcc5c04142a6741beede09 Mon Sep 17 00:00:00 2001 From: jairajdev Date: Tue, 9 Apr 2024 21:49:27 +0545 Subject: [PATCH] Update closeDatabase function --- scripts/accountentry_creator.ts | 3 +- scripts/accounthistorystate_creator.ts | 3 +- scripts/data_patcher.ts | 3 +- scripts/query-perf-checker/block.ts | 3 +- scripts/query-perf-checker/transaction.ts | 3 +- scripts/repair_account_state.ts | 4 +- scripts/repair_missing_cycle_block.ts | 3 +- src/collector.ts | 1 - src/storage/index.ts | 8 ++- src/storage/sqlite3storage.ts | 74 +++++++++++------------ 10 files changed, 51 insertions(+), 54 deletions(-) diff --git a/scripts/accountentry_creator.ts b/scripts/accountentry_creator.ts index b4bcc86..088475e 100644 --- a/scripts/accountentry_creator.ts +++ b/scripts/accountentry_creator.ts @@ -2,7 +2,6 @@ import * as Storage from '../src/storage' import * as AccountEntryDB from '../src/storage/accountEntry' import * as AccountDB from '../src/storage/account' import { AccountSearchType } from '../src/types' -import { closeDatabase } from '../src/storage/sqlite3storage' const start = async (): Promise => { await Storage.initializeDB() @@ -18,7 +17,7 @@ const start = async (): Promise => { } const accountEntriesCount = await AccountEntryDB.queryAccountEntryCount() console.log('accountEntriesCount', accountEntriesCount) - await closeDatabase() + await Storage.closeDatabase() } start() diff --git a/scripts/accounthistorystate_creator.ts b/scripts/accounthistorystate_creator.ts index 4ccca37..c9ba370 100644 --- a/scripts/accounthistorystate_creator.ts +++ b/scripts/accounthistorystate_creator.ts @@ -1,7 +1,6 @@ import * as Storage from '../src/storage' import * as ReceiptDB from '../src/storage/receipt' import * as AccountHistoryStateDB from '../src/storage/accountHistoryState' -import { closeDatabase } from '../src/storage/sqlite3storage' const start = async (): Promise => { await Storage.initializeDB() @@ -60,7 +59,7 @@ const start = async (): Promise => { } const accountHistoryStateCount = await AccountHistoryStateDB.queryAccountHistoryStateCount() console.log('accountHistoryStateCount', accountHistoryStateCount) - await closeDatabase() + await Storage.closeDatabase() } start() diff --git a/scripts/data_patcher.ts b/scripts/data_patcher.ts index 627a633..fb220e8 100644 --- a/scripts/data_patcher.ts +++ b/scripts/data_patcher.ts @@ -4,7 +4,6 @@ import * as Crypto from '../src/utils/crypto' import * as Storage from '../src/storage' import * as DataSync from '../src/class/DataSync' import { config, overrideDefaultConfig } from '../src/config' -import { closeDatabase } from '../src/storage/sqlite3storage' let startCycle = 0 let endCycle = 0 @@ -43,7 +42,7 @@ const start = async (): Promise => { await DataSync.downloadOriginalTxsDataBetweenCycles(startCycle, endCycle, patchOnlyMissingData) console.log('OriginalTxs Patched!') - await closeDatabase() + await Storage.closeDatabase() console.log('Patching done! from cycle', startCycle, 'to cycle', endCycle) } diff --git a/scripts/query-perf-checker/block.ts b/scripts/query-perf-checker/block.ts index e2f6278..30d98a6 100644 --- a/scripts/query-perf-checker/block.ts +++ b/scripts/query-perf-checker/block.ts @@ -1,7 +1,6 @@ import * as crypto from '@shardus/crypto-utils' import * as Storage from '../../src/storage' import * as Block from '../../src/storage/block' -import { closeDatabase } from '../../src/storage/sqlite3storage' crypto.init('69fa4195670576c0160d660c3be36556ff8d504725be8a59b5a96509e0c994bc') @@ -35,7 +34,7 @@ const start = async (): Promise => { end_time = process.hrtime(start_time) console.log('End Time', end_time) console.log('Time in millisecond is: ', end_time[0] * 1000 + end_time[1] / 1000000) - await closeDatabase() + await Storage.closeDatabase() } start() diff --git a/scripts/query-perf-checker/transaction.ts b/scripts/query-perf-checker/transaction.ts index 1c9b7ad..d3e29f8 100644 --- a/scripts/query-perf-checker/transaction.ts +++ b/scripts/query-perf-checker/transaction.ts @@ -5,7 +5,6 @@ import * as Transaction from '../../src/storage/transaction' import * as Account from '../../src/storage/account' import * as Block from '../../src/storage/block' import { TransactionSearchType } from '../../src/types' -import { closeDatabase } from '../../src/storage/sqlite3storage' crypto.init('69fa4195670576c0160d660c3be36556ff8d504725be8a59b5a96509e0c994bc') @@ -244,7 +243,7 @@ const start = async (): Promise => { // console.error(error) // }) - await closeDatabase() + await Storage.closeDatabase() } start() diff --git a/scripts/repair_account_state.ts b/scripts/repair_account_state.ts index f30331a..a9c0815 100644 --- a/scripts/repair_account_state.ts +++ b/scripts/repair_account_state.ts @@ -9,7 +9,6 @@ import { config, overrideDefaultConfig } from '../src/config' import { Account, AccountCopy, AccountSearchType, AccountType } from '../src/types' import { bytesToHex } from '@ethereumjs/util' import { getContractInfo } from '../src/class/TxDecoder' -import { closeDatabase } from '../src/storage/sqlite3storage' const patchAccountData = false @@ -60,7 +59,8 @@ const start = async (): Promise => { } } } - await closeDatabase() + + await Storage.closeDatabase() console.log('Finish verifying accounts state data!') } diff --git a/scripts/repair_missing_cycle_block.ts b/scripts/repair_missing_cycle_block.ts index 862b1d4..f7726dc 100644 --- a/scripts/repair_missing_cycle_block.ts +++ b/scripts/repair_missing_cycle_block.ts @@ -7,7 +7,6 @@ import * as db from '../src/storage/sqlite3storage' import * as CycleDB from '../src/storage/cycle' import * as BlockDB from '../src/storage/block' import { config, overrideDefaultConfig } from '../src/config' -import { closeDatabase } from '../src/storage/sqlite3storage' const patchCycleData = false const patchBlockData = false @@ -48,7 +47,7 @@ const start = async (): Promise => { } await checkBlockData(0, lastStoredBlock[0].number) console.log('Block data check complete.') - await closeDatabase() + await Storage.closeDatabase() } /** diff --git a/src/collector.ts b/src/collector.ts index 1bd5982..3cb35fb 100644 --- a/src/collector.ts +++ b/src/collector.ts @@ -19,7 +19,6 @@ import { queryFromDistributor, DataType, } from './class/DataSync' -import { closeDatabase } from './storage/sqlite3storage' import { validateData } from './class/validateData' import { DistributorSocketCloseCodes } from './types' import { initDataLogWriter } from './class/DataLogWriter' diff --git a/src/storage/index.ts b/src/storage/index.ts index 3c18b96..ec4fd76 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -118,17 +118,21 @@ export const initializeDB = async (): Promise => { ) } +export const closeDatabase = async (): Promise => { + await db.close() +} + export const addExitListeners = (ws?: WebSocket) => { process.on('SIGINT', async () => { console.log('Exiting on SIGINT') if (ws) ws.close() - await db.closeDatabase() + await closeDatabase() process.exit(0) }) process.on('SIGTERM', async () => { console.log('Exiting on SIGTERM') if (ws) ws.close() - await db.closeDatabase() + await closeDatabase() process.exit(0) }) } diff --git a/src/storage/sqlite3storage.ts b/src/storage/sqlite3storage.ts index 0190e1c..a62925d 100644 --- a/src/storage/sqlite3storage.ts +++ b/src/storage/sqlite3storage.ts @@ -14,43 +14,6 @@ export interface DbOptions { shardeumIndexerSqlitePath: string } -/** - * Closes the Database and Indexer Connections Gracefully - */ -export async function closeDatabase(): Promise { - try { - console.log('Terminating Database/Indexer Connections...') - await new Promise((resolve, reject) => { - db.close((err) => { - if (err) { - console.error('Error closing Database Connection.') - reject(err) - } else { - console.log('Database connection closed.') - resolve() - } - }) - }) - - if (config.enableShardeumIndexer && shardeumIndexerDb) { - await new Promise((resolve, reject) => { - shardeumIndexerDb.close((err) => { - if (err) { - console.error('Error closing Indexer Connection.') - reject(err) - } else { - console.log('Shardeum Indexer Database Connection closed.') - resolve() - } - }) - }) - } - } catch (err) { - console.error('Error thrown in closeDatabase() function: ') - console.error(err) - } -} - export async function init(config: DbOptions): Promise { db = new sqlite3.Database(config.defaultDbSqlitePath) await run('PRAGMA journal_mode=WAL') @@ -136,6 +99,43 @@ export async function all( }) } +/** + * Closes the Database and Indexer Connections Gracefully + */ +export async function close(): Promise { + try { + console.log('Terminating Database/Indexer Connections...') + await new Promise((resolve, reject) => { + db.close((err) => { + if (err) { + console.error('Error closing Database Connection.') + reject(err) + } else { + console.log('Database connection closed.') + resolve() + } + }) + }) + + if (config.enableShardeumIndexer && shardeumIndexerDb) { + await new Promise((resolve, reject) => { + shardeumIndexerDb.close((err) => { + if (err) { + console.error('Error closing Indexer Connection.') + reject(err) + } else { + console.log('Shardeum Indexer Database Connection closed.') + resolve() + } + }) + }) + } + } catch (err) { + console.error('Error thrown in db close() function: ') + console.error(err) + } +} + export function extractValues(object: object): string[] { try { const inputs: string[] = []