Browse Source

ReaderWorker теперь синглтон

Book Pauk 5 năm trước cách đây
mục cha
commit
ca65ef3cb7
1 tập tin đã thay đổi với 17 bổ sung13 xóa
  1. 17 13
      server/core/ReaderWorker.js

+ 17 - 13
server/core/ReaderWorker.js

@@ -8,28 +8,32 @@ const BookConverter = require('./BookConverter');
 const utils = require('./utils');
 const log = new (require('./AppLogger'))().log;//singleton
 
-let singleCleanExecute = false;
+let instance = null;
 
+//singleton
 class ReaderWorker {
     constructor(config) {
-        this.config = Object.assign({}, config);
-        
-        this.config.tempDownloadDir = `${config.tempDir}/download`;
-        fs.ensureDirSync(this.config.tempDownloadDir);
+        if (!instance) {
+            this.config = Object.assign({}, config);
+            
+            this.config.tempDownloadDir = `${config.tempDir}/download`;
+            fs.ensureDirSync(this.config.tempDownloadDir);
 
-        this.config.tempPublicDir = `${config.publicDir}/tmp`;
-        fs.ensureDirSync(this.config.tempPublicDir);
+            this.config.tempPublicDir = `${config.publicDir}/tmp`;
+            fs.ensureDirSync(this.config.tempPublicDir);
 
-        this.workerState = new WorkerState();
-        this.down = new FileDownloader();
-        this.decomp = new FileDecompressor();
-        this.bookConverter = new BookConverter(this.config);
+            this.workerState = new WorkerState();
+            this.down = new FileDownloader();
+            this.decomp = new FileDecompressor();
+            this.bookConverter = new BookConverter(this.config);
 
-        if (!singleCleanExecute) {
             this.periodicCleanDir(this.config.tempPublicDir, this.config.maxTempPublicDirSize, 60*60*1000);//1 раз в час
             this.periodicCleanDir(this.config.uploadDir, this.config.maxUploadPublicDirSize, 60*60*1000);//1 раз в час
-            singleCleanExecute = true;
+            
+            instance = this;
         }
+
+        return instance;
     }
 
     async loadBook(opts, wState) {