WebWorker.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 = 2*60*60*1000;//каждые 2 часа
  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. result = {
  273. genreTree: genres,
  274. langList: langs,
  275. inpxHash: (config.inpxHash ? config.inpxHash : ''),
  276. };
  277. db.wwCache.genres = result;
  278. } else {
  279. result = db.wwCache.genres;
  280. }
  281. return result;
  282. }
  283. async extractBook(bookPath) {
  284. const outFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  285. const folder = `${this.config.libDir}/${path.dirname(bookPath)}`;
  286. const file = path.basename(bookPath);
  287. const zipReader = new ZipReader();
  288. await zipReader.open(folder);
  289. try {
  290. await zipReader.extractToFile(file, outFile);
  291. return outFile;
  292. } finally {
  293. await zipReader.close();
  294. }
  295. }
  296. async restoreBook(bookUid, bookPath, downFileName) {
  297. const db = this.db;
  298. let extractedFile = '';
  299. let hash = '';
  300. if (!this.remoteLib) {
  301. extractedFile = await this.extractBook(bookPath);
  302. hash = await utils.getFileHash(extractedFile, 'sha256', 'hex');
  303. } else {
  304. hash = await this.remoteLib.downloadBook(bookUid);
  305. }
  306. const link = `${this.config.bookPathStatic}/${hash}`;
  307. const bookFile = `${this.config.bookDir}/${hash}`;
  308. const bookFileDesc = `${bookFile}.d.json`;
  309. if (!await fs.pathExists(bookFile) || !await fs.pathExists(bookFileDesc)) {
  310. if (!await fs.pathExists(bookFile) && extractedFile) {
  311. const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  312. await utils.gzipFile(extractedFile, tmpFile, 4);
  313. await fs.remove(extractedFile);
  314. await fs.move(tmpFile, bookFile, {overwrite: true});
  315. } else {
  316. await utils.touchFile(bookFile);
  317. }
  318. await fs.writeFile(bookFileDesc, JSON.stringify({bookPath, downFileName}));
  319. } else {
  320. if (extractedFile)
  321. await fs.remove(extractedFile);
  322. await utils.touchFile(bookFile);
  323. await utils.touchFile(bookFileDesc);
  324. }
  325. await db.insert({
  326. table: 'file_hash',
  327. replace: true,
  328. rows: [
  329. {id: bookPath, hash},
  330. {id: hash, bookPath, downFileName}
  331. ]
  332. });
  333. return link;
  334. }
  335. async getBookLink(bookUid) {
  336. this.checkMyState();
  337. try {
  338. const db = this.db;
  339. let link = '';
  340. //найдем downFileName и bookPath
  341. let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
  342. if (!rows.length)
  343. throw new Error('404 Файл не найден');
  344. const book = rows[0];
  345. let downFileName = book.file;
  346. const authors = book.author.split(',');
  347. let author = authors[0];
  348. author = author.split(' ').filter(r => r.trim());
  349. for (let i = 1; i < author.length; i++)
  350. author[i] = `${(i === 1 ? ' ' : '')}${author[i][0]}.`;
  351. if (authors.length > 1)
  352. author.push(' и др.');
  353. const at = [author.join(''), (book.title ? `_${book.title}` : '')];
  354. downFileName = utils.makeValidFileNameOrEmpty(at.filter(r => r).join(''))
  355. || utils.makeValidFileNameOrEmpty(at[0])
  356. || utils.makeValidFileNameOrEmpty(at[1])
  357. || downFileName;
  358. if (downFileName.length > 50)
  359. downFileName = `${downFileName.substring(0, 50)}_`;
  360. const ext = `.${book.ext}`;
  361. if (downFileName.substring(downFileName.length - ext.length) != ext)
  362. downFileName += ext;
  363. const bookPath = `${book.folder}/${book.file}${ext}`;
  364. //найдем хеш
  365. rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
  366. if (rows.length) {//хеш найден по bookPath
  367. const hash = rows[0].hash;
  368. const bookFile = `${this.config.bookDir}/${hash}`;
  369. const bookFileDesc = `${bookFile}.d.json`;
  370. if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
  371. link = `${this.config.bookPathStatic}/${hash}`;
  372. }
  373. }
  374. if (!link) {
  375. link = await this.restoreBook(bookUid, bookPath, downFileName)
  376. }
  377. if (!link)
  378. throw new Error('404 Файл не найден');
  379. return {link, bookPath, downFileName};
  380. } catch(e) {
  381. log(LM_ERR, `getBookLink error: ${e.message}`);
  382. if (e.message.indexOf('ENOENT') >= 0)
  383. throw new Error('404 Файл не найден');
  384. throw e;
  385. }
  386. }
  387. async getBookInfo(bookUid) {
  388. this.checkMyState();
  389. try {
  390. const db = this.db;
  391. let bookInfo = await this.getBookLink(bookUid);
  392. const hash = path.basename(bookInfo.link);
  393. const bookFile = `${this.config.bookDir}/${hash}`;
  394. const bookFileInfo = `${bookFile}.i.json`;
  395. let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
  396. if (!rows.length)
  397. throw new Error('404 Файл не найден');
  398. const book = rows[0];
  399. const restoreBookInfo = async(info) => {
  400. const result = {};
  401. result.book = book;
  402. result.cover = '';
  403. result.fb2 = false;
  404. let parser = null;
  405. if (book.ext == 'fb2') {
  406. const {fb2, cover, coverExt} = await this.fb2Helper.getDescAndCover(bookFile);
  407. parser = fb2;
  408. result.fb2 = fb2.rawNodes;
  409. if (cover) {
  410. result.cover = `${this.config.bookPathStatic}/${hash}${coverExt}`;
  411. await fs.writeFile(`${bookFile}${coverExt}`, cover);
  412. }
  413. }
  414. Object.assign(info, result);
  415. await fs.writeFile(bookFileInfo, JSON.stringify(info));
  416. if (this.config.branch === 'development') {
  417. await fs.writeFile(`${bookFile}.dev`, `${JSON.stringify(info, null, 2)}\n\n${parser ? parser.toString({format: true}) : ''}`);
  418. }
  419. };
  420. if (!await fs.pathExists(bookFileInfo)) {
  421. await restoreBookInfo(bookInfo);
  422. } else {
  423. await utils.touchFile(bookFileInfo);
  424. const info = await fs.readFile(bookFileInfo, 'utf-8');
  425. const tmpInfo = JSON.parse(info);
  426. //проверим существование файла обложки, восстановим если нету
  427. let coverFile = '';
  428. if (tmpInfo.cover)
  429. coverFile = `${this.config.publicFilesDir}${tmpInfo.cover}`;
  430. if (book.id != tmpInfo.book.id || (coverFile && !await fs.pathExists(coverFile))) {
  431. await restoreBookInfo(bookInfo);
  432. } else {
  433. bookInfo = tmpInfo;
  434. }
  435. }
  436. return {bookInfo};
  437. } catch(e) {
  438. log(LM_ERR, `getBookInfo error: ${e.message}`);
  439. if (e.message.indexOf('ENOENT') >= 0)
  440. throw new Error('404 Файл не найден');
  441. throw e;
  442. }
  443. }
  444. async getInpxFile(params) {
  445. let data = null;
  446. if (params.inpxFileHash && this.inpxFileHash && params.inpxFileHash === this.inpxFileHash) {
  447. data = false;
  448. }
  449. if (data === null)
  450. data = await fs.readFile(this.config.inpxFile, 'base64');
  451. return {data};
  452. }
  453. logServerStats() {
  454. try {
  455. const memUsage = process.memoryUsage().rss/(1024*1024);//Mb
  456. let loadAvg = os.loadavg();
  457. loadAvg = loadAvg.map(v => v.toFixed(2));
  458. log(`Server info [ memUsage: ${memUsage.toFixed(2)}MB, loadAvg: (${loadAvg.join(', ')}) ]`);
  459. if (this.config.server.ready)
  460. log(`Server accessible at http://127.0.0.1:${this.config.server.port} (listening on ${this.config.server.host}:${this.config.server.port})`);
  461. } catch (e) {
  462. log(LM_ERR, e.message);
  463. }
  464. }
  465. async periodicLogServerStats() {
  466. while (1) {// eslint-disable-line
  467. this.logServerStats();
  468. await utils.sleep(60*1000);
  469. }
  470. }
  471. async cleanDir(config) {
  472. const {dir, maxSize} = config;
  473. const list = await fs.readdir(dir);
  474. let size = 0;
  475. let files = [];
  476. //формируем список
  477. for (const filename of list) {
  478. const filePath = `${dir}/${filename}`;
  479. const stat = await fs.stat(filePath);
  480. if (!stat.isDirectory()) {
  481. size += stat.size;
  482. files.push({name: filePath, stat});
  483. }
  484. }
  485. log(LM_WARN, `clean dir ${dir}, maxSize=${maxSize}, found ${files.length} files, total size=${size}`);
  486. files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs);
  487. let i = 0;
  488. //удаляем
  489. while (i < files.length && size > maxSize) {
  490. const file = files[i];
  491. const oldFile = file.name;
  492. await fs.remove(oldFile);
  493. size -= file.stat.size;
  494. i++;
  495. }
  496. log(LM_WARN, `removed ${i} files`);
  497. }
  498. async periodicCleanDir(dirConfig) {
  499. try {
  500. for (const config of dirConfig)
  501. await fs.ensureDir(config.dir);
  502. let lastCleanDirTime = 0;
  503. while (1) {// eslint-disable-line no-constant-condition
  504. //чистка папок
  505. if (Date.now() - lastCleanDirTime >= cleanDirInterval) {
  506. for (const config of dirConfig) {
  507. try {
  508. await this.cleanDir(config);
  509. } catch(e) {
  510. log(LM_ERR, e.stack);
  511. }
  512. }
  513. lastCleanDirTime = Date.now();
  514. }
  515. await utils.sleep(60*1000);//интервал проверки 1 минута
  516. }
  517. } catch (e) {
  518. log(LM_FATAL, e.message);
  519. asyncExit.exit(1);
  520. }
  521. }
  522. async periodicCheckInpx() {
  523. const inpxCheckInterval = this.config.inpxCheckInterval;
  524. if (!inpxCheckInterval)
  525. return;
  526. while (1) {// eslint-disable-line no-constant-condition
  527. try {
  528. while (this.myState != ssNormal)
  529. await utils.sleep(1000);
  530. if (this.remoteLib) {
  531. await this.remoteLib.downloadInpxFile();
  532. }
  533. const newInpxHash = await this.inpxHashCreator.getHash();
  534. const dbConfig = await this.dbConfig();
  535. const currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : '');
  536. if (newInpxHash !== currentInpxHash) {
  537. log('inpx file: changes found, recreating DB');
  538. await this.recreateDb();
  539. } else {
  540. log('inpx file: no changes');
  541. }
  542. } catch(e) {
  543. log(LM_ERR, `periodicCheckInpx: ${e.message}`);
  544. }
  545. await utils.sleep(inpxCheckInterval*60*1000);
  546. }
  547. }
  548. async periodicCheckNewRelease() {
  549. const checkReleaseLink = this.config.checkReleaseLink;
  550. if (!checkReleaseLink)
  551. return;
  552. const down = new FileDownloader(1024*1024);
  553. while (1) {// eslint-disable-line no-constant-condition
  554. try {
  555. let release = await down.load(checkReleaseLink);
  556. release = JSON.parse(release.toString());
  557. if (release.tag_name)
  558. this.config.latestVersion = release.tag_name;
  559. } catch(e) {
  560. log(LM_ERR, `periodicCheckNewRelease: ${e.message}`);
  561. }
  562. await utils.sleep(checkReleaseInterval);
  563. }
  564. }
  565. }
  566. module.exports = WebWorker;