WebWorker.js 16 KB

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