--- /dev/null
+import { join } from "path";
+import { DynamicThreadPool, FixedThreadPool } from "poolifier";
+import { MyData, MyResponse } from "./worker";
+
+export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
+ 8,
+ join(__dirname, "worker.js"),
+ {
+ errorHandler: (e) => console.error(e),
+ onlineHandler: () => console.log("Worker is online"),
+ }
+);
+
+export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
+ 2,
+ 8,
+ join(__dirname, "worker.js"),
+ {
+ errorHandler: (e) => console.error(e),
+ onlineHandler: () => console.log("Worker is online"),
+ }
+);
--- /dev/null
+import { ThreadWorker } from "poolifier";
+
+export interface MyData {
+ ok: number;
+}
+
+export interface MyResponse {
+ message: string;
+ data: MyData;
+}
+
+class MyThreadWorker extends ThreadWorker<MyData, Promise<MyResponse>> {
+ constructor() {
+ super((data) => this.process(data), {
+ maxInactiveTime: 60_000,
+ async: true,
+ });
+ }
+
+ private async process(data: MyData): Promise<MyResponse> {
+ return new Promise((resolve) => {
+ setTimeout(() => {
+ resolve({ message: "Hello from Worker :)", data });
+ }, 10_000);
+ });
+ }
+}
+
+export default new MyThreadWorker();