WebWorker.js 16 KB

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