1
0
isundil 1 жил өмнө
parent
commit
b89bdf296c

+ 4 - 43
src/imagemagickWrapper.js

@@ -1,51 +1,12 @@
 
 const im = require('imagemagick');
-
-const maxTasksCount = 5;
+const ThreadPool = require('./threadPool.js');
 
 class ImagemagickWrapper {
-    #tasks = [];
-    #activeTask = 0;
-
-    #runNextTask() {
-        if (this.#activeTask >= maxTasksCount || this.#tasks.length === 0)
-            return;
-        const activeTask = (this.#tasks.shift());
-        this.#activeTask++;
-        let taskResult = null;
-        try {
-            taskResult = activeTask.fnc();
-        }
-        catch (err) {
-            taskResult = Promise.reject(err);
-        }
-        if (!(taskResult instanceof Promise))
-            taskResult = Promise.resolve(activeTask);
-        taskResult
-            .then(result => {
-                let ok = activeTask.ok;
-                this.#activeTask--;
-                ok(result);
-            })
-            .catch(() => {
-                let ko = activeTask.ko;
-                this.#activeTask--;
-                ko(taskResult.result);
-            })
-            .finally(() => {
-                this.#runNextTask();
-            });
-    };
-
-    async #pushTask(task) {
-        return new Promise((ok, ko) => {
-            this.#tasks.push({ fnc: task, ok: ok, ko: ko });
-            this.#runNextTask();
-        });
-    }
+    #threadPool = new ThreadPool(5);
 
     readMeta(path) {
-        return this.#pushTask(() => new Promise((ok, ko) => {
+        return this.#threadPool.pushTask(() => new Promise((ok, ko) => {
             im.identify(['-format', '%[EXIF:*]Compression=%[compression]\nWidth=%w\nHeight=%h\n', path], (err, stdout) => {
                 if (err)
                     return ko(err);
@@ -55,7 +16,7 @@ class ImagemagickWrapper {
     }
 
     convert(args) {
-        return this.#pushTask(() => new Promise((ok, ko) => {
+        return this.#threadPool.pushTask(() => new Promise((ok, ko) => {
             im.convert(args, (err, stdout) => {
                 if (err)
                     return ko(err);

+ 4 - 4
src/library.js

@@ -56,9 +56,9 @@ File.prototype.enrich = async function() {
 File.prototype.saveDb = async function(db, libraryHash) {
     if (this.dbItem) {
         this.fixedSum = this.dbItem.fixedSum;
-        await db.remove(MediaFileModel, { path: this.dbItem.path, md5sum: this.dbItem.md5sum });
-        await db.remove(MediaFileMetaModel, { md5sum: this.dbItem.md5sum, fromFile: true });
-        await db.remove(MediaFileTagModel, { md5sum: this.dbItem.md5sum, fromMeta: true });
+        await db.remove(MediaFileModel, { path: this.dbItem.path, fixedSum: this.dbItem.fixedSum });
+        await db.remove(MediaFileMetaModel, { md5sum: this.dbItem.fixedSum, fromFile: true });
+        await db.remove(MediaFileTagModel, { md5sum: this.dbItem.fixedSum, fromMeta: true });
     }
     let entity = new MediaFileModel();
     let _this = this;
@@ -97,7 +97,7 @@ async function Library_doUpdate(dbHelper, lib) {
     await enrichAll(lib);
     (await Promise.allSettled(lib.buff.map(i => i.saveDb(dbHelper, lib.dbHash)))).forEach(x => { if (x.status === 'rejected') { console.log(`Cannot update item: `, x.reason); }});
     lib.foundMedias = lib.foundMedias.concat(lib.buff);
-    console.log(`Updated ${lib.buff.length} media items`);
+    lib.buff.length && console.log(`Updated ${lib.buff.length} media items`);
     lib.buff = [];
 }
 

+ 50 - 0
src/threadPool.js

@@ -0,0 +1,50 @@
+
+class ThreadPool {
+    #tasks = [];
+    #activeTask = 0;
+    #maxTasks = 1;
+
+    constructor(maxTasksInPool) {
+        this.#maxTasks = maxTasksInPool;
+    }
+
+    #runNextTask() {
+        if (this.#activeTask >= this.#maxTasks || this.#tasks.length === 0)
+            return;
+        const activeTask = (this.#tasks.shift());
+        this.#activeTask++;
+        let taskResult = null;
+        try {
+            taskResult = activeTask.fnc();
+        }
+        catch (err) {
+            taskResult = Promise.reject(err);
+        }
+        if (!(taskResult instanceof Promise))
+            taskResult = Promise.resolve(activeTask);
+        taskResult
+            .then(result => {
+                let ok = activeTask.ok;
+                this.#activeTask--;
+                ok(result);
+            })
+            .catch(() => {
+                let ko = activeTask.ko;
+                this.#activeTask--;
+                ko(taskResult.result);
+            })
+            .finally(() => {
+                this.#runNextTask();
+            });
+    };
+
+    pushTask(task) {
+        return new Promise((ok, ko) => {
+            this.#tasks.push({ fnc: task, ok: ok, ko: ko });
+            this.#runNextTask();
+        });
+    }
+}
+
+module.exports = ThreadPool;
+