WebWorker.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. const os = require('os');
  2. const path = require('path');
  3. const fs = require('fs-extra');
  4. const _ = require('lodash');
  5. const ZipReader = require('./ZipReader');
  6. const WorkerState = require('./WorkerState');//singleton
  7. const { JembaDb, JembaDbThread } = require('jembadb');
  8. const DbCreator = require('./DbCreator');
  9. const DbSearcher = require('./DbSearcher');
  10. const InpxHashCreator = require('./InpxHashCreator');
  11. const RemoteLib = require('./RemoteLib');//singleton
  12. const asyncExit = new (require('./AsyncExit'))();
  13. const log = new (require('./AppLogger'))().log;//singleton
  14. const utils = require('./utils');
  15. const genreTree = require('./genres');
  16. const Fb2Helper = require('./fb2/Fb2Helper');
  17. //server states
  18. const ssNormal = 'normal';
  19. const ssDbLoading = 'db_loading';
  20. const ssDbCreating = 'db_creating';
  21. const stateToText = {
  22. [ssNormal]: '',
  23. [ssDbLoading]: 'Загрузка поисковой базы',
  24. [ssDbCreating]: 'Создание поисковой базы',
  25. };
  26. const cleanDirPeriod = 60*60*1000;//каждый час
  27. //singleton
  28. let instance = null;
  29. class WebWorker {
  30. constructor(config) {
  31. if (!instance) {
  32. this.config = config;
  33. this.workerState = new WorkerState();
  34. this.remoteLib = null;
  35. if (config.remoteLib) {
  36. this.remoteLib = new RemoteLib(config);
  37. }
  38. this.inpxHashCreator = new InpxHashCreator(config);
  39. this.fb2Helper = new Fb2Helper();
  40. this.inpxFileHash = '';
  41. this.wState = this.workerState.getControl('server_state');
  42. this.myState = '';
  43. this.db = null;
  44. this.dbSearcher = null;
  45. asyncExit.add(this.closeDb.bind(this));
  46. this.loadOrCreateDb();//no await
  47. this.periodicLogServerStats();//no await
  48. const dirConfig = [
  49. {
  50. dir: config.bookDir,
  51. maxSize: config.maxFilesDirSize,
  52. },
  53. ];
  54. this.periodicCleanDir(dirConfig);//no await
  55. this.periodicCheckInpx();//no await
  56. instance = this;
  57. }
  58. return instance;
  59. }
  60. checkMyState() {
  61. if (this.myState != ssNormal)
  62. throw new Error('server_busy');
  63. }
  64. setMyState(newState, workerState = {}) {
  65. this.myState = newState;
  66. this.wState.set(Object.assign({}, workerState, {
  67. state: newState,
  68. serverMessage: stateToText[newState]
  69. }));
  70. }
  71. async closeDb() {
  72. if (this.db) {
  73. await this.db.unlock();
  74. this.db = null;
  75. }
  76. }
  77. async createDb(dbPath) {
  78. this.setMyState(ssDbCreating);
  79. log('Searcher DB create start');
  80. const config = this.config;
  81. if (await fs.pathExists(dbPath))
  82. throw new Error(`createDb.pathExists: ${dbPath}`);
  83. const db = new JembaDbThread();
  84. await db.lock({
  85. dbPath,
  86. create: true,
  87. softLock: true,
  88. tableDefaults: {
  89. cacheSize: config.dbCacheSize,
  90. },
  91. });
  92. try {
  93. const dbCreator = new DbCreator(config);
  94. await dbCreator.run(db, (state) => {
  95. this.setMyState(ssDbCreating, state);
  96. if (state.fileName)
  97. log(` load ${state.fileName}`);
  98. if (state.recsLoaded)
  99. log(` processed ${state.recsLoaded} records`);
  100. if (state.job)
  101. log(` ${state.job}`);
  102. });
  103. log('Searcher DB successfully created');
  104. } finally {
  105. await db.unlock();
  106. }
  107. }
  108. async loadOrCreateDb(recreate = false, iteration = 0) {
  109. this.setMyState(ssDbLoading);
  110. try {
  111. const config = this.config;
  112. const dbPath = `${config.dataDir}/db`;
  113. this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash();
  114. //проверим полный InxpHash (включая фильтр и версию БД)
  115. //для этого заглянем в конфиг внутри БД, если он есть
  116. if (!(config.recreateDb || recreate) && await fs.pathExists(dbPath)) {
  117. const newInpxHash = await this.inpxHashCreator.getHash();
  118. const tmpDb = new JembaDb();
  119. await tmpDb.lock({dbPath, softLock: true});
  120. try {
  121. await tmpDb.open({table: 'config'});
  122. const rows = await tmpDb.select({table: 'config', where: `@@id('inpxHash')`});
  123. if (!rows.length || newInpxHash !== rows[0].value)
  124. throw new Error('inpx file: changes found on start, recreating DB');
  125. } catch (e) {
  126. log(LM_WARN, e.message);
  127. recreate = true;
  128. } finally {
  129. await tmpDb.unlock();
  130. }
  131. }
  132. //удалим БД если нужно
  133. if (config.recreateDb || recreate)
  134. await fs.remove(dbPath);
  135. //пересоздаем БД из INPX если нужно
  136. if (!await fs.pathExists(dbPath)) {
  137. await this.createDb(dbPath);
  138. utils.freeMemory();
  139. }
  140. //загружаем БД
  141. this.setMyState(ssDbLoading);
  142. log('Searcher DB loading');
  143. const db = new JembaDbThread();//в отдельном потоке
  144. await db.lock({
  145. dbPath,
  146. softLock: true,
  147. tableDefaults: {
  148. cacheSize: config.dbCacheSize,
  149. },
  150. });
  151. try {
  152. //открываем таблицы
  153. await db.openAll({exclude: ['author_id', 'series_id', 'title_id', 'book']});
  154. const bookCacheSize = 500;
  155. await db.open({
  156. table: 'book',
  157. cacheSize: (config.lowMemoryMode || config.dbCacheSize > bookCacheSize ? config.dbCacheSize : bookCacheSize)
  158. });
  159. } catch(e) {
  160. log(LM_ERR, `Database error: ${e.message}`);
  161. if (iteration < 1) {
  162. log('Recreating DB');
  163. await this.loadOrCreateDb(true, iteration + 1);
  164. } else
  165. throw e;
  166. return;
  167. }
  168. //поисковый движок
  169. this.dbSearcher = new DbSearcher(config, db);
  170. await this.dbSearcher.init();
  171. //stuff
  172. db.wwCache = {};
  173. this.db = db;
  174. this.setMyState(ssNormal);
  175. log('Searcher DB ready');
  176. this.logServerStats();
  177. } catch (e) {
  178. log(LM_FATAL, e.message);
  179. asyncExit.exit(1);
  180. }
  181. }
  182. async recreateDb() {
  183. this.setMyState(ssDbCreating);
  184. if (this.dbSearcher) {
  185. await this.dbSearcher.close();
  186. this.dbSearcher = null;
  187. }
  188. await this.closeDb();
  189. await this.loadOrCreateDb(true);
  190. }
  191. async dbConfig() {
  192. this.checkMyState();
  193. const db = this.db;
  194. if (!db.wwCache.config) {
  195. const rows = await db.select({table: 'config'});
  196. const config = {};
  197. for (const row of rows) {
  198. config[row.id] = row.value;
  199. }
  200. db.wwCache.config = config;
  201. }
  202. return db.wwCache.config;
  203. }
  204. async search(from, query) {
  205. this.checkMyState();
  206. const result = await this.dbSearcher.search(from, query);
  207. const config = await this.dbConfig();
  208. result.inpxHash = (config.inpxHash ? config.inpxHash : '');
  209. return result;
  210. }
  211. async bookSearch(query) {
  212. this.checkMyState();
  213. const result = await this.dbSearcher.bookSearch(query);
  214. const config = await this.dbConfig();
  215. result.inpxHash = (config.inpxHash ? config.inpxHash : '');
  216. return result;
  217. }
  218. async opdsQuery(from, query) {
  219. this.checkMyState();
  220. return await this.dbSearcher.opdsQuery(from, query);
  221. }
  222. async getAuthorBookList(authorId, author) {
  223. this.checkMyState();
  224. return await this.dbSearcher.getAuthorBookList(authorId, author);
  225. }
  226. async getAuthorSeriesList(authorId) {
  227. this.checkMyState();
  228. return await this.dbSearcher.getAuthorSeriesList(authorId);
  229. }
  230. async getSeriesBookList(series) {
  231. this.checkMyState();
  232. return await this.dbSearcher.getSeriesBookList(series);
  233. }
  234. async getGenreTree() {
  235. this.checkMyState();
  236. const config = await this.dbConfig();
  237. let result;
  238. const db = this.db;
  239. if (!db.wwCache.genres) {
  240. const genres = _.cloneDeep(genreTree);
  241. const last = genres[genres.length - 1];
  242. const genreValues = new Set();
  243. for (const section of genres) {
  244. for (const g of section.value)
  245. genreValues.add(g.value);
  246. }
  247. //добавим к жанрам те, что нашлись при парсинге
  248. const genreParsed = new Set();
  249. let rows = await db.select({table: 'genre', map: `(r) => ({value: r.value})`});
  250. for (const row of rows) {
  251. genreParsed.add(row.value);
  252. if (!genreValues.has(row.value))
  253. last.value.push({name: row.value, value: row.value});
  254. }
  255. //уберем те, которые не нашлись при парсинге
  256. for (let j = 0; j < genres.length; j++) {
  257. const section = genres[j];
  258. for (let i = 0; i < section.value.length; i++) {
  259. const g = section.value[i];
  260. if (!genreParsed.has(g.value))
  261. section.value.splice(i--, 1);
  262. }
  263. if (!section.value.length)
  264. genres.splice(j--, 1);
  265. }
  266. // langs
  267. rows = await db.select({table: 'lang', map: `(r) => ({value: r.value})`});
  268. const langs = rows.map(r => r.value);
  269. result = {
  270. genreTree: genres,
  271. langList: langs,
  272. inpxHash: (config.inpxHash ? config.inpxHash : ''),
  273. };
  274. db.wwCache.genres = result;
  275. } else {
  276. result = db.wwCache.genres;
  277. }
  278. return result;
  279. }
  280. async extractBook(bookPath) {
  281. const outFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  282. const folder = `${this.config.libDir}/${path.dirname(bookPath)}`;
  283. const file = path.basename(bookPath);
  284. const zipReader = new ZipReader();
  285. await zipReader.open(folder);
  286. try {
  287. await zipReader.extractToFile(file, outFile);
  288. return outFile;
  289. } finally {
  290. await zipReader.close();
  291. }
  292. }
  293. async restoreBook(bookUid, bookPath, downFileName) {
  294. const db = this.db;
  295. let extractedFile = '';
  296. let hash = '';
  297. if (!this.remoteLib) {
  298. extractedFile = await this.extractBook(bookPath);
  299. hash = await utils.getFileHash(extractedFile, 'sha256', 'hex');
  300. } else {
  301. hash = await this.remoteLib.downloadBook(bookUid);
  302. }
  303. const link = `${this.config.bookPathStatic}/${hash}`;
  304. const bookFile = `${this.config.bookDir}/${hash}`;
  305. const bookFileDesc = `${bookFile}.d.json`;
  306. if (!await fs.pathExists(bookFile) || !await fs.pathExists(bookFileDesc)) {
  307. if (!await fs.pathExists(bookFile) && extractedFile) {
  308. const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  309. await utils.gzipFile(extractedFile, tmpFile, 4);
  310. await fs.remove(extractedFile);
  311. await fs.move(tmpFile, bookFile, {overwrite: true});
  312. } else {
  313. await utils.touchFile(bookFile);
  314. }
  315. await fs.writeFile(bookFileDesc, JSON.stringify({bookPath, downFileName}));
  316. } else {
  317. if (extractedFile)
  318. await fs.remove(extractedFile);
  319. await utils.touchFile(bookFile);
  320. await utils.touchFile(bookFileDesc);
  321. }
  322. await db.insert({
  323. table: 'file_hash',
  324. replace: true,
  325. rows: [
  326. {id: bookPath, hash},
  327. {id: hash, bookPath, downFileName}
  328. ]
  329. });
  330. return link;
  331. }
  332. async getBookLink(bookUid) {
  333. this.checkMyState();
  334. try {
  335. const db = this.db;
  336. let link = '';
  337. //найдем bookPath и downFileName
  338. let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
  339. if (!rows.length)
  340. throw new Error('404 Файл не найден');
  341. const book = rows[0];
  342. let downFileName = book.file;
  343. const author = book.author.split(',');
  344. const at = [author[0], book.title];
  345. downFileName = utils.makeValidFileNameOrEmpty(at.filter(r => r).join(' - '))
  346. || utils.makeValidFileNameOrEmpty(at[0])
  347. || utils.makeValidFileNameOrEmpty(at[1])
  348. || downFileName;
  349. downFileName = downFileName.substring(0, 100);
  350. const ext = `.${book.ext}`;
  351. if (downFileName.substring(downFileName.length - ext.length) != ext)
  352. downFileName += ext;
  353. const bookPath = `${book.folder}/${book.file}${ext}`;
  354. //найдем хеш
  355. rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
  356. if (rows.length) {//хеш найден по bookPath
  357. const hash = rows[0].hash;
  358. const bookFile = `${this.config.bookDir}/${hash}`;
  359. const bookFileDesc = `${bookFile}.d.json`;
  360. if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
  361. link = `${this.config.bookPathStatic}/${hash}`;
  362. }
  363. }
  364. if (!link) {
  365. link = await this.restoreBook(bookUid, bookPath, downFileName)
  366. }
  367. if (!link)
  368. throw new Error('404 Файл не найден');
  369. return {link, bookPath, downFileName};
  370. } catch(e) {
  371. log(LM_ERR, `getBookLink error: ${e.message}`);
  372. if (e.message.indexOf('ENOENT') >= 0)
  373. throw new Error('404 Файл не найден');
  374. throw e;
  375. }
  376. }
  377. async getBookInfo(bookUid) {
  378. this.checkMyState();
  379. try {
  380. const db = this.db;
  381. let bookInfo = await this.getBookLink(bookUid);
  382. const hash = path.basename(bookInfo.link);
  383. const bookFile = `${this.config.bookDir}/${hash}`;
  384. const bookFileInfo = `${bookFile}.i.json`;
  385. let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
  386. if (!rows.length)
  387. throw new Error('404 Файл не найден');
  388. const book = rows[0];
  389. const restoreBookInfo = async(info) => {
  390. const result = {};
  391. result.book = book;
  392. result.cover = '';
  393. result.fb2 = false;
  394. let parser = null;
  395. if (book.ext == 'fb2') {
  396. const {fb2, cover, coverExt} = await this.fb2Helper.getDescAndCover(bookFile);
  397. parser = fb2;
  398. result.fb2 = fb2.rawNodes;
  399. if (cover) {
  400. result.cover = `${this.config.bookPathStatic}/${hash}${coverExt}`;
  401. await fs.writeFile(`${bookFile}${coverExt}`, cover);
  402. }
  403. }
  404. Object.assign(info, result);
  405. await fs.writeFile(bookFileInfo, JSON.stringify(info));
  406. if (this.config.branch === 'development') {
  407. await fs.writeFile(`${bookFile}.dev`, `${JSON.stringify(info, null, 2)}\n\n${parser ? parser.toString({format: true}) : ''}`);
  408. }
  409. };
  410. if (!await fs.pathExists(bookFileInfo)) {
  411. await restoreBookInfo(bookInfo);
  412. } else {
  413. await utils.touchFile(bookFileInfo);
  414. const info = await fs.readFile(bookFileInfo, 'utf-8');
  415. const tmpInfo = JSON.parse(info);
  416. //проверим существование файла обложки, восстановим если нету
  417. let coverFile = '';
  418. if (tmpInfo.cover)
  419. coverFile = `${this.config.publicFilesDir}${tmpInfo.cover}`;
  420. if (book.id != tmpInfo.book.id || (coverFile && !await fs.pathExists(coverFile))) {
  421. await restoreBookInfo(bookInfo);
  422. } else {
  423. bookInfo = tmpInfo;
  424. }
  425. }
  426. return {bookInfo};
  427. } catch(e) {
  428. log(LM_ERR, `getBookInfo error: ${e.message}`);
  429. if (e.message.indexOf('ENOENT') >= 0)
  430. throw new Error('404 Файл не найден');
  431. throw e;
  432. }
  433. }
  434. async getInpxFile(params) {
  435. let data = null;
  436. if (params.inpxFileHash && this.inpxFileHash && params.inpxFileHash === this.inpxFileHash) {
  437. data = false;
  438. }
  439. if (data === null)
  440. data = await fs.readFile(this.config.inpxFile, 'base64');
  441. return {data};
  442. }
  443. logServerStats() {
  444. try {
  445. const memUsage = process.memoryUsage().rss/(1024*1024);//Mb
  446. let loadAvg = os.loadavg();
  447. loadAvg = loadAvg.map(v => v.toFixed(2));
  448. log(`Server info [ memUsage: ${memUsage.toFixed(2)}MB, loadAvg: (${loadAvg.join(', ')}) ]`);
  449. if (this.config.server.ready)
  450. log(`Server accessible at http://127.0.0.1:${this.config.server.port} (listening on ${this.config.server.host}:${this.config.server.port})`);
  451. } catch (e) {
  452. log(LM_ERR, e.message);
  453. }
  454. }
  455. async periodicLogServerStats() {
  456. while (1) {// eslint-disable-line
  457. this.logServerStats();
  458. await utils.sleep(60*1000);
  459. }
  460. }
  461. async cleanDir(config) {
  462. const {dir, maxSize} = config;
  463. const list = await fs.readdir(dir);
  464. let size = 0;
  465. let files = [];
  466. //формируем список
  467. for (const filename of list) {
  468. const filePath = `${dir}/${filename}`;
  469. const stat = await fs.stat(filePath);
  470. if (!stat.isDirectory()) {
  471. size += stat.size;
  472. files.push({name: filePath, stat});
  473. }
  474. }
  475. log(LM_WARN, `clean dir ${dir}, maxSize=${maxSize}, found ${files.length} files, total size=${size}`);
  476. files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs);
  477. let i = 0;
  478. //удаляем
  479. while (i < files.length && size > maxSize) {
  480. const file = files[i];
  481. const oldFile = file.name;
  482. await fs.remove(oldFile);
  483. size -= file.stat.size;
  484. i++;
  485. }
  486. log(LM_WARN, `removed ${i} files`);
  487. }
  488. async periodicCleanDir(dirConfig) {
  489. try {
  490. for (const config of dirConfig)
  491. await fs.ensureDir(config.dir);
  492. let lastCleanDirTime = 0;
  493. while (1) {// eslint-disable-line no-constant-condition
  494. //чистка папок
  495. if (Date.now() - lastCleanDirTime >= cleanDirPeriod) {
  496. for (const config of dirConfig) {
  497. try {
  498. await this.cleanDir(config);
  499. } catch(e) {
  500. log(LM_ERR, e.stack);
  501. }
  502. }
  503. lastCleanDirTime = Date.now();
  504. }
  505. await utils.sleep(60*1000);//интервал проверки 1 минута
  506. }
  507. } catch (e) {
  508. log(LM_FATAL, e.message);
  509. asyncExit.exit(1);
  510. }
  511. }
  512. async periodicCheckInpx() {
  513. const inpxCheckInterval = this.config.inpxCheckInterval;
  514. if (!inpxCheckInterval)
  515. return;
  516. while (1) {// eslint-disable-line no-constant-condition
  517. try {
  518. while (this.myState != ssNormal)
  519. await utils.sleep(1000);
  520. if (this.remoteLib) {
  521. await this.remoteLib.downloadInpxFile();
  522. }
  523. const newInpxHash = await this.inpxHashCreator.getHash();
  524. const dbConfig = await this.dbConfig();
  525. const currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : '');
  526. if (newInpxHash !== currentInpxHash) {
  527. log('inpx file: changes found, recreating DB');
  528. await this.recreateDb();
  529. } else {
  530. log('inpx file: no changes');
  531. }
  532. } catch(e) {
  533. log(LM_ERR, `periodicCheckInpx: ${e.message}`);
  534. }
  535. await utils.sleep(inpxCheckInterval*60*1000);
  536. }
  537. }
  538. }
  539. module.exports = WebWorker;