Skip to content

Commit

Permalink
Add sidecar-browsershot config and warming support (#18)
Browse files Browse the repository at this point in the history
* Add sidecar-browsershot config file

* Use config ins BrowsershotFunction

* Add comments to config

* Document Warming
  • Loading branch information
stefanzweifel authored May 29, 2022
1 parent fd52a89 commit 82fddc9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ You can install the package via composer:
composer require wnx/sidecar-browsershot
```

You can publish the config file with:

```bash
php artisan vendor:publish --tag="sidecar-browsershot-config"
```

Register the `BrowsershotFunction::class` in your `sidecar.php` config file.

```php
Expand Down Expand Up @@ -64,6 +70,18 @@ $html = BrowsershotLambda::url('https://example.com')->bodyHtml();
Storage::disk('s3')->put('example.html', $html);
```

## Warming

sidecar-browsershot supports [warming](https://hammerstone.dev/sidecar/docs/main/functions/warming) for faster execution.

To enable this feature set the `SIDECAR_BROWSERSHOT_WARMING_INSTANCES` variable in your `.env` to the desired number of instances Sidecar should warm for you.

```shell
SIDECAR_BROWSERSHOT_WARMING_INSTANCES=5
```

Alternatively you can publish the `sidecar-browsershot.php` config file and change the `warming` setting yourself.

## Testing

The testsuite makes connections to AWS and runs the deployed Lambda function. In order to run the testsuite, you will need an active [AWS account](https://aws.amazon.com/).
Expand Down
24 changes: 24 additions & 0 deletions config/sidecar-browsershot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

return [
/**
* Define the allocated memory available to SidecarBrowsershot in megabytes. (Defaults to 2GB)
* We suggest to allocate at least 513 MB of memory to push Chrome/Puppeteer out of "low-spec" mode.
* @see https://hammerstone.dev/sidecar/docs/main/functions/customization#memory
* @see https://github.blog/2021-06-22-framework-building-open-graph-images/
*/
'memory' => env('SIDECAR_BROWSERSHOT_MEMORY', 2048),

/**
* Define the number of warming instances to boot.
* @see https://hammerstone.dev/sidecar/docs/main/functions/warming
*/
'warming' => env('SIDECAR_BROWSERSHOT_WARMING_INSTANCES', 0),

/**
* AWS Layer to use by Lambda. Defaults to "shelfio/chrome-aws-lambda-layer" in your AWS region.
* Must contain "chrome-aws-lambda".
* @see https://github.com/shelfio/chrome-aws-lambda-layer
*/
'layer' => env('SIDECAR_BROWSERSHOT_LAYER'),
];
18 changes: 13 additions & 5 deletions src/Functions/BrowsershotFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Hammerstone\Sidecar\LambdaFunction;
use Hammerstone\Sidecar\Package;
use Hammerstone\Sidecar\Runtime;
use Hammerstone\Sidecar\WarmingConfig;

class BrowsershotFunction extends LambdaFunction
{
Expand Down Expand Up @@ -57,16 +58,23 @@ public function runtime()

public function memory()
{
return 2048;
return config('sidecar-browsershot.memory');
}

public function warmingConfig()
{
return WarmingConfig::instances(config('sidecar-browsershot.warming'));
}

public function layers()
{
if ($layer = config('sidecar-browsershot.layer')) {
return [$layer];
}

$region = config('sidecar.aws_region');

return [
// https://github.com/shelfio/chrome-aws-lambda-layer
"arn:aws:lambda:{$region}:764866452798:layer:chrome-aws-lambda:25",
];
// https://github.com/shelfio/chrome-aws-lambda-layer
return ["arn:aws:lambda:{$region}:764866452798:layer:chrome-aws-lambda:25"];
}
}
1 change: 1 addition & 0 deletions src/SidecarBrowsershotServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function configurePackage(Package $package): void
*/
$package
->name('sidecar-browsershot')
->hasConfigFile()
->hasCommand(InternalBrowsershotSetupCommand::class);
}
}

0 comments on commit 82fddc9

Please sign in to comment.