WebWorker.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. const os = require('os');
  2. const path = require('path');
  3. const fs = require('fs-extra');
  4. const zlib = require('zlib');
  5. const _ = require('lodash');
  6. const ZipReader = require('./ZipReader');
  7. const WorkerState = require('./WorkerState');//singleton
  8. const { JembaDbThread } = require('jembadb');
  9. const DbCreator = require('./DbCreator');
  10. const DbSearcher = require('./DbSearcher');
  11. const InpxHashCreator = require('./InpxHashCreator');
  12. const RemoteLib = require('./RemoteLib');//singleton
  13. const ayncExit = new (require('./AsyncExit'))();
  14. const log = new (require('./AppLogger'))().log;//singleton
  15. const utils = require('./utils');
  16. const genreTree = require('./genres');
  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.wState = this.workerState.getControl('server_state');
  39. this.myState = '';
  40. this.db = null;
  41. this.dbSearcher = null;
  42. ayncExit.add(this.closeDb.bind(this));
  43. this.loadOrCreateDb();//no await
  44. this.periodicLogServerStats();//no await
  45. const dirConfig = [
  46. {
  47. dir: `${this.config.publicDir}/files`,
  48. maxSize: this.config.maxFilesDirSize,
  49. },
  50. ];
  51. this.periodicCleanDir(dirConfig);//no await
  52. this.periodicCheckInpx();//no await
  53. instance = this;
  54. }
  55. return instance;
  56. }
  57. checkMyState() {
  58. if (this.myState != ssNormal)
  59. throw new Error('server_busy');
  60. }
  61. setMyState(newState, workerState = {}) {
  62. this.myState = newState;
  63. this.wState.set(Object.assign({}, workerState, {
  64. state: newState,
  65. serverMessage: stateToText[newState]
  66. }));
  67. }
  68. async closeDb() {
  69. if (this.db) {
  70. await this.db.unlock();
  71. this.db = null;
  72. }
  73. }
  74. async createDb(dbPath) {
  75. this.setMyState(ssDbCreating);
  76. log('Searcher DB create start');
  77. const config = this.config;
  78. if (await fs.pathExists(dbPath))
  79. throw new Error(`createDb.pathExists: ${dbPath}`);
  80. const db = new JembaDbThread();
  81. await db.lock({
  82. dbPath,
  83. create: true,
  84. softLock: true,
  85. tableDefaults: {
  86. cacheSize: 5,
  87. },
  88. });
  89. try {
  90. const dbCreator = new DbCreator(config);
  91. await dbCreator.run(db, (state) => {
  92. this.setMyState(ssDbCreating, state);
  93. if (state.fileName)
  94. log(` load ${state.fileName}`);
  95. if (state.recsLoaded)
  96. log(` processed ${state.recsLoaded} records`);
  97. if (state.job)
  98. log(` ${state.job}`);
  99. });
  100. log('Searcher DB successfully created');
  101. } finally {
  102. await db.unlock();
  103. }
  104. }
  105. async loadOrCreateDb(recreate = false) {
  106. this.setMyState(ssDbLoading);
  107. try {
  108. const config = this.config;
  109. const dbPath = `${config.dataDir}/db`;
  110. //пересоздаем БД из INPX если нужно
  111. if (config.recreateDb || recreate)
  112. await fs.remove(dbPath);
  113. if (!await fs.pathExists(dbPath)) {
  114. await this.createDb(dbPath);
  115. utils.freeMemory();
  116. }
  117. //загружаем БД
  118. this.setMyState(ssDbLoading);
  119. log('Searcher DB loading');
  120. const db = new JembaDbThread();
  121. await db.lock({
  122. dbPath,
  123. softLock: true,
  124. tableDefaults: {
  125. cacheSize: 5,
  126. },
  127. });
  128. //открываем все таблицы
  129. await db.openAll();
  130. this.dbSearcher = new DbSearcher(config, db);
  131. db.wwCache = {};
  132. this.db = db;
  133. log('Searcher DB ready');
  134. this.logServerStats();
  135. } catch (e) {
  136. log(LM_FATAL, e.message);
  137. ayncExit.exit(1);
  138. } finally {
  139. this.setMyState(ssNormal);
  140. }
  141. }
  142. async recreateDb() {
  143. this.setMyState(ssDbCreating);
  144. if (this.dbSearcher) {
  145. await this.dbSearcher.close();
  146. this.dbSearcher = null;
  147. }
  148. await this.closeDb();
  149. await this.loadOrCreateDb(true);
  150. }
  151. async dbConfig() {
  152. this.checkMyState();
  153. const db = this.db;
  154. if (!db.wwCache.config) {
  155. const rows = await db.select({table: 'config'});
  156. const config = {};
  157. for (const row of rows) {
  158. config[row.id] = row.value;
  159. }
  160. db.wwCache.config = config;
  161. }
  162. return db.wwCache.config;
  163. }
  164. async search(query) {
  165. this.checkMyState();
  166. const config = await this.dbConfig();
  167. const result = await this.dbSearcher.search(query);
  168. return {
  169. author: result.result,
  170. totalFound: result.totalFound,
  171. inpxHash: (config.inpxHash ? config.inpxHash : ''),
  172. };
  173. }
  174. async getBookList(authorId) {
  175. this.checkMyState();
  176. return await this.dbSearcher.getBookList(authorId);
  177. }
  178. async getSeriesBookList(seriesId) {
  179. this.checkMyState();
  180. return await this.dbSearcher.getSeriesBookList(seriesId);
  181. }
  182. async getGenreTree() {
  183. this.checkMyState();
  184. const config = await this.dbConfig();
  185. let result;
  186. const db = this.db;
  187. if (!db.wwCache.genres) {
  188. const genres = _.cloneDeep(genreTree);
  189. const last = genres[genres.length - 1];
  190. const genreValues = new Set();
  191. for (const section of genres) {
  192. for (const g of section.value)
  193. genreValues.add(g.value);
  194. }
  195. //добавим к жанрам те, что нашлись при парсинге
  196. const genreParsed = new Set();
  197. let rows = await db.select({table: 'genre', map: `(r) => ({value: r.value})`});
  198. for (const row of rows) {
  199. genreParsed.add(row.value);
  200. if (!genreValues.has(row.value))
  201. last.value.push({name: row.value, value: row.value});
  202. }
  203. //уберем те, которые не нашлись при парсинге
  204. for (let j = 0; j < genres.length; j++) {
  205. const section = genres[j];
  206. for (let i = 0; i < section.value.length; i++) {
  207. const g = section.value[i];
  208. if (!genreParsed.has(g.value))
  209. section.value.splice(i--, 1);
  210. }
  211. if (!section.value.length)
  212. genres.splice(j--, 1);
  213. }
  214. // langs
  215. rows = await db.select({table: 'lang', map: `(r) => ({value: r.value})`});
  216. const langs = rows.map(r => r.value);
  217. result = {
  218. genreTree: genres,
  219. langList: langs,
  220. inpxHash: (config.inpxHash ? config.inpxHash : ''),
  221. };
  222. db.wwCache.genres = result;
  223. } else {
  224. result = db.wwCache.genres;
  225. }
  226. return result;
  227. }
  228. async extractBook(bookPath) {
  229. const outFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  230. const folder = `${this.config.libDir}/${path.dirname(bookPath)}`;
  231. const file = path.basename(bookPath);
  232. const zipReader = new ZipReader();
  233. await zipReader.open(folder);
  234. try {
  235. await zipReader.extractToFile(file, outFile);
  236. return outFile;
  237. } finally {
  238. await zipReader.close();
  239. }
  240. }
  241. //async
  242. gzipFile(inputFile, outputFile, level = 1) {
  243. return new Promise((resolve, reject) => {
  244. const gzip = zlib.createGzip({level});
  245. const input = fs.createReadStream(inputFile);
  246. const output = fs.createWriteStream(outputFile);
  247. input.pipe(gzip).pipe(output).on('finish', (err) => {
  248. if (err) reject(err);
  249. else resolve();
  250. });
  251. });
  252. }
  253. async restoreBook(bookPath, downFileName) {
  254. const db = this.db;
  255. let extractedFile = '';
  256. let hash = '';
  257. if (!this.remoteLib) {
  258. extractedFile = await this.extractBook(bookPath);
  259. hash = await utils.getFileHash(extractedFile, 'sha256', 'hex');
  260. } else {
  261. hash = await this.remoteLib.downloadBook(bookPath, downFileName);
  262. }
  263. const link = `/files/${hash}`;
  264. const publicPath = `${this.config.publicDir}${link}`;
  265. if (!await fs.pathExists(publicPath)) {
  266. await fs.ensureDir(path.dirname(publicPath));
  267. const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  268. await this.gzipFile(extractedFile, tmpFile, 4);
  269. await fs.remove(extractedFile);
  270. await fs.move(tmpFile, publicPath, {overwrite: true});
  271. } else {
  272. if (extractedFile)
  273. await fs.remove(extractedFile);
  274. await utils.touchFile(publicPath);
  275. }
  276. await db.insert({
  277. table: 'file_hash',
  278. replace: true,
  279. rows: [
  280. {id: bookPath, hash},
  281. {id: hash, bookPath, downFileName}
  282. ]
  283. });
  284. return link;
  285. }
  286. async getBookLink(params) {
  287. this.checkMyState();
  288. const {bookPath, downFileName} = params;
  289. try {
  290. const db = this.db;
  291. let link = '';
  292. //найдем хеш
  293. const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
  294. if (rows.length) {//хеш найден по bookPath
  295. const hash = rows[0].hash;
  296. link = `/files/${hash}`;
  297. const publicPath = `${this.config.publicDir}${link}`;
  298. if (!await fs.pathExists(publicPath)) {
  299. link = '';
  300. }
  301. }
  302. if (!link) {
  303. link = await this.restoreBook(bookPath, downFileName)
  304. }
  305. if (!link)
  306. throw new Error('404 Файл не найден');
  307. return {link};
  308. } catch(e) {
  309. log(LM_ERR, `getBookLink error: ${e.message}`);
  310. if (e.message.indexOf('ENOENT') >= 0)
  311. throw new Error('404 Файл не найден');
  312. throw e;
  313. }
  314. }
  315. async restoreBookFile(publicPath) {
  316. this.checkMyState();
  317. try {
  318. const db = this.db;
  319. const hash = path.basename(publicPath);
  320. //найдем bookPath и downFileName
  321. const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(hash)})`});
  322. if (rows.length) {//нашли по хешу
  323. const rec = rows[0];
  324. await this.restoreBook(rec.bookPath, rec.downFileName);
  325. return rec.downFileName;
  326. } else {//bookPath не найден
  327. throw new Error('404 Файл не найден');
  328. }
  329. } catch(e) {
  330. log(LM_ERR, `restoreBookFile error: ${e.message}`);
  331. if (e.message.indexOf('ENOENT') >= 0)
  332. throw new Error('404 Файл не найден');
  333. throw e;
  334. }
  335. }
  336. async getDownFileName(publicPath) {
  337. this.checkMyState();
  338. const db = this.db;
  339. const hash = path.basename(publicPath);
  340. //найдем downFileName
  341. const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(hash)})`});
  342. if (rows.length) {//downFileName найден по хешу
  343. return rows[0].downFileName;
  344. } else {//bookPath не найден
  345. throw new Error('404 Файл не найден');
  346. }
  347. }
  348. logServerStats() {
  349. try {
  350. const memUsage = process.memoryUsage().rss/(1024*1024);//Mb
  351. let loadAvg = os.loadavg();
  352. loadAvg = loadAvg.map(v => v.toFixed(2));
  353. log(`Server info [ memUsage: ${memUsage.toFixed(2)}MB, loadAvg: (${loadAvg.join(', ')}) ]`);
  354. if (this.config.server.ready)
  355. log(`Server accessible on http://127.0.0.1:${this.config.server.port} (listening on ${this.config.server.host}:${this.config.server.port})`);
  356. } catch (e) {
  357. log(LM_ERR, e.message);
  358. }
  359. }
  360. async periodicLogServerStats() {
  361. while (1) {// eslint-disable-line
  362. this.logServerStats();
  363. await utils.sleep(60*1000);
  364. }
  365. }
  366. async cleanDir(config) {
  367. const {dir, maxSize} = config;
  368. const list = await fs.readdir(dir);
  369. let size = 0;
  370. let files = [];
  371. //формируем список
  372. for (const filename of list) {
  373. const filePath = `${dir}/${filename}`;
  374. const stat = await fs.stat(filePath);
  375. if (!stat.isDirectory()) {
  376. size += stat.size;
  377. files.push({name: filePath, stat});
  378. }
  379. }
  380. log(LM_WARN, `clean dir ${dir}, maxSize=${maxSize}, found ${files.length} files, total size=${size}`);
  381. files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs);
  382. let i = 0;
  383. //удаляем
  384. while (i < files.length && size > maxSize) {
  385. const file = files[i];
  386. const oldFile = file.name;
  387. await fs.remove(oldFile);
  388. size -= file.stat.size;
  389. i++;
  390. }
  391. log(LM_WARN, `removed ${i} files`);
  392. }
  393. async periodicCleanDir(dirConfig) {
  394. try {
  395. for (const config of dirConfig)
  396. await fs.ensureDir(config.dir);
  397. let lastCleanDirTime = 0;
  398. while (1) {// eslint-disable-line no-constant-condition
  399. //чистка папок
  400. if (Date.now() - lastCleanDirTime >= cleanDirPeriod) {
  401. for (const config of dirConfig) {
  402. try {
  403. await this.cleanDir(config);
  404. } catch(e) {
  405. log(LM_ERR, e.stack);
  406. }
  407. }
  408. lastCleanDirTime = Date.now();
  409. }
  410. await utils.sleep(60*1000);//интервал проверки 1 минута
  411. }
  412. } catch (e) {
  413. log(LM_FATAL, e.message);
  414. ayncExit.exit(1);
  415. }
  416. }
  417. async periodicCheckInpx() {
  418. const inpxCheckInterval = this.config.inpxCheckInterval;
  419. if (!inpxCheckInterval)
  420. return;
  421. const inpxHashCreator = new InpxHashCreator(this.config);
  422. while (1) {// eslint-disable-line no-constant-condition
  423. try {
  424. while (this.myState != ssNormal)
  425. await utils.sleep(1000);
  426. if (this.remoteLib) {
  427. await this.remoteLib.downloadInpxFile(60*1000);
  428. }
  429. const newInpxHash = await inpxHashCreator.getHash();
  430. const dbConfig = await this.dbConfig();
  431. const currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : '');
  432. if (newInpxHash !== currentInpxHash) {
  433. log('inpx file: changes found, recreating DB');
  434. await this.recreateDb();
  435. } else {
  436. log('inpx file: no changes');
  437. }
  438. } catch(e) {
  439. log(LM_ERR, `periodicCheckInpx: ${e.message}`);
  440. }
  441. await utils.sleep(inpxCheckInterval*60*1000);
  442. }
  443. }
  444. }
  445. module.exports = WebWorker;