Миграция "jembadb" => "^2.3.0"

This commit is contained in:
Book Pauk
2022-03-29 15:49:48 +07:00
parent a349d8af68
commit 02d458d192
5 changed files with 24 additions and 29 deletions

View File

@@ -1,27 +1,26 @@
let instance = null;
const defaultTimeout = 15*1000;//15 sec
const exitSignals = ['SIGINT', 'SIGTERM', 'SIGBREAK', 'SIGHUP', 'uncaughtException', 'SIGUSR2'];
const exitSignals = ['SIGINT', 'SIGTERM', 'SIGBREAK', 'SIGHUP', 'uncaughtException'];
//singleton
class AsyncExit {
constructor() {
constructor(signals = exitSignals, codeOnSignal = 2) {
if (!instance) {
this.onSignalCallbacks = new Map();
this.callbacks = new Map();
this.afterCallbacks = new Map();
this.exitTimeout = defaultTimeout;
this.inited = false;
this._init(signals, codeOnSignal);
instance = this;
}
return instance;
}
init(signals = exitSignals, codeOnSignal = 2) {
if (this.inited)
throw new Error('AsyncExit: initialized already');
_init(signals, codeOnSignal) {
const runSingalCallbacks = async(signal) => {
for (const signalCallback of this.onSignalCallbacks.keys()) {
try {
@@ -38,8 +37,6 @@ class AsyncExit {
this.exit(codeOnSignal);
});
}
this.inited = true;
}
onSignal(signalCallback) {

View File

@@ -14,6 +14,7 @@ class JembaConnManager {
constructor() {
if (!instance) {
this.inited = false;
this._db = {};
instance = this;
}
@@ -28,6 +29,8 @@ class JembaConnManager {
this.config = config;
this._db = {};
ayncExit.add(this.close.bind(this));
for (const dbConfig of this.config.jembaDb) {
const dbPath = `${this.config.dataDir}/db/${dbConfig.dbName}`;
@@ -44,21 +47,23 @@ class JembaConnManager {
} else {
dbConn = new JembaDb();
}
this._db[dbConfig.dbName] = dbConn;
log(`Open "${dbConfig.dbName}" begin`);
await dbConn.lock({
dbPath,
create: true,
softLock: true,
tableDefaults: {
cacheSize: dbConfig.cacheSize,
compressed: dbConfig.compressed,
forceFileClosing: dbConfig.forceFileClosing
forceFileClosing: dbConfig.forceFileClosing,
typeCompatMode: true,
},
});
if (dbConfig.openAll) {
if (dbConfig.openAll || forceAutoRepair || dbConfig.autoRepair) {
try {
await dbConn.openAll();
} catch(e) {
@@ -87,21 +92,15 @@ class JembaConnManager {
if (applied.length)
log(`${applied.length} migrations applied to "${dbConfig.dbName}"`);
}
this._db[dbConfig.dbName] = dbConn;
}
ayncExit.add(this.close.bind(this));
this.inited = true;
}
async close() {
if (!this.inited)
return;
for (const dbConfig of this.config.jembaDb) {
await this._db[dbConfig.dbName].unlock();
if (this._db[dbConfig.dbName])
await this._db[dbConfig.dbName].unlock();
}
this._db = {};

View File

@@ -8,7 +8,6 @@ const http = require('http');
const WebSocket = require ('ws');
const ayncExit = new (require('./core/AsyncExit'))();
ayncExit.init();
let log = null;