Back

[SOLVED] Using Puppeteer in a Function?

  • 0
  • Functions
Drake
11 Dec, 2023, 22:48

execSync

TL;DR
Issue: The user is facing an issue with using Puppeteer in a function. The function fails at creating the PDF in storage. They mention trying different solutions like updating the code and adding a try-catch block. They also mention the possibility of the wrong mimetype and the execution time of the function. Solution: The user should make sure to install the dependencies at runtime in the function. They should also consider executing the functions asynchronously.
ZachHandley
11 Dec, 2023, 22:48

I don't understand, that runs the install command no?

ZachHandley
11 Dec, 2023, 22:49

Mine is in the build process so it's always there is the thought, why would that matter here?

Drake
11 Dec, 2023, 22:50

no it's not.

Drake
11 Dec, 2023, 22:50

and it needs to be done in the function

ZachHandley
11 Dec, 2023, 22:50

huh, oky

Drake
11 Dec, 2023, 22:51

the build container is not the same container where your function gets exeucted. Also, the container can be removed and the nrecreated. That's why you always need to make sure to install the dependencies at runtime

ZachHandley
11 Dec, 2023, 22:51

interesting, lemme try this rq

ZachHandley
11 Dec, 2023, 22:52

see my issue with the one executor is a perfect example here

ZachHandley
11 Dec, 2023, 22:52

a PDF generation thus far has taken 2s, if 1000 people want one, I can't have it as an Appwrite Function no?

ZachHandley
11 Dec, 2023, 22:53

oof 9s to successfully generate one

Drake
11 Dec, 2023, 22:53

if it always takes less than 30x, you can execute the function synchronously and they should all execute concurrently. However, it's probably best to execute the functions asynchronously

Drake
11 Dec, 2023, 22:53

is that the 1st execution of the function?

ZachHandley
11 Dec, 2023, 22:54

yeah, oh true

ZachHandley
11 Dec, 2023, 22:54

the actual execution took 7 seconds I believe

ZachHandley
11 Dec, 2023, 22:54

including install

ZachHandley
11 Dec, 2023, 22:54

so lemme try again

ZachHandley
11 Dec, 2023, 22:54

oh yeah

ZachHandley
11 Dec, 2023, 23:03

so it's failing at creating the PDF in storage for some reason

ZachHandley
11 Dec, 2023, 23:03

fails here

TypeScript
log(`Ending Puppeteer timestamp (seconds): ${Date.now() / 1000}`);
    const fileToUpload = InputFile.fromBuffer(pdf, `document_${new Date.now()}_upload.pdf`);
    log(`File to upload created, trying to create file in Storage`);
    const file = await storage.createFile(
      "my_storage_id",
      ID.unique(),
      fileToUpload,
    )
ZachHandley
11 Dec, 2023, 23:04

could it be that it's getting the wrong mimetype? it has no error that it's actually logging

Drake
11 Dec, 2023, 23:13

not sure exactly...Maybe you can add a try catch.

Drake
11 Dec, 2023, 23:35

hmm how about:

TypeScript
import { Readable } from "stream";

// ...

    const stream = Readable.from(b);
    const size = Buffer.byteLength(b);
    const inputFile = InputFile.fromStream(stream, `document_${new Date.now()}_upload.pdf`, size);
ZachHandley
14 Dec, 2023, 19:09

So I got it to work doing exactly what you said yeah, forgot to update

ZachHandley
14 Dec, 2023, 19:09

[SOLVED] Using Puppeteer in a Function?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more