WebWorker.js 15 KB

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