WebWorker.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. const config = this.config;
  52. if (await fs.pathExists(dbPath))
  53. throw new Error(`createDb.pathExists: ${dbPath}`);
  54. const db = new JembaDbThread();
  55. await db.lock({
  56. dbPath,
  57. create: true,
  58. softLock: true,
  59. tableDefaults: {
  60. cacheSize: 5,
  61. },
  62. });
  63. try {
  64. const dbCreator = new DbCreator(config);
  65. await dbCreator.run(db, (state) => this.setMyState(ssDbCreating, state));
  66. } finally {
  67. await db.unlock();
  68. }
  69. }
  70. async loadOrCreateDb() {
  71. this.setMyState(ssDbLoading);
  72. try {
  73. const config = this.config;
  74. const dbPath = `${config.dataDir}/db`;
  75. //пересоздаем БД из INPX если нужно
  76. if (config.recreateDb)
  77. await fs.remove(dbPath);
  78. if (!await fs.pathExists(dbPath)) {
  79. await this.createDb(dbPath);
  80. }
  81. //загружаем БД
  82. this.setMyState(ssDbLoading);
  83. this.db = new JembaDbThread();
  84. await this.db.lock({
  85. dbPath,
  86. softLock: true,
  87. tableDefaults: {
  88. cacheSize: 5,
  89. },
  90. });
  91. //открываем все таблицы
  92. await this.db.openAll();
  93. } catch (e) {
  94. log(LM_FATAL, e.message);
  95. ayncExit.exit(1);
  96. } finally {
  97. this.setMyState(ssNormal);
  98. }
  99. }
  100. }
  101. module.exports = WebWorker;