I have a test1.ts file where I was trying to offload some CPU-intensive image manipulation work to worker threads. Below is the code shown for file test1.ts and also its invocation at another file. The project is a nodejs expressjs web server.
test1.ts file
const { Worker, workerData,isMainThread, parentPort } = require("worker_threads");
const { generateUrls } = require("./util");
const Jimp = require("jimp");
const { uploadImage } = require("../imageStorage");
if (!isMainThread){
mainWork()
.finally(() => {
parentPort?.postMessage({});
})
}
async function mainWork() {
const { accountId, imageBuffer, imageNames, docUrls, meta } = workerData;
try {
const { imageIndex, imageType } = meta;
const imageName = imageNames.imageName + `_${imageType}_` + `${imageIndex || ""}` + ".png";
docUrls[imageType === "heatmap" ? "heatmapUrls" : "normalUrls"][imageIndex] = generateUrls(
accountId,
imageName
);
const maxWidth = 764;
const maxHeight = 2000;
const image = await Jimp.read(imageBuffer);
const originalWidth = image.bitmap.width;
const originalHeight = image.bitmap.height;
// Calculate the aspect ratio
const aspectRatio = originalWidth / originalHeight;
// Determine the new dimensions based on the desired maximum width and height
let newWidth = originalWidth;
let newHeight = originalHeight;
if (originalWidth > maxWidth) {
newWidth = maxWidth;
newHeight = maxWidth / aspectRatio;
}
if (newHeight > maxHeight) {
newHeight = maxHeight;
newWidth = maxHeight * aspectRatio;
}
image.resize(newWidth, newHeight);
image.quality(100);
const newBuffer = await image.getBufferAsync(Jimp.MIME_PNG);
await uploadImage(newBuffer, accountId, imageName);
return { imageName, docUrls }; // Return the result
} catch (error) {
console.error("Error resizing and converting image:", error);
return Promise.reject(error); // Reject the Promise with the error
}
}
function imageResizeCropUpload(
accountId: any,
imageBuffer: any,
imageNames: any,
docUrls: any,
meta: any
): Promise<void> {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: {
accountId,
imageBuffer,
imageNames,
docUrls,
meta
}
});
worker.on("message", (result:any) => {
resolve(result); // Resolve the Promise with the result
});
worker.on("error", (e:any) => {
reject(e);
});
worker.on("exit", (code:any) => {
if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
});
});
}
export default imageResizeCropUpload
SomeOtherfile.ts
import imageResizeCropUpload from './test1'
uploadPromises.push(
imageResizeCropUpload(accountId, buffers[0], imageNames, docUrls, {
imageIndex: 0,
imageType: "heatmap"
})
);
Now what is happening I don't know, but all I am getting is "Worker stopped with exit code 1" with an error as process.send() is not a function. I looked online but couldn't find anything. I am using worker threads first time. What can I try next?