Commit 3c910601 authored by Cédric Moreau's avatar Cédric Moreau

[enh] dump-ww: use file copy + semaphore to trigger the copy

parent 14074533
Pipeline #5086 failed with stages
in 12 minutes and 43 seconds
......@@ -123,6 +123,7 @@ export const Directory = {
INSTANCE_NAME: getDomain(opts.mdb),
INSTANCE_HOME: getHomePath(opts.mdb, opts.home),
GET_FILE_PATH: (fileSubPath: string, home = '') => path.join(home || Directory.INSTANCE_HOME, fileSubPath),
INSTANCE_HOMELOG_FILE: getLogsPath(opts.mdb, opts.home),
DUNITER_DB_NAME: 'duniter',
LOKI_DB_DIR: 'loki',
......@@ -136,7 +137,7 @@ export const Directory = {
return new SQLiteDriver(':memory:')
}
// Or file
const sqlitePath = path.join(home || Directory.INSTANCE_HOME, dbName)
const sqlitePath = Directory.GET_FILE_PATH(dbName, home)
return new SQLiteDriver(sqlitePath)
},
......
export const WotWizardConstants = {
DB_NAME_0: 'wotwizard-export_0.db',
DB_NAME: 'wotwizard-export.db',
FILE_UPDATING: 'updating',
BLOCKS_SAVE_BATCH_SIZE: 10,
DELAY_FOR_UPDATING: 15 * 1000, // in milliseconds
}
\ No newline at end of file
import * as fs from "fs"
import {Server} from "../../../../server"
import {createExportStructure} from "./wotwizard.init.structure"
import {WotWizardConstants} from "./wotwizard.constants"
......@@ -5,11 +6,12 @@ import {addLegacyBlocks} from "./wotwizard.legacy.blocks"
import {addNewBlocks} from "./wotwizard.new.blocks"
import {deleteNonLegacy} from "./wotwizard.delete"
import {copyMemPool} from "./wotwizard.copy.mempool"
import {Directory} from "../../../lib/system/directory"
export async function dumpWotWizard(server: Server) {
// 1. Create dump structure if it does not exist
const wwDAL = await createExportStructure(WotWizardConstants.DB_NAME)
const wwDAL = await createExportStructure(WotWizardConstants.DB_NAME_0)
// 2. Integrate legacy blocks (= non-forkable)
await addLegacyBlocks(server, wwDAL)
......@@ -22,4 +24,28 @@ export async function dumpWotWizard(server: Server) {
// 5. Copy mempool
await copyMemPool(server, wwDAL)
// 6. Close SQL connections
await Promise.all([
wwDAL.blockDao,
wwDAL.iindexDao,
wwDAL.idtyDao,
wwDAL.certDao,
wwDAL.msDao,
].map(dao => dao.close()))
// 7. Copy
let lastCopyIsOldEnough = false
const updatingFile = Directory.GET_FILE_PATH(WotWizardConstants.FILE_UPDATING)
if (fs.existsSync(updatingFile)) {
const content = parseInt(fs.readFileSync(updatingFile, 'utf8'))
lastCopyIsOldEnough = Date.now() - content > WotWizardConstants.DELAY_FOR_UPDATING
} else {
// Never done
lastCopyIsOldEnough = true
}
if (lastCopyIsOldEnough) {
fs.copyFileSync(Directory.GET_FILE_PATH(WotWizardConstants.DB_NAME_0), Directory.GET_FILE_PATH(WotWizardConstants.DB_NAME))
fs.writeFileSync(updatingFile, Date.now())
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment