bref/symfony-bridge

Bref使Symfony在AWS Lambda上运行

资助包维护!
mnapoli

安装数: 819 474

依赖者: 0

建议者: 0

安全: 0

星级: 48

关注者: 8

分支: 14

开放问题: 10

1.0.3 2024-08-27 12:56 UTC

This package is auto-updated.

Last update: 2024-08-27 12:57:10 UTC


README

Bref 运行时,在AWS Lambda上运行Symfony。

Build Status Latest Version Total Downloads

安装

composer req bref/symfony-bridge

用法

您只需进行一个小改动,就可以快速设置Symfony以与Bref一起使用。

// src/Kernel.php

namespace App;

+ use Bref\SymfonyBridge\BrefKernel;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

- class Kernel extends BaseKernel
+ class Kernel extends BrefKernel
{
    // ...

现在您已经启动并运行了。

优化首次请求

在您部署了新版本后,第一次击中应用程序的HTTP请求将使用冷缓存目录。现在,Symfony正在花费时间构建缓存。这取决于应用程序的复杂性,可能需要1-20秒的时间。

从技术上讲,每当您的应用程序在新的Lambda上运行时,都会发生这种情况。这可能是在您获得大量流量时(AWS增加资源)或AWS决定杀死您目前所在的Lambda函数(或服务器)时。每天至少发生几次这种情况是很正常的。

为了优化首次请求,必须以热缓存部署应用程序。在简单应用程序中,这意味着部署脚本应包括cache:warmup,类似于以下内容

# Install dependencies
composer install --classmap-authoritative --no-dev --no-scripts

# Warmup the cache
bin/console cache:clear --env=prod

# Disable use of Dotenv component
echo "<?php return [];" > .env.local.php

serverless deploy

优化缓存

在Lambda上运行Symfony时,应避免写入文件系统。如果部署前预先热缓存,您通常没有问题。但您还应该确保您从未写入文件系统缓存(如cache.system)或使用类似

framework:
    cache:
        pools:
            my_pool:
                adapter: cache.adapter.filesystem

如果您不写入此类缓存池,则可以通过不复制var/cache/pools目录来优化您的设置。下面的更改将确保创建pools目录的符号链接。

// src/Kernel.php


class Kernel extends BrefKernel
{
    // ...

+    protected function getWritableCacheDirectories(): array
+    {
+        return [];
+    }
}

在不使用FPM的情况下处理请求

注意:这是一个高级主题。除非您知道自己在做什么,否则不要担心这个。

通过保持进程存活,使用Symfony Kernel通过不使用PHP-FPM处理HTTP请求

# serverless.yml

functions:
    app:
-        handler: public/index.php
+        handler: App\Kernel
        layers:
            # Switch from PHP-FPM to the "function" runtime:
-            - ${bref:layer.php-80-fpm}
+            - ${bref:layer.php-80}
        environment:
            # The Symfony process will restart every 100 requests
            BREF_LOOP_MAX: 100

App\Kernel将通过Symfony Runtime从public/index.php检索。如果您没有public/index.php,请阅读下一节。

类处理器

通过类名处理其他事件(例如,通过Symfony Messenger处理SQS消息

# serverless.yml

functions:
    sqsHandler:
-        handler: bin/consumer.php
+        handler: App\Service\MyService
        layers:
            - ${bref:layer.php-80}

服务将通过Symfony Runtime从public/index.php返回的Symfony Kernel检索。

注意:必须在Symfony配置中将服务配置为公共public: true)。

自定义引导文件

如果您没有public/index.php文件,您可以创建一个返回内核(或任何PSR-11容器)的文件

<?php

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    return new App\Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

并在serverless.yml中配置它

# serverless.yml
functions:
    sqsHandler:
        handler: kernel.php:App\Service\MyService