PeerTube/server/lib/worker/parent-process.ts

76 lines
2.7 KiB
TypeScript
Raw Normal View History

import { join } from 'path'
import Piscina from 'piscina'
2022-06-28 02:09:03 -05:00
import { processImage } from '@server/helpers/image-utils'
import { WORKER_THREADS } from '@server/initializers/constants'
import { httpBroadcast } from './workers/http-broadcast'
import { downloadImage } from './workers/image-downloader'
2022-06-28 02:09:03 -05:00
let downloadImageWorker: Piscina
function downloadImageFromWorker (options: Parameters<typeof downloadImage>[0]): Promise<ReturnType<typeof downloadImage>> {
2022-06-28 02:09:03 -05:00
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)
}
2022-06-27 04:53:12 -05:00
// ---------------------------------------------------------------------------
2022-06-28 02:09:03 -05:00
let processImageWorker: Piscina
2022-06-27 04:53:12 -05:00
function processImageFromWorker (options: Parameters<typeof processImage>[0]): Promise<ReturnType<typeof processImage>> {
2022-06-28 02:09:03 -05:00
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
})
}
2022-06-27 04:53:12 -05:00
return processImageWorker.run(options)
}
// ---------------------------------------------------------------------------
let parallelHTTPBroadcastWorker: Piscina
function parallelHTTPBroadcastFromWorker (options: Parameters<typeof httpBroadcast>[0]): Promise<ReturnType<typeof httpBroadcast>> {
if (!parallelHTTPBroadcastWorker) {
parallelHTTPBroadcastWorker = new Piscina({
filename: join(__dirname, 'workers', 'http-broadcast.js'),
concurrentTasksPerWorker: WORKER_THREADS.PARALLEL_HTTP_BROADCAST.CONCURRENCY,
maxThreads: WORKER_THREADS.PARALLEL_HTTP_BROADCAST.MAX_THREADS
})
}
return parallelHTTPBroadcastWorker.run(options)
}
// ---------------------------------------------------------------------------
let sequentialHTTPBroadcastWorker: Piscina
function sequentialHTTPBroadcastFromWorker (options: Parameters<typeof httpBroadcast>[0]): Promise<ReturnType<typeof httpBroadcast>> {
if (!sequentialHTTPBroadcastWorker) {
sequentialHTTPBroadcastWorker = new Piscina({
filename: join(__dirname, 'workers', 'http-broadcast.js'),
concurrentTasksPerWorker: WORKER_THREADS.SEQUENTIAL_HTTP_BROADCAST.CONCURRENCY,
maxThreads: WORKER_THREADS.SEQUENTIAL_HTTP_BROADCAST.MAX_THREADS
})
}
return sequentialHTTPBroadcastWorker.run(options)
}
export {
2022-06-27 04:53:12 -05:00
downloadImageFromWorker,
processImageFromWorker,
parallelHTTPBroadcastFromWorker,
sequentialHTTPBroadcastFromWorker
}