stefanzweifel / sidecar-browsershot

A Sidecar function to run Browsershot on Lambda.
https://stefanzweifel.dev/posts/2022/06/21/introducing-sidecar-browsershot
MIT License
196 stars 26 forks source link

LambdaExecutionException when calling function #135

Open lbattaglioli2000 opened 15 hours ago

lbattaglioli2000 commented 15 hours ago

I just pulled this package into a Laravel Zero CLI App I'm building, and it seems to be failing to take the screenshots and I'm not sure why.

I can confirm my function is in fact deployed to AWS Lambda.

I've included the error I'm getting when I attempt to run the screenshot. Any help would be appreciated. Not sure if this is a bug, or just skill issue. Happy to provide additional info.

This is the code that is throwing the error:

// Take a screenshot of the PDF generation page at the Build URL for this size
        $screenshot = BrowsershotLambda::url($this->getBuildUrl() . '/_pdf-generator')
            ->usePipe()
            ->waitUntilNetworkIdle()
            ->fullPage()
            ->windowSize(2500, 1200)
            ->setOption('addStyleTag', json_encode(['content' => 'body{ padding: 10px 50px 10px 50px; }']))
            ->deviceScaleFactor(3)
            ->save($this->getPdfGenerationDirectory() . '/' . $this . '.png');
❯ php deevoted banner:screenshot
Generating a PDF for Banner Ad: Option 1

   Hammerstone\Sidecar\Exceptions\LambdaExecutionException

  Lambda Execution Exception for Wnx\SidecarBrowsershot\Functions\BrowsershotFunction: "Command failed: node ./browser.cjs '-f file:///tmp/browsershot.js'
TargetCloseError: Protocol error (Page.navigate): Target closed
    at CallbackRegistry.clear (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:146:36)
    at CDPSessionImpl._onClosed (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:500:25)
    at #onClose (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:292:21)
    at Socket.<anonymous> (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/node/PipeTransport.js:24:34)
    at Socket.emit (node:events:529:35)
    at Pipe.<anonymous> (node:net:350:12) {
  cause: ProtocolError
      at <instance_members_initializer> (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:49:14)
      at new Callback (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:53:16)
      at CallbackRegistry.create (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:93:26)
      at Connection._rawSend (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:215:26)
      at CDPSessionImpl.send (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:465:33)
      at navigate (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Frame.js:170:51)
      at CDPFrame.goto (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Frame.js:148:17)
      at CDPFrame.<anonymous> (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/util/decorators.js:62:27)
      at CDPPage.goto (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Page.js:674:39)
      at callChrome (/var/task/browser.cjs:277:37)
}
. [TRACE] Error: Command failed: node ./browser.cjs '-f file:///tmp/browsershot.js' TargetCloseError: Protocol error (Page.navigate): Target closed at CallbackRegistry.clear (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:146:36)".

  at vendor/hammerstone/sidecar/src/Results/SettledResult.php:103
     99▕         if (!$this->isError()) {
    100▕             return $this;
    101▕         }
    102▕
  ➜ 103▕         throw new LambdaExecutionException(sprintf('Lambda Execution Exception for %s: "%s".', ...[
    104▕             get_class($this->function),
    105▕             $this->errorAsString()
    106▕         ]));
    107▕     }

      +3 vendor frames
  4   app/Classes/BannerAds/Size.php:150
      Spatie\Browsershot\Browsershot::save("Banners/public_html/clomid/cose0014/banner1/728x90/_pdf-generator/728x90.png")

  5   app/Classes/BannerAds/BannerAd.php:124
      App\Classes\BannerAds\Size::generatePdfScreenshot()
stefanzweifel commented 14 hours ago

A first silly question would be, if the final URL generated by $this->getBuildUrl() is accessible to the internet. The Lambda function is deployed to AWS and wouldn't be able to access a http://localhost-URL; but as you're using this in a Laravel Zero app, this shouldn't be an issue.

The "Target closed" error rings a bell. We had a previous issue regarding this in #100. As usual with Puppeteer, it's hard to debug and find a definitive solution.

Some quick checks you could do:

lbattaglioli2000 commented 10 hours ago

Hahaha, yeah the build URLs are all publicly accessible.

Yessir, I just installed both packages using the latest versions of each!

The HTML on the PDF generator is fairly small. It's pretty much just a couple images on the page, and a snippet of text.

Nope! I'm just trying to take a PNG screenshot of the full web page (as plain old HTML and CSS). Once I have the PNG screenshots, the CLI I'm building compiles the images into a PDF using DomPDF. But I don't think that has anything to do with this, as that should take place after I get the images back from the Lambda function. 🤔

lbattaglioli2000 commented 7 hours ago

Oh wait, I'm a goofy goober.

I wasn't actually on the latest version. I was on an old version of Browsershot, and composer was pulling in an older version of this package as a result.

However, even after updating I'm still getting this error 😢

   Hammerstone\Sidecar\Exceptions\LambdaExecutionException

  Lambda Execution Exception for Wnx\SidecarBrowsershot\Functions\BrowsershotFunction: "Command failed: node ./browser.cjs '-f file:///tmp/browsershot.js'
TargetCloseError: Protocol error (Page.navigate): Target closed
    at CallbackRegistry.clear (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:146:36)
    at CDPSessionImpl._onClosed (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:500:25)
    at #onClose (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:292:21)
    at Socket.<anonymous> (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/node/PipeTransport.js:24:34)
    at Socket.emit (node:events:529:35)
    at Pipe.<anonymous> (node:net:350:12) {
  cause: ProtocolError
      at <instance_members_initializer> (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:49:14)
      at new Callback (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:53:16)
      at CallbackRegistry.create (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:93:26)
      at Connection._rawSend (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:215:26)
      at CDPSessionImpl.send (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:465:33)
      at navigate (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Frame.js:170:51)
      at CDPFrame.goto (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Frame.js:148:17)
      at CDPFrame.<anonymous> (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/util/decorators.js:62:27)
      at CDPPage.goto (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Page.js:674:39)
      at callChrome (/var/task/browser.cjs:277:37)
}
. [TRACE] Error: Command failed: node ./browser.cjs '-f file:///tmp/browsershot.js' TargetCloseError: Protocol error (Page.navigate): Target closed at CallbackRegistry.clear (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:146:36)".

If it helps, here's my composer.json file too:

{
    "name": "deevoted/deevoted-cli",
    "description": "DeevotedCLI is a command line utility to leverage the Stepwise API to speed up your development workflow, and make client handoff a breeze.",
    "keywords": ["framework", "laravel", "laravel zero", "console", "cli"],
    "homepage": "https://stepwise.deevoted.build",
    "type": "project",
    "license": "MIT",
    "support": {
        "issues": "https://github.com/DeevotedLLC/DeevotedCLI-v2/issues",
        "source": "https://github.com/DeevotedLLC/DeevotedCLI-v2"
    },
    "authors": [
        {
            "name": "Luigi Battaglioli",
            "email": "luigi@deevoted.com"
        }
    ],
    "require": {
        "php": "^8.1",
        "ext-imagick": "*",
        "ext-zip": "*",
        "gmostafa/php-graphql-client": "^1.13",
        "gmostafa/php-graphql-oqm": "^1.4",
        "guzzlehttp/guzzle": "^7.5",
        "hammerstone/sidecar": "^0.4.2",
        "illuminate/http": "^10.0",
        "illuminate/support": "^10.0",
        "laminas/laminas-text": "^2.10",
        "laravel-zero/phar-updater": "^1.3",
        "nunomaduro/laravel-console-menu": "^3.4",
        "nunomaduro/termwind": "^1.15",
        "pusher/pusher-php-server": "^7.2",
        "ratchet/pawl": "^0.4.1",
        "react/event-loop": "^1.3",
        "spatie/browsershot": "^4.0.0",
        "spatie/image": "^2.2",
        "spatie/ray": "^1.37",
        "spatie/ssh": "^1.8",
        "wnx/sidecar-browsershot": "^v2.0.0"
    },
    "require-dev": {
        "laravel-zero/framework": "^10.0",
        "laravel/pint": "^1.5",
        "mockery/mockery": "^1.5.1",
        "pestphp/pest": "^1.22.3"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "GraphQL\\SchemaObject\\": "app/GraphQL/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true,
    "bin": ["builds/deevoted"]
}