Skip to content
Snippets Groups Projects
Commit 3c910601 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

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

parent 14074533
Branches
Tags
1 merge request!1278WotWizard compliance with Duniter 1.7
...@@ -123,6 +123,7 @@ export const Directory = { ...@@ -123,6 +123,7 @@ export const Directory = {
INSTANCE_NAME: getDomain(opts.mdb), INSTANCE_NAME: getDomain(opts.mdb),
INSTANCE_HOME: getHomePath(opts.mdb, opts.home), 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), INSTANCE_HOMELOG_FILE: getLogsPath(opts.mdb, opts.home),
DUNITER_DB_NAME: 'duniter', DUNITER_DB_NAME: 'duniter',
LOKI_DB_DIR: 'loki', LOKI_DB_DIR: 'loki',
...@@ -136,7 +137,7 @@ export const Directory = { ...@@ -136,7 +137,7 @@ export const Directory = {
return new SQLiteDriver(':memory:') return new SQLiteDriver(':memory:')
} }
// Or file // Or file
const sqlitePath = path.join(home || Directory.INSTANCE_HOME, dbName) const sqlitePath = Directory.GET_FILE_PATH(dbName, home)
return new SQLiteDriver(sqlitePath) return new SQLiteDriver(sqlitePath)
}, },
......
export const WotWizardConstants = { export const WotWizardConstants = {
DB_NAME_0: 'wotwizard-export_0.db',
DB_NAME: 'wotwizard-export.db', DB_NAME: 'wotwizard-export.db',
FILE_UPDATING: 'updating',
BLOCKS_SAVE_BATCH_SIZE: 10, 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 {Server} from "../../../../server"
import {createExportStructure} from "./wotwizard.init.structure" import {createExportStructure} from "./wotwizard.init.structure"
import {WotWizardConstants} from "./wotwizard.constants" import {WotWizardConstants} from "./wotwizard.constants"
...@@ -5,11 +6,12 @@ import {addLegacyBlocks} from "./wotwizard.legacy.blocks" ...@@ -5,11 +6,12 @@ import {addLegacyBlocks} from "./wotwizard.legacy.blocks"
import {addNewBlocks} from "./wotwizard.new.blocks" import {addNewBlocks} from "./wotwizard.new.blocks"
import {deleteNonLegacy} from "./wotwizard.delete" import {deleteNonLegacy} from "./wotwizard.delete"
import {copyMemPool} from "./wotwizard.copy.mempool" import {copyMemPool} from "./wotwizard.copy.mempool"
import {Directory} from "../../../lib/system/directory"
export async function dumpWotWizard(server: Server) { export async function dumpWotWizard(server: Server) {
// 1. Create dump structure if it does not exist // 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) // 2. Integrate legacy blocks (= non-forkable)
await addLegacyBlocks(server, wwDAL) await addLegacyBlocks(server, wwDAL)
...@@ -22,4 +24,28 @@ export async function dumpWotWizard(server: Server) { ...@@ -22,4 +24,28 @@ export async function dumpWotWizard(server: Server) {
// 5. Copy mempool // 5. Copy mempool
await copyMemPool(server, wwDAL) 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())
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment