Skip to content

Commit

Permalink
Update closeDatabase function
Browse files Browse the repository at this point in the history
  • Loading branch information
jairajdev authored and afostr committed Apr 10, 2024
1 parent 1b48219 commit 64341c0
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 54 deletions.
3 changes: 1 addition & 2 deletions scripts/accountentry_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
await Storage.initializeDB()
Expand All @@ -18,7 +17,7 @@ const start = async (): Promise<void> => {
}
const accountEntriesCount = await AccountEntryDB.queryAccountEntryCount()
console.log('accountEntriesCount', accountEntriesCount)
await closeDatabase()
await Storage.closeDatabase()
}

start()
3 changes: 1 addition & 2 deletions scripts/accounthistorystate_creator.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
await Storage.initializeDB()
Expand Down Expand Up @@ -60,7 +59,7 @@ const start = async (): Promise<void> => {
}
const accountHistoryStateCount = await AccountHistoryStateDB.queryAccountHistoryStateCount()
console.log('accountHistoryStateCount', accountHistoryStateCount)
await closeDatabase()
await Storage.closeDatabase()
}

start()
3 changes: 1 addition & 2 deletions scripts/data_patcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +42,7 @@ const start = async (): Promise<void> => {
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)
}

Expand Down
3 changes: 1 addition & 2 deletions scripts/query-perf-checker/block.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -35,7 +34,7 @@ const start = async (): Promise<void> => {
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()
3 changes: 1 addition & 2 deletions scripts/query-perf-checker/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -244,7 +243,7 @@ const start = async (): Promise<void> => {
// console.error(error)
// })

await closeDatabase()
await Storage.closeDatabase()
}

start()
4 changes: 2 additions & 2 deletions scripts/repair_account_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -60,7 +59,8 @@ const start = async (): Promise<void> => {
}
}
}
await closeDatabase()

await Storage.closeDatabase()
console.log('Finish verifying accounts state data!')
}

Expand Down
3 changes: 1 addition & 2 deletions scripts/repair_missing_cycle_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,7 +47,7 @@ const start = async (): Promise<void> => {
}
await checkBlockData(0, lastStoredBlock[0].number)
console.log('Block data check complete.')
await closeDatabase()
await Storage.closeDatabase()
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 6 additions & 2 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,21 @@ export const initializeDB = async (): Promise<void> => {
)
}

export const closeDatabase = async (): Promise<void> => {
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)
})
}
74 changes: 37 additions & 37 deletions src/storage/sqlite3storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,6 @@ export interface DbOptions {
shardeumIndexerSqlitePath: string
}

/**
* Closes the Database and Indexer Connections Gracefully
*/
export async function closeDatabase(): Promise<void> {
try {
console.log('Terminating Database/Indexer Connections...')
await new Promise<void>((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<void>((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<void> {
db = new sqlite3.Database(config.defaultDbSqlitePath)
await run('PRAGMA journal_mode=WAL')
Expand Down Expand Up @@ -136,6 +99,43 @@ export async function all<T>(
})
}

/**
* Closes the Database and Indexer Connections Gracefully
*/
export async function close(): Promise<void> {
try {
console.log('Terminating Database/Indexer Connections...')
await new Promise<void>((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<void>((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[] = []
Expand Down

0 comments on commit 64341c0

Please sign in to comment.