From 3c910601e7c27b2088fdf058ab179db1ab15b6cb Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Wed, 3 Apr 2019 13:17:00 +0200 Subject: [PATCH 1/2] =?UTF-8?q?[enh]=C2=A0dump-ww:=20use=20file=20copy=20+?= =?UTF-8?q?=20semaphore=20to=20trigger=20the=20copy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/system/directory.ts | 3 +- .../dump/wotwizard/wotwizard.constants.ts | 3 ++ app/modules/dump/wotwizard/wotwizard.dump.ts | 28 ++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/lib/system/directory.ts b/app/lib/system/directory.ts index c0aa31108..0e17edb27 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 6019e22a9..dbc5db2cb 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', 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 64628788e..cf619fecc 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()) + } } -- GitLab From 0017e1405d50ca4bed0f6d3d6ed38119f41da769 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Fri, 5 Apr 2019 12:51:25 +0200 Subject: [PATCH 2/2] [fix] WW needs an extension for a file --- app/modules/dump/wotwizard/wotwizard.constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/modules/dump/wotwizard/wotwizard.constants.ts b/app/modules/dump/wotwizard/wotwizard.constants.ts index dbc5db2cb..97f98be1a 100644 --- a/app/modules/dump/wotwizard/wotwizard.constants.ts +++ b/app/modules/dump/wotwizard/wotwizard.constants.ts @@ -2,7 +2,7 @@ export const WotWizardConstants = { DB_NAME_0: 'wotwizard-export_0.db', DB_NAME: 'wotwizard-export.db', - FILE_UPDATING: 'updating', + FILE_UPDATING: 'updating.txt', BLOCKS_SAVE_BATCH_SIZE: 10, DELAY_FOR_UPDATING: 15 * 1000, // in milliseconds } \ No newline at end of file -- GitLab