WebWorker.js 23 KB

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