Out of Memory error with email workflow and large attachment

This topic was automatically generated from Slack. You can find the original thread here.

Walid : Hello, I hope everyone is well! I am working on a workflow that is triggered by email and takes a large attachment (using the instructions as per upload.pipedream.net) I then parse the email and upload the base64code output to s3.

It is working on some tries and then failing with the exact same variables (pdf file, email, etc) and giving me n Out of Memory error.

Steps dealing with the content_url: Trigger: Large upload email attachment (around 5 mb)

  1. Capture content:
const stream = require("stream");
const { promisify } = require("util");
const fs = require("fs");
const got = require("got");


const pipeline = promisify(stream.pipeline);

await pipeline(
  got.stream(steps.trigger.event.mail.content_url),
  fs.createWriteStream(`/tmp/raw_email`)
);

// Now read the file and parse its contents into the `parsed` variable
// See <https://nodemailer.com/extras/mailparser/> for parsing options
  1. Parse content:
const simpleParser = require("mailparser").simpleParser;
const fs = require("fs");


const f = fs.readFileSync(`/tmp/raw_email`);
this.parsed = await simpleParser(f);

The next steps are to upload to S3
and finally I attempted to use a remove files from storage code, but it does not seem to be working:

// The files stored on disk may persist in our VM for a few executions
// of our workflow. So we need to remove the files we just processed
// to make sure we don't process them again on the next run.
const fs = require("fs")


  fs.unlinkSync(`/tmp/raw_email`)
  console.log(`Deleted /tmp/raw_email`)

Can you please let me know whats the best way to solve the out of memory error

Dylan Sather (Pipedream) : would you mind clicking the Share button in the top-right of your workflow and share it with dylan@pipedream.com? I can take a look to see if I can help you optimize the memory usage, or raise the memory so you get around the error.

Walid : absolutely

Walid : Shared

Walid : Thank you So much Dylan!

Dylan Sather (Pipedream) : could you retry one of your events? Let’s see if that works

Walid : Absolutely.

Walid : Thats amazing! Thank you

Walid : Anything I could have done better to prevent that error?

Dylan Sather (Pipedream) : no in this case, since you need to read the file to parse it, you need to read the full file into memory, so no other easy way around it

Dylan Sather (Pipedream) : reach out if you ever encounter this error again and we’re happy to raise your memory for other workflows. We’re hoping to make this self-service (so you can edit the memory yourself) in the future

Walid : That is wonderful. I did see that requesting an increase was an option but I wanted to see if I could further optimize it before resorting to that. Thank you so much and I’ll keep that in mind.