2022-06-24 16:31:32 +02:00
|
|
|
import { join } from 'path'
|
|
|
|
import Piscina from 'piscina'
|
2022-06-28 09:09:03 +02:00
|
|
|
import { processImage } from '@server/helpers/image-utils'
|
2022-11-16 14:40:10 +01:00
|
|
|
import { JOB_CONCURRENCY, WORKER_THREADS } from '@server/initializers/constants'
|
2022-09-08 12:26:46 +02:00
|
|
|
import { httpBroadcast } from './workers/http-broadcast'
|
2022-06-24 16:31:32 +02:00
|
|
|
import { downloadImage } from './workers/image-downloader'
|
|
|
|
|
2022-06-28 09:09:03 +02:00
|
|
|
let downloadImageWorker: Piscina
|
2022-06-24 16:31:32 +02:00
|
|
|
|
|
|
|
function downloadImageFromWorker (options: Parameters<typeof downloadImage>[0]): Promise<ReturnType<typeof downloadImage>> {
|
2022-06-28 09:09:03 +02: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-24 16:31:32 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 11:53:12 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-06-28 09:09:03 +02:00
|
|
|
let processImageWorker: Piscina
|
2022-06-27 11:53:12 +02:00
|
|
|
|
|
|
|
function processImageFromWorker (options: Parameters<typeof processImage>[0]): Promise<ReturnType<typeof processImage>> {
|
2022-06-28 09:09:03 +02: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 11:53:12 +02:00
|
|
|
return processImageWorker.run(options)
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:26:46 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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'),
|
2022-11-16 14:40:10 +01:00
|
|
|
// Keep it sync with job concurrency so the worker will accept all the requests sent by the parallelized jobs
|
|
|
|
concurrentTasksPerWorker: JOB_CONCURRENCY['activitypub-http-broadcast-parallel'],
|
|
|
|
maxThreads: 1
|
2022-09-08 12:26:46 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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'),
|
2022-11-16 14:40:10 +01:00
|
|
|
// Keep it sync with job concurrency so the worker will accept all the requests sent by the parallelized jobs
|
|
|
|
concurrentTasksPerWorker: JOB_CONCURRENCY['activitypub-http-broadcast'],
|
|
|
|
maxThreads: 1
|
2022-09-08 12:26:46 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return sequentialHTTPBroadcastWorker.run(options)
|
|
|
|
}
|
|
|
|
|
2022-06-24 16:31:32 +02:00
|
|
|
export {
|
2022-06-27 11:53:12 +02:00
|
|
|
downloadImageFromWorker,
|
2022-09-08 12:26:46 +02:00
|
|
|
processImageFromWorker,
|
|
|
|
parallelHTTPBroadcastFromWorker,
|
|
|
|
sequentialHTTPBroadcastFromWorker
|
2022-06-24 16:31:32 +02:00
|
|
|
}
|