WebWorker.js 15 KB

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