javascriptphplaravelpuppeteerbrowsershot

Browsershot / Puppeteer overloads CPU and times out


I have been using Browsershot to retrieve some images from my website without any issues for months, but recently (past 1-2 weeks), the request has been timing out. Looking further into it, I've found that it has been consuming a large amount of cpu power while trying to execute the command.

Browsershot CPU Usage

As far as I can tell, this constant CPU drain will continue until I restart the server. I also only requested for one image to be made in this example, yet there are three processes of chrome. I'm not sure if that's irregular.

In order to make sure that it wasn't something on my website or possibly a setting causing this, I used a simple function call.

    Browsershot::url("https://example.com")
        ->setScreenshotType('png')
        ->save(Storage::path("overview_images/test.png"));

Unfortunately the problem still occurs.

I am using a Laravel provisioned Forge server, so I used the installation recommended by the Github page. Like I said, it had been working fine up until a couple of weeks ago. No changes were made to Browsershot around that time that would cause something like this. I have tried following the steps to install again, but it still happens.

Any help is greatly appreciated!

Edit 1:

After looking into it further, I've figured out that the problem lies within Puppeteer, not Browsershot. I tried implementing the example.js provided by Google, and I ran into the exact same problem.

Edit 2:

Somehow I was able to get it fixed. I can't tell exactly what did it, but I may be able to offer some help to anyone else who may find this question.

Running ndb example.js, I got the message

Downloading Chromium r624492...
ERROR: Failed to download Chromium r624492!
Error: EACCES: permission denied

Even though I had installed puppeteer (multiple times), I guess it was continuously trying and failing to download Chromium. I assume this is why the CPU usage was so high and never stopped until the server was restarted.

With that said, I didn't change anything specifically that made it suddenly start working. If it helps at all, this was a series of commands that I used when it suddenly started to work.

> node example.js // Didn't Work. Would be in continuous command with high CPU usage until cancelled
> sudo npm install -g ndb
> ndb example.js // This is when I received the message about downloading Chomium
> node example.js // It worked ?

Still don't know the exact reason for why this is happening, so I'll leave this as an open question if anybody can figure that out.

Edit 3:

It broke again.


Solution

  • I think I've finally figured it out.

    From Browsershot's Github page, it recommends executing these commands to install puppeteer.

    curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
    sudo apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev libxshmfence-dev
    sudo npm install --global --unsafe-perm puppeteer
    sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium
    

    The npm install fails installing the chromium part of puppeteer despite the --unsafe-perm.

    Changing --unsafe-perm to --unsafe-perm=true seems to have fixed it.

    So changing the command to the following should fix the install and actually download Chromium.

    sudo npm install -g puppeteer --unsafe-perm=true