From f134d74b1336ebf170c901320ccd0c8a0c94eb60 Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Sat, 21 Jan 2017 22:54:43 +0100 Subject: [PATCH] implementing basic thread pooling for thumbnail generation --- backend/middlewares/ThumbnailGeneratorMWs.ts | 63 +++++++++++++------- package.json | 1 + 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/backend/middlewares/ThumbnailGeneratorMWs.ts b/backend/middlewares/ThumbnailGeneratorMWs.ts index eaf182b5..b08b7225 100644 --- a/backend/middlewares/ThumbnailGeneratorMWs.ts +++ b/backend/middlewares/ThumbnailGeneratorMWs.ts @@ -1,6 +1,5 @@ /// import * as path from "path"; -import * as Jimp from "jimp"; import * as crypto from "crypto"; import * as fs from "fs"; import {NextFunction, Request, Response} from "express"; @@ -11,6 +10,38 @@ import {DirectoryDTO} from "../../common/entities/DirectoryDTO"; import {ProjectPath} from "../ProjectPath"; import {PhotoDTO} from "../../common/entities/PhotoDTO"; +const Pool = require('threads').Pool; +const pool = new Pool(); + +pool.run( + (input: {imagePath: string, size: number, thPath: string}, done) => { + + //generate thumbnail + let Jimp = require("jimp"); + Jimp.read(input.imagePath).then((image) => { + /** + * newWidth * newHeight = size*size + * newHeight/newWidth = height/width + * + * newHeight = (height/width)*newWidth + * newWidth * newWidth = (size*size) / (height/width) + * + * @type {number} + */ + let ratio = image.bitmap.height / image.bitmap.width; + let newWidth = Math.sqrt((input.size * input.size) / ratio); + + image.resize(newWidth, Jimp.AUTO, Jimp.RESIZE_BEZIER); + + image.quality(60); // set JPEG quality + image.write(input.thPath, () => { // save + return done(); + }); + }).catch(function (err) { + return done(new Error(ErrorCodes.GENERAL_ERROR)); + }); + } +); export class ThumbnailGeneratorMWs { @@ -93,30 +124,18 @@ export class ThumbnailGeneratorMWs { fs.mkdirSync(ProjectPath.ThumbnailFolder); } - //generate thumbnail - Jimp.read(imagePath).then((image) => { - /** - * newWidth * newHeight = size*size - * newHeight/newWidth = height/width - * - * newHeight = (height/width)*newWidth - * newWidth * newWidth = (size*size) / (height/width) - * - * @type {number} - */ - let ratio = image.bitmap.height / image.bitmap.width; - let newWidth = Math.sqrt((size * size) / ratio); - image.resize(newWidth, Jimp.AUTO, Jimp.RESIZE_BEZIER); - - image.quality(60); // set JPEG quality - image.write(thPath, () => { // save - return next(); - }); - }).catch(function (err) { - return next(new Error(ErrorCodes.GENERAL_ERROR)); + //run on other thread + pool.send({imagePath: imagePath, size: size, thPath: thPath}) + .on('done', (out) => { + return next(out); + }).on('error', (job, error) => { + return next(new Error(ErrorCodes.GENERAL_ERROR, error)); }); + + + } private static generateThumbnailName(imagePath: string, size: number): string { diff --git a/package.json b/package.json index 0001ba2a..e1b80dec 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "reflect-metadata": "^0.1.9", "rxjs": "^5.0.2", "systemjs": "0.19.41", + "threads": "^0.7.2", "typeorm": "0.0.6", "zone.js": "^0.7.4" },