-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'
+import {
+ availableParallelism,
+ DynamicThreadPool,
+ FixedThreadPool,
+} from 'poolifier'
-export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
- 8,
- join(__dirname, "worker.js"),
+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<MyData, MyResponse>(
+ availableParallelism(),
+ workerFile,
{
- errorHandler: (e) => console.error(e),
- onlineHandler: () => console.log("Worker is online"),
+ errorHandler: (e: Error) => {
+ console.error(e)
+ },
+ onlineHandler: () => {
+ console.info('Worker is online')
+ },
}
-);
+)
+
+await fixedPool.execute()
-export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
- 2,
- 8,
- join(__dirname, "worker.js"),
+const dynamicPool = new DynamicThreadPool<MyData, MyResponse>(
+ Math.floor(availableParallelism() / 2),
+ availableParallelism(),
+ workerFile,
{
- errorHandler: (e) => console.error(e),
- onlineHandler: () => console.log("Worker is online"),
+ errorHandler: (e: Error) => {
+ console.error(e)
+ },
+ onlineHandler: () => {
+ console.info('Worker is online')
+ },
}
-);
+)
+
+await dynamicPool.execute()
+
+// eslint-disable-next-line @typescript-eslint/no-misused-promises
+setTimeout(async () => {
+ await fixedPool.destroy()
+ await dynamicPool.destroy()
+}, 3000)