|
|
@@ -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);
|