ReaderWorker.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const LimitedQueue = require('../LimitedQueue');
  4. const WorkerState = require('../WorkerState');//singleton
  5. const FileDownloader = require('../FileDownloader');
  6. const FileDecompressor = require('../FileDecompressor');
  7. const BookConverter = require('./BookConverter');
  8. const RemoteStorage = require('../RemoteStorage');
  9. const JembaConnManager = require('../../db/JembaConnManager');//singleton
  10. const ayncExit = new (require('../AsyncExit'))();
  11. const utils = require('../utils');
  12. const log = new (require('../AppLogger'))().log;//singleton
  13. const cleanDirPeriod = 60*60*1000;//каждый час
  14. const remoteSendPeriod = 119*1000;//примерно раз 2 минуты
  15. const queue = new LimitedQueue(5, 100, 2*60*1000 + 15000);//2 минуты ожидание подвижек
  16. let instance = null;
  17. //singleton
  18. class ReaderWorker {
  19. constructor(config) {
  20. if (!instance) {
  21. this.config = Object.assign({}, config);
  22. this.config.tempDownloadDir = `${config.tempDir}/download`;
  23. fs.ensureDirSync(this.config.tempDownloadDir);
  24. this.config.tempPublicDir = `${config.publicDir}/tmp`;
  25. fs.ensureDirSync(this.config.tempPublicDir);
  26. this.workerState = new WorkerState();
  27. this.down = new FileDownloader(config.maxUploadFileSize);
  28. this.decomp = new FileDecompressor(3*config.maxUploadFileSize);
  29. this.bookConverter = new BookConverter(this.config);
  30. this.connManager = new JembaConnManager();
  31. this.appDb = this.connManager.db['app'];
  32. this.remoteStorage = false;
  33. if (config.remoteStorage) {
  34. this.remoteStorage = new RemoteStorage(
  35. Object.assign({maxContentLength: 3*config.maxUploadFileSize}, config.remoteStorage)
  36. );
  37. }
  38. this.dirConfigArr = [
  39. {
  40. dir: this.config.tempPublicDir,
  41. remoteDir: '/tmp',
  42. maxSize: this.config.maxTempPublicDirSize,
  43. moveToRemote: true,
  44. },
  45. {
  46. dir: this.config.uploadDir,
  47. remoteDir: '/upload',
  48. maxSize: this.config.maxUploadPublicDirSize,
  49. moveToRemote: true,
  50. }
  51. ];
  52. //преобразуем в объект для большего удобства
  53. this.dirConfig = {};
  54. for (const configRec of this.dirConfigArr)
  55. this.dirConfig[configRec.remoteDir] = configRec;
  56. this.remoteFilesToSend = [];
  57. this.periodicCleanDir();//no await
  58. instance = this;
  59. }
  60. return instance;
  61. }
  62. async loadBook(opts, wState) {
  63. const url = opts.url;
  64. let decompDir = '';
  65. let downloadedFilename = '';
  66. let isUploaded = false;
  67. let convertFilename = '';
  68. const overLoadMes = 'Слишком большая очередь загрузки. Пожалуйста, попробуйте позже.';
  69. const fileNotFoundMes = 'Файл не найден';
  70. const overLoadErr = new Error(overLoadMes);
  71. let q = null;
  72. try {
  73. wState.set({state: 'queue', step: 1, totalSteps: 1});
  74. try {
  75. let qSize = 0;
  76. q = await queue.get((place) => {
  77. wState.set({place, progress: (qSize ? Math.round((qSize - place)/qSize*100) : 0)});
  78. if (!qSize)
  79. qSize = place;
  80. });
  81. } catch (e) {
  82. throw overLoadErr;
  83. }
  84. wState.set({state: 'download', step: 1, totalSteps: 3, url});
  85. const tempFilename = utils.randomHexString(30);
  86. const tempFilename2 = utils.randomHexString(30);
  87. const decompDirname = utils.randomHexString(30);
  88. let downloadSize = -1;
  89. //download or use uploaded
  90. if (url.indexOf('disk://') != 0) {//download
  91. const downdata = await this.down.load(url, {}, (progress) => {
  92. wState.set({progress});
  93. }, q.abort);
  94. downloadedFilename = `${this.config.tempDownloadDir}/${tempFilename}`;
  95. downloadSize = downdata.length;
  96. await fs.writeFile(downloadedFilename, downdata);
  97. } else {//uploaded file
  98. const fileHash = url.substr(7);
  99. downloadedFilename = `${this.config.uploadDir}/${fileHash}`;
  100. if (!await fs.pathExists(downloadedFilename)) {
  101. //если удалено из upload, попробуем восстановить из удаленного хранилища
  102. try {
  103. await this.restoreRemoteFile(fileHash, '/upload');
  104. } catch(e) {
  105. throw new Error('Файл не найден на сервере (возможно был удален как устаревший). Пожалуйста, загрузите файл с диска на сервер заново.');
  106. }
  107. }
  108. await utils.touchFile(downloadedFilename);
  109. isUploaded = true;
  110. }
  111. wState.set({progress: 100});
  112. if (q.abort())
  113. throw overLoadErr;
  114. q.resetTimeout();
  115. //decompress
  116. wState.set({state: 'decompress', step: 2, progress: 0});
  117. decompDir = `${this.config.tempDownloadDir}/${decompDirname}`;
  118. let decompFiles = {};
  119. try {
  120. decompFiles = await this.decomp.decompressNested(downloadedFilename, decompDir);
  121. } catch (e) {
  122. log(LM_ERR, e.stack);
  123. throw new Error('Ошибка распаковки');
  124. }
  125. wState.set({progress: 100});
  126. if (q.abort())
  127. throw overLoadErr;
  128. q.resetTimeout();
  129. //конвертирование в fb2
  130. wState.set({state: 'convert', step: 3, progress: 0});
  131. convertFilename = `${this.config.tempDownloadDir}/${tempFilename2}`;
  132. await this.bookConverter.convertToFb2(decompFiles, convertFilename, opts, progress => {
  133. wState.set({progress});
  134. if (queue.freed > 0)
  135. q.resetTimeout();
  136. }, q.abort);
  137. //сжимаем файл в tmp, если там уже нет с тем же именем-sha256
  138. const compFilename = await this.decomp.gzipFileIfNotExists(convertFilename, this.config.tempPublicDir);
  139. const stat = await fs.stat(compFilename);
  140. wState.set({progress: 100});
  141. //finish
  142. const finishFilename = path.basename(compFilename);
  143. const result = {path: `/tmp/${finishFilename}`, size: stat.size};
  144. if (downloadSize >= 0)
  145. result.downloadSize = downloadSize;
  146. wState.finish(result);
  147. //асинхронно через 30 сек добавим в очередь на отправку
  148. //т.к. gzipFileIfNotExists может переупаковать файл
  149. (async() => {
  150. await utils.sleep(30*1000);
  151. this.pushRemoteSend(compFilename, '/tmp');
  152. })();
  153. } catch (e) {
  154. log(LM_ERR, `url: ${url}, downloadedFilename: ${downloadedFilename}`);
  155. log(LM_ERR, e.stack);
  156. let mes = e.message.split('|FORLOG|');
  157. if (mes[1])
  158. log(LM_ERR, mes[0] + mes[1]);
  159. mes = mes[0];
  160. if (mes == 'abort')
  161. mes = overLoadMes;
  162. if (mes.indexOf('ENOTDIR') >= 0)
  163. mes = fileNotFoundMes;
  164. wState.set({state: 'error', error: mes});
  165. } finally {
  166. //clean
  167. try {
  168. if (q)
  169. q.ret();
  170. if (decompDir)
  171. await fs.remove(decompDir);
  172. if (downloadedFilename && !isUploaded)
  173. await fs.remove(downloadedFilename);
  174. if (convertFilename)
  175. await fs.remove(convertFilename);
  176. } catch (e) {
  177. log(LM_ERR, `Remove error: ${e.stack}`);
  178. }
  179. }
  180. }
  181. loadBookUrl(opts) {
  182. const workerId = this.workerState.generateWorkerId();
  183. const wState = this.workerState.getControl(workerId);
  184. wState.set({state: 'start'});
  185. this.loadBook(opts, wState);
  186. return workerId;
  187. }
  188. async saveFile(file) {
  189. const hash = await utils.getFileHash(file.path, 'sha256', 'hex');
  190. const outFilename = `${this.config.uploadDir}/${hash}`;
  191. if (!await fs.pathExists(outFilename)) {
  192. await fs.move(file.path, outFilename);
  193. this.pushRemoteSend(outFilename, '/upload');
  194. } else {
  195. await utils.touchFile(outFilename);
  196. await fs.remove(file.path);
  197. }
  198. return `disk://${hash}`;
  199. }
  200. async saveFileBuf(buf) {
  201. const hash = await utils.getBufHash(buf, 'sha256', 'hex');
  202. const outFilename = `${this.config.uploadDir}/${hash}`;
  203. if (!await fs.pathExists(outFilename)) {
  204. await fs.writeFile(outFilename, buf);
  205. this.pushRemoteSend(outFilename, '/upload');
  206. } else {
  207. await utils.touchFile(outFilename);
  208. }
  209. return `disk://${hash}`;
  210. }
  211. async uploadFileTouch(url) {
  212. const outFilename = `${this.config.uploadDir}/${url.replace('disk://', '')}`;
  213. await utils.touchFile(outFilename);
  214. return url;
  215. }
  216. async restoreRemoteFile(filename, remoteDir) {
  217. let targetDir = '';
  218. if (this.dirConfig[remoteDir])
  219. targetDir = this.dirConfig[remoteDir].dir;
  220. else
  221. throw new Error(`restoreRemoteFile: unknown remoteDir value (${remoteDir})`);
  222. const basename = path.basename(filename);
  223. const targetName = `${targetDir}/${basename}`;
  224. if (!await fs.pathExists(targetName)) {
  225. let found = false;
  226. if (this.remoteStorage) {
  227. found = await this.remoteStorage.getFileSuccess(targetName, remoteDir);
  228. }
  229. if (!found) {
  230. throw new Error('404 Файл не найден');
  231. }
  232. }
  233. return targetName;
  234. }
  235. pushRemoteSend(fileName, remoteDir) {
  236. if (this.remoteStorage
  237. && this.dirConfig[remoteDir]
  238. && this.dirConfig[remoteDir].moveToRemote) {
  239. this.remoteFilesToSend.push({fileName, remoteDir});
  240. }
  241. }
  242. async remoteSendFile(sendFileRec) {
  243. const {fileName, remoteDir} = sendFileRec;
  244. const sent = this.remoteSent;
  245. if (!fileName || sent[fileName])
  246. return;
  247. log(`remoteSendFile ${remoteDir}/${path.basename(fileName)}`);
  248. //отправляем в remoteStorage
  249. await this.remoteStorage.putFile(fileName, remoteDir);
  250. sent[fileName] = true;
  251. await this.appDb.insert({table: 'remote_sent', ignore: true, rows: [{id: fileName, remoteDir}]});
  252. }
  253. async remoteSendAll() {
  254. if (!this.remoteStorage)
  255. return;
  256. const newSendQueue = [];
  257. while (this.remoteFilesToSend.length) {
  258. const sendFileRec = this.remoteFilesToSend.shift();
  259. if (sendFileRec.remoteDir
  260. && this.dirConfig[sendFileRec.remoteDir]
  261. && this.dirConfig[sendFileRec.remoteDir].moveToRemote) {
  262. try {
  263. await this.remoteSendFile(sendFileRec);
  264. } catch (e) {
  265. newSendQueue.push(sendFileRec)
  266. log(LM_ERR, e.stack);
  267. }
  268. }
  269. }
  270. this.remoteFilesToSend = newSendQueue;
  271. }
  272. async cleanDir(config) {
  273. const {dir, remoteDir, maxSize, moveToRemote} = config;
  274. const sent = this.remoteSent;
  275. const list = await fs.readdir(dir);
  276. let size = 0;
  277. let files = [];
  278. for (const filename of list) {
  279. const filePath = `${dir}/${filename}`;
  280. const stat = await fs.stat(filePath);
  281. if (!stat.isDirectory()) {
  282. size += stat.size;
  283. files.push({name: filePath, stat});
  284. }
  285. }
  286. log(LM_WARN, `clean dir ${dir}, maxSize=${maxSize}, found ${files.length} files, total size=${size}`);
  287. files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs);
  288. //удаленное хранилище
  289. if (moveToRemote && this.remoteStorage) {
  290. const foundFiles = new Set();
  291. for (const file of files) {
  292. foundFiles.add(file.name);
  293. //отсылаем на всякий случай перед удалением, если вдруг remoteSendAll не справился
  294. try {
  295. await this.remoteSendFile({fileName: file.name, remoteDir});
  296. } catch (e) {
  297. log(LM_ERR, e.stack);
  298. }
  299. }
  300. //почистим remoteSent и БД
  301. //несколько неоптимально, таскает все записи из таблицы
  302. const rows = await this.appDb.select({table: 'remote_sent'});
  303. for (const row of rows) {
  304. if ((row.remoteDir === remoteDir && !foundFiles.has(row.id))
  305. || !this.dirConfig[row.remoteDir]) {
  306. delete sent[row.id];
  307. await this.appDb.delete({table: 'remote_sent', where: `@@id(${this.appDb.esc(row.id)})`});
  308. }
  309. }
  310. }
  311. let i = 0;
  312. let j = 0;
  313. while (i < files.length && size > maxSize) {
  314. const file = files[i];
  315. const oldFile = file.name;
  316. //реально удаляем только если сохранили в хранилище или размер dir увеличен в 1.5 раза
  317. if (!(moveToRemote && this.remoteStorage)
  318. || (moveToRemote && this.remoteStorage && sent[oldFile])
  319. || size > maxSize*1.5) {
  320. await fs.remove(oldFile);
  321. j++;
  322. }
  323. size -= file.stat.size;
  324. i++;
  325. }
  326. log(LM_WARN, `removed ${j} files`);
  327. }
  328. async periodicCleanDir() {
  329. try {
  330. if (!this.remoteSent)
  331. this.remoteSent = {};
  332. //инициализация this.remoteSent
  333. if (this.remoteStorage) {
  334. const rows = await this.appDb.select({table: 'remote_sent'});
  335. for (const row of rows) {
  336. this.remoteSent[row.id] = true;
  337. }
  338. }
  339. let lastCleanDirTime = 0;
  340. let lastRemoteSendTime = 0;
  341. while (1) {// eslint-disable-line no-constant-condition
  342. //отсылка в удаленное хранилище
  343. if (Date.now() - lastRemoteSendTime >= remoteSendPeriod) {
  344. try {
  345. await this.remoteSendAll();
  346. } catch(e) {
  347. log(LM_ERR, e.stack);
  348. }
  349. lastRemoteSendTime = Date.now();
  350. }
  351. //чистка папок
  352. if (Date.now() - lastCleanDirTime >= cleanDirPeriod) {
  353. for (const config of Object.values(this.dirConfig)) {
  354. try {
  355. await this.cleanDir(config);
  356. } catch(e) {
  357. log(LM_ERR, e.stack);
  358. }
  359. }
  360. lastCleanDirTime = Date.now();
  361. }
  362. await utils.sleep(60*1000);//интервал проверки 1 минута
  363. }
  364. } catch (e) {
  365. log(LM_FATAL, e.message);
  366. ayncExit.exit(1);
  367. }
  368. }
  369. }
  370. module.exports = ReaderWorker;