35 lines
No EOL
777 B
TypeScript
35 lines
No EOL
777 B
TypeScript
import Piscina from 'piscina';
|
|
import path from 'node:path';
|
|
import { availableParallelism } from 'node:os';
|
|
|
|
const configuredThreads = Number(
|
|
process.env.DOCX_WORKERS || 4
|
|
);
|
|
|
|
const maxThreads = Math.max(
|
|
1,
|
|
Math.min(
|
|
Number.isFinite(configuredThreads) && configuredThreads > 0
|
|
? Math.floor(configuredThreads)
|
|
: 4,
|
|
Math.max(1, availableParallelism() - 1)
|
|
)
|
|
);
|
|
|
|
const globalForDocx = globalThis as typeof globalThis & {
|
|
docxPool?: Piscina;
|
|
};
|
|
|
|
export const docxPool =
|
|
globalForDocx.docxPool ??=
|
|
new Piscina({
|
|
filename: path.resolve(
|
|
process.cwd(),
|
|
'workers/dist/docx.worker.mjs'
|
|
),
|
|
minThreads: 1,
|
|
maxThreads,
|
|
idleTimeout: 30000,
|
|
maxQueue: 'auto',
|
|
concurrentTasksPerWorker: 1
|
|
}); |