0

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?

1 Answer 1

0

process.send() is a function that's used in Node.js processes that were created using child_process.fork(). This function allows child processes to communicate back to their parent process. In your scenario, you're encountering an error because you're using worker_threads, rather than child_process. The worker threads module doesn't have the process.send() function because worker threads communicate with their main thread through a different mechanism, specifically using parentPort.PostMessage()

https://nodejs.org/api/worker_threads.html

https://nodejs.org/api/child_process.html

The call to process.send() could be in Jimp or imageStorage, if that's the case, you may need to change to using child process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.