bref: Exception thrown unless Symfony cache directory is also changed to `/tmp/...`
Following the example code for Symfony and deploying, the request fails with the response {"message": "Internal server error"}
.
Checking CloudWatch logs shows 2 relevant entries:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /var/task/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 171
and
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 73728 bytes) in /var/task/vendor/symfony/debug/DebugClassLoader.php on line 145
Altering Kernel.php
to include the following makes the problem go away
public function getCacheDir()
{
// When on the lambda only /tmp is writable
if (getenv('LAMBDA_TASK_ROOT') !== false) {
return '/tmp/cache/'.$this->environment;
}
return $this->getProjectDir().'/var/cache/'.$this->environment;
}
However…
Presumably setting the cache dir to Lambda’s local /tmp
kind-of defeats the point pre-warming the cache with the hooks in the .bref.yml
file? The object is to avoid launching the application without the cache already in place, right?
.bref.yml
:
hooks:
build:
- 'APP_ENV=prod php bin/console cache:clear --no-debug --no-warmup'
- 'APP_ENV=prod php bin/console cache:warmup'
serverless.yml
:
service: test
provider:
name: aws
runtime: nodejs6.10
package:
exclude:
- '*'
- '**'
include:
- bref.php
- 'src/**'
- 'vendor/**'
- composer.json # Symfony uses it to figure out the root directory
- 'bin/**'
- 'config/**'
- 'var/cache/prod/**' # We want to deploy the production caches
functions:
# By default we create one "main" function
main:
handler: handler.handle
timeout: 20 # Timeout in seconds, the default is 6 seconds
# The function will match all HTTP URLs
events:
- http: 'ANY /'
- http: 'ANY {proxy+}'
environment:
APP_ENV: 'prod'
APP_DEBUG: '0'
bref.php
:
<?php
use App\Kernel;
use Bref\Bridge\Symfony\SymfonyAdapter;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
require __DIR__.'/vendor/autoload.php';
Debug::enable();
// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
(new Dotenv)->load(__DIR__.'/.env');
}
if ($_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev'))) {
umask(0000);
}
$kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev'))));
$app = new \Bref\Application;
$app->httpHandler(new SymfonyAdapter($kernel));
$app->cliHandler(new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel));
$app->run();
I get the same behaviour with cloning the mnapoli/bref-symfony-demo
repo and running bref deploy
, and also with adding bref to a symfony/website-skeleton
project.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 25 (25 by maintainers)
Commits related to this issue
- Merge pull request #39 from deleugpn/runtime-pr Makefile cpu-specific — committed to brefphp-bot/bref by deleugpn 2 years ago
Hi! I personally chose to redirect Twig’s cache to
config/packages/prod/twig.yaml
to be able to warm up other bundles.I know it’s a temporary solution but I preferer doing this because making
/tmp
the default Symfony cache directory increases the cold start from 500ms to 2 seconds.I hope Twig’s team will work on this problem to comply with the Symfony4 best practices (var/cache should be read-only and warmable).
I’ll be closing this issue since it was opened in 2019, and since then we have the Symfony bridge that should take care of the cache 🎉
Workaround documentation added in #42
I reverted my changes in
Kernel.php
and did the following:I see the same exception(s) related to the cache again