Работа над DbCreator и WebWorker
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
const InpxParser =
|
||||||
|
|
||||||
|
class DbCreator {
|
||||||
|
constructor(config) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(db, callback) {
|
||||||
|
const config = this.config;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = DbCreator;
|
||||||
@@ -1,8 +1,22 @@
|
|||||||
const WorkerState = require('./WorkerState');
|
const fs = require('fs-extra');
|
||||||
|
|
||||||
//server states:
|
const WorkerState = require('./WorkerState');
|
||||||
|
const { JembaDbThread } = require('jembadb');
|
||||||
|
const DbCreator = require('./DbCreator');
|
||||||
|
|
||||||
|
const ayncExit = new (require('./AsyncExit'))();
|
||||||
|
const log = new (require('./AppLogger'))().log;//singleton
|
||||||
|
|
||||||
|
//server states
|
||||||
const ssNormal = 'normal';
|
const ssNormal = 'normal';
|
||||||
const ssDbLoading = 'db_loading';
|
const ssDbLoading = 'db_loading';
|
||||||
|
const ssDbCreating = 'db_creating';
|
||||||
|
|
||||||
|
const stateToText = {
|
||||||
|
[ssNormal]: '',
|
||||||
|
[ssDbLoading]: 'Загрузка поисковой базы',
|
||||||
|
[ssDbCreating]: 'Создание поисковой базы',
|
||||||
|
};
|
||||||
|
|
||||||
//singleton
|
//singleton
|
||||||
let instance = null;
|
let instance = null;
|
||||||
@@ -15,6 +29,9 @@ class WebWorker {
|
|||||||
|
|
||||||
this.wState = this.workerState.getControl('server_state');
|
this.wState = this.workerState.getControl('server_state');
|
||||||
this.myState = '';
|
this.myState = '';
|
||||||
|
this.db = null;
|
||||||
|
|
||||||
|
ayncExit.add(this.closeDb.bind(this));
|
||||||
|
|
||||||
this.loadOrCreateDb();//no await
|
this.loadOrCreateDb();//no await
|
||||||
|
|
||||||
@@ -29,18 +46,81 @@ class WebWorker {
|
|||||||
throw new Error('server_busy');
|
throw new Error('server_busy');
|
||||||
}
|
}
|
||||||
|
|
||||||
setMyState(newState) {
|
setMyState(newState, workerState = {}) {
|
||||||
this.myState = newState;
|
this.myState = newState;
|
||||||
this.wState.set({state: newState});
|
this.wState.set(Object.assign({}, workerState, {
|
||||||
|
state: newState,
|
||||||
|
serverMessage: stateToText[newState]
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async closeDb() {
|
||||||
|
if (this.db) {
|
||||||
|
await this.db.unlock();
|
||||||
|
this.db = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async createDb(dbPath) {
|
||||||
|
this.setMyState(ssDbCreating);
|
||||||
|
|
||||||
|
const config = this.config;
|
||||||
|
|
||||||
|
if (await fs.pathExists(dbPath))
|
||||||
|
throw new Error(`createDb.pathExists: ${dbPath}`);
|
||||||
|
|
||||||
|
const db = new JembaDbThread();
|
||||||
|
await db.lock({
|
||||||
|
dbPath,
|
||||||
|
create: true,
|
||||||
|
softLock: true,
|
||||||
|
|
||||||
|
tableDefaults: {
|
||||||
|
cacheSize: 5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dbCreator = new DbCreator(config);
|
||||||
|
await dbCreator.run(db, (state) => this.setMyState(ssDbCreating, state));
|
||||||
|
} finally {
|
||||||
|
await db.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadOrCreateDb() {
|
async loadOrCreateDb() {
|
||||||
this.setMyState(ssDbLoading);
|
this.setMyState(ssDbLoading);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//
|
const config = this.config;
|
||||||
|
const dbPath = `${config.dataDir}/db`;
|
||||||
|
|
||||||
|
//пересоздаем БД из INPX если нужно
|
||||||
|
if (config.recreateDb)
|
||||||
|
await fs.remove(dbPath);
|
||||||
|
|
||||||
|
if (!await fs.pathExists(dbPath)) {
|
||||||
|
await this.createDb(dbPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
//загружаем БД
|
||||||
|
this.setMyState(ssDbLoading);
|
||||||
|
|
||||||
|
this.db = new JembaDbThread();
|
||||||
|
await this.db.lock({
|
||||||
|
dbPath,
|
||||||
|
softLock: true,
|
||||||
|
|
||||||
|
tableDefaults: {
|
||||||
|
cacheSize: 5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
//открываем все таблицы
|
||||||
|
await this.db.openAll();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//
|
log(LM_FATAL, e.message);
|
||||||
|
ayncExit.exit(1);
|
||||||
} finally {
|
} finally {
|
||||||
this.setMyState(ssNormal);
|
this.setMyState(ssNormal);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user