WebWorker.js 23 KB

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