luzrain/workerman-bundle

Workerman 运行时用于 symfony 应用程序

安装: 80

依赖者: 0

建议者: 0

安全: 0

星标: 21

关注者: 1

分支: 0

公开问题: 0

类型:symfony-bundle

v0.3.0 2024-02-15 15:53 UTC

This package is auto-updated.

Last update: 2024-09-15 17:11:20 UTC


README

PHP ^8.1 Symfony ^6.4|^7.0 Tests Status

Workerman 是一个纯 PHP 编写的性能高效的异步事件驱动 PHP 框架。
此扩展包提供了 Workerman 在 Symfony 中的集成,允许您在一个地方轻松创建 http 服务器、调度程序和监视器。此扩展包允许您用纯 PHP(没有 Go,没有外部二进制文件)替换传统的 web 应用程序堆栈,如 php-fpm + nginx + cron + supervisord。请求处理器在事件循环中工作,这意味着 Symfony 内核和依赖注入容器在请求之间被保留,这使得您的应用程序更快,且不需要(或几乎没有)代码更改。

入门

安装 composer 包

$ composer require luzrain/workerman-bundle nyholm/psr7

启用扩展包

<?php
// config/bundles.php

return [
    // ...
    Luzrain\WorkermanBundle\WorkermanBundle::class => ['all' => true],
];

配置扩展包

最小配置可能如下所示。
有关所有可用选项和文档,请参阅命令输出。

$ bin/console config:dump-reference workerman
# config/packages/workerman.yaml

workerman:
  servers:
    - name: 'Symfony webserver'
      listen: http://0.0.0.0:80
      processes: 4

  reload_strategy:
    exception:
      active: true

    file_monitor:
      active: true

启动应用程序

$ APP_RUNTIME=Luzrain\\WorkermanBundle\\Runtime php public/index.php start

* 为了更好的性能,Workerman 推荐安装 php-event 扩展。

重载策略

由于服务器异步的特性,工作进程会在每次请求中重用已加载的资源。这意味着在某些情况下我们需要重启工作进程。
例如,在抛出异常后,为了防止服务处于不可恢复的状态。或者每次在 IDE 中更改代码时。
实现了一些重启策略,可以根据环境启用或禁用。

  • exception
    在请求处理过程中抛出异常时,每次重新加载工作进程。
  • max_requests
    每 N 次请求重新加载工作进程,以防止内存泄漏。
  • file_monitor
    每次更改文件时重新加载所有工作进程**。
  • always
    每次请求后重新加载工作进程。

** 非常推荐安装 php-inotify 扩展以进行文件监控。如果没有它,监控将以轮询模式工作,这可能会对大型项目造成非常大的 CPU 和磁盘压力。

请参阅命令输出中每个策略的所有可用选项。

$ bin/console config:dump-reference workerman reload_strategy

实现自己的重载策略

您可以通过实现 RebootStrategyInterface 并将 workerman.reboot_strategy 标签添加到服务中,使用自己的逻辑创建重载策略。

<?php

use Luzrain\WorkermanBundle\Reboot\RebootStrategyInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('workerman.reboot_strategy')]
final class TestRebootStrategy implements RebootStrategyInterface
{
    public function shouldReboot(): bool
    {
        return true;
    }
}

调度程序

可以使用属性或配置文件中的标签配置周期性任务。
调度字符串可以格式化为几种方式

  • 一个整数,用于定义频率为秒数。例如:60
  • ISO8601 日期时间格式。例如:2023-08-01T01:00:00+08:00
  • ISO8601 持续时间格式。例如:PT1M
  • 支持 DateInterval 的相对日期格式。例如:1 minutes
  • cron 表达式**。例如:*/1 * * * *

** 注意,如果您想使用 cron 表达式作为调度字符串,则需要安装 dragonmantank/cron-expression 包。

<?php

use Luzrain\WorkermanBundle\Attribute\AsTask;

/**
 * Attribute parameters
 * name: Task name
 * schedule: Task schedule in any format
 * method: method to call, __invoke by default
 * jitter: Maximum jitter in seconds that adds a random time offset to the schedule. Use to prevent multiple tasks from running at the same time
 */
#[AsTask(name: 'My scheduled task', schedule: '1 minutes')]
final class TaskService
{
    public function __invoke()
    {
        // ...
    }
}

监视器

可以使用属性或配置文件中的标签配置监视器。
进程保持活跃,如果一个进程死亡,其他进程将被唤醒。

<?php

use Luzrain\WorkermanBundle\Attribute\AsProcess;

/**
 * Attribute parameters
 * name: Process name
 * processes: number of processes
 * method: method to call, __invoke by default
 */
#[AsProcess(name: 'My worker', processes: 1)]
final class ProcessService
{
    public function __invoke()
    {
        // ...
    }
}