diff --git a/app/lib/system/directory.ts b/app/lib/system/directory.ts index c0aa31108bbd8432a1ece9c9cc43e67dd4aa41f5..0e17edb27736d6949e1bcc9e6bd2b778e5766d3a 100644 --- a/app/lib/system/directory.ts +++ b/app/lib/system/directory.ts @@ -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) }, diff --git a/app/modules/dump/wotwizard/wotwizard.constants.ts b/app/modules/dump/wotwizard/wotwizard.constants.ts index 6019e22a9a7ca417357b38747879d21b79dc63d4..97f98be1a39c1518c201b8a33b86ac84a3366d82 100644 --- a/app/modules/dump/wotwizard/wotwizard.constants.ts +++ b/app/modules/dump/wotwizard/wotwizard.constants.ts @@ -1,5 +1,8 @@ export const WotWizardConstants = { + DB_NAME_0: 'wotwizard-export_0.db', DB_NAME: 'wotwizard-export.db', + FILE_UPDATING: 'updating.txt', BLOCKS_SAVE_BATCH_SIZE: 10, + DELAY_FOR_UPDATING: 15 * 1000, // in milliseconds } \ No newline at end of file diff --git a/app/modules/dump/wotwizard/wotwizard.dump.ts b/app/modules/dump/wotwizard/wotwizard.dump.ts index 64628788e19b32fc29c5830be9c7e4365a5dd329..cf619fecc8b9fd06277123d8e47c1241c25146b5 100644 --- a/app/modules/dump/wotwizard/wotwizard.dump.ts +++ b/app/modules/dump/wotwizard/wotwizard.dump.ts @@ -1,3 +1,4 @@ +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()) + } }