From daa0226b0a2ae1d19b342c2b7a54267d2fd8d30e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 28 Jun 2022 09:09:03 +0200 Subject: Lazy load piscina --- server/lib/worker/parent-process.ts | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'server/lib/worker') diff --git a/server/lib/worker/parent-process.ts b/server/lib/worker/parent-process.ts index 65300bbc8..4bc7f2620 100644 --- a/server/lib/worker/parent-process.ts +++ b/server/lib/worker/parent-process.ts @@ -1,28 +1,36 @@ import { join } from 'path' import Piscina from 'piscina' +import { processImage } from '@server/helpers/image-utils' import { WORKER_THREADS } from '@server/initializers/constants' import { downloadImage } from './workers/image-downloader' -import { processImage } from '@server/helpers/image-utils' -const downloadImagerWorker = new Piscina({ - filename: join(__dirname, 'workers', 'image-downloader.js'), - concurrentTasksPerWorker: WORKER_THREADS.DOWNLOAD_IMAGE.CONCURRENCY, - maxThreads: WORKER_THREADS.DOWNLOAD_IMAGE.MAX_THREADS -}) +let downloadImageWorker: Piscina function downloadImageFromWorker (options: Parameters[0]): Promise> { - return downloadImagerWorker.run(options) + if (!downloadImageWorker) { + downloadImageWorker = new Piscina({ + filename: join(__dirname, 'workers', 'image-downloader.js'), + concurrentTasksPerWorker: WORKER_THREADS.DOWNLOAD_IMAGE.CONCURRENCY, + maxThreads: WORKER_THREADS.DOWNLOAD_IMAGE.MAX_THREADS + }) + } + + return downloadImageWorker.run(options) } // --------------------------------------------------------------------------- -const processImageWorker = new Piscina({ - filename: join(__dirname, 'workers', 'image-processor.js'), - concurrentTasksPerWorker: WORKER_THREADS.PROCESS_IMAGE.CONCURRENCY, - maxThreads: WORKER_THREADS.PROCESS_IMAGE.MAX_THREADS -}) +let processImageWorker: Piscina function processImageFromWorker (options: Parameters[0]): Promise> { + if (!processImageWorker) { + processImageWorker = new Piscina({ + filename: join(__dirname, 'workers', 'image-processor.js'), + concurrentTasksPerWorker: WORKER_THREADS.PROCESS_IMAGE.CONCURRENCY, + maxThreads: WORKER_THREADS.PROCESS_IMAGE.MAX_THREADS + }) + } + return processImageWorker.run(options) } -- cgit v1.2.3