WebWorker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const fs = require('fs-extra');
  2. const WorkerState = require('./WorkerState');
  3. const { JembaDbThread } = require('jembadb');
  4. const DbCreator = require('./DbCreator');
  5. const ayncExit = new (require('./AsyncExit'))();
  6. const log = new (require('./AppLogger'))().log;//singleton
  7. //server states
  8. const ssNormal = 'normal';
  9. const ssDbLoading = 'db_loading';
  10. const ssDbCreating = 'db_creating';
  11. const stateToText = {
  12. [ssNormal]: '',
  13. [ssDbLoading]: 'Загрузка поисковой базы',
  14. [ssDbCreating]: 'Создание поисковой базы',
  15. };
  16. //singleton
  17. let instance = null;
  18. class WebWorker {
  19. constructor(config) {
  20. if (!instance) {
  21. this.config = config;
  22. this.workerState = new WorkerState();
  23. this.wState = this.workerState.getControl('server_state');
  24. this.myState = '';
  25. this.db = null;
  26. ayncExit.add(this.closeDb.bind(this));
  27. this.loadOrCreateDb();//no await
  28. instance = this;
  29. }
  30. return instance;
  31. }
  32. checkMyState() {
  33. if (this.myState != ssNormal)
  34. throw new Error('server_busy');
  35. }
  36. setMyState(newState, workerState = {}) {
  37. this.myState = newState;
  38. this.wState.set(Object.assign({}, workerState, {
  39. state: newState,
  40. serverMessage: stateToText[newState]
  41. }));
  42. }
  43. async closeDb() {
  44. if (this.db) {
  45. await this.db.unlock();
  46. this.db = null;
  47. }
  48. }
  49. async createDb(dbPath) {
  50. this.setMyState(ssDbCreating);
  51. log('Searcher DB create start');
  52. const config = this.config;
  53. if (await fs.pathExists(dbPath))
  54. throw new Error(`createDb.pathExists: ${dbPath}`);
  55. const db = new JembaDbThread();
  56. await db.lock({
  57. dbPath,
  58. create: true,
  59. softLock: true,
  60. tableDefaults: {
  61. cacheSize: 5,
  62. },
  63. });
  64. try {
  65. log(' start INPX import');
  66. const dbCreator = new DbCreator(config);
  67. await dbCreator.run(db, (state) => {
  68. this.setMyState(ssDbCreating, state);
  69. if (state.fileName)
  70. log(` load ${state.fileName}`);
  71. if (state.recsLoaded)
  72. log(` processed ${state.recsLoaded} records`);
  73. });
  74. log(' finish INPX import');
  75. } finally {
  76. await db.unlock();
  77. log('Searcher DB successfully created');
  78. }
  79. }
  80. async loadOrCreateDb() {
  81. this.setMyState(ssDbLoading);
  82. try {
  83. const config = this.config;
  84. const dbPath = `${config.dataDir}/db`;
  85. //пересоздаем БД из INPX если нужно
  86. if (config.recreateDb)
  87. await fs.remove(dbPath);
  88. if (!await fs.pathExists(dbPath)) {
  89. await this.createDb(dbPath);
  90. }
  91. //загружаем БД
  92. this.setMyState(ssDbLoading);
  93. log('Searcher DB open');
  94. this.db = new JembaDbThread();
  95. await this.db.lock({
  96. dbPath,
  97. softLock: true,
  98. tableDefaults: {
  99. cacheSize: 5,
  100. },
  101. });
  102. //открываем все таблицы
  103. await this.db.openAll();
  104. log('Searcher DB is ready');
  105. } catch (e) {
  106. log(LM_FATAL, e.message);
  107. ayncExit.exit(1);
  108. } finally {
  109. this.setMyState(ssNormal);
  110. }
  111. }
  112. }
  113. module.exports = WebWorker;