X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fpool.ts;h=3965e1f0a1cd4c8e2632b59018972e79fa5b798e;hb=15f12f77ca67758e32afdf2c5658075d6beac912;hp=8e7d4bb7fd981c3bc1e01b80e716374e70187c53;hpb=e9e162504b02f053c805c43e7cd5a4a340730970;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 8e7d4bb7..3965e1f0 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,22 +1,52 @@ -import { join } from "path"; -import { DynamicThreadPool, FixedThreadPool } from "poolifier"; -import { MyData, MyResponse } from "./worker"; +import { dirname, extname, join } from 'node:path' +import { fileURLToPath } from 'node:url' -export const fixedPool = new FixedThreadPool>( - 8, - join(__dirname, "worker.js"), +import { + availableParallelism, + DynamicThreadPool, + FixedThreadPool +} from 'poolifier' + +import type { MyData, MyResponse } from './worker.js' + +const workerFile = join( + dirname(fileURLToPath(import.meta.url)), + `worker${extname(fileURLToPath(import.meta.url))}` +) + +const fixedPool = new FixedThreadPool( + availableParallelism(), + workerFile, { - errorHandler: (e) => console.error(e), - onlineHandler: () => console.log("Worker is online"), + onlineHandler: () => { + console.info('Worker is online') + }, + errorHandler: (e: Error) => { + console.error(e) + } } -); +) -export const dynamicPool = new DynamicThreadPool>( - 2, - 8, - join(__dirname, "worker.js"), +await fixedPool.execute() + +const dynamicPool = new DynamicThreadPool( + Math.floor(availableParallelism() / 2), + availableParallelism(), + workerFile, { - errorHandler: (e) => console.error(e), - onlineHandler: () => console.log("Worker is online"), + onlineHandler: () => { + console.info('Worker is online') + }, + errorHandler: (e: Error) => { + console.error(e) + } } -); +) + +await dynamicPool.execute() + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +setTimeout(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() +}, 3000)