m6web/daemon-bundle

该包的最新版本(v8.0.0)没有提供许可证信息。

创建sf easing creating daemon commands

安装次数: 285,251

依赖: 0

建议者: 0

安全: 0

星标: 59

关注者: 47

分支: 12

开放问题: 0

类型:symfony-bundle

v8.0.0 2024-05-27 19:20 UTC

README

目录

DaemonBundle Build Status

允许您使用 React 事件循环组件 创建守护进程命令。

安装

通过 composer

composer require m6web/daemon-bundle

注意

  • 如果您使用的是 symfony 版本 >= 4.3,请使用最新版本。
  • 对于 symfony 版本在 2.33.0 之间,您可以使用 m6web/daemon-bundle:^1.4
  • 对于 PHP 版本 >=5.5.9<7.0 的支持,您可以使用 m6web/daemon-bundle:^3.0

有关插件安装的更多信息,请参阅您版本的 symfony 文档。

配置

您可以选择定义每 X 次迭代触发的事件。

m6_web_daemon:
    iterations_events:
        -
            count: 10
            name: Path\From\Your\Project\Event\EachTenEvent
        -
            count: 5
            name: Path\From\Your\Project\Event\EachFiveEvent

您的事件需要扩展类似于以下的 AbstractDaemonEvent

<?php

namespace Path\From\Your\Project\Event;

use M6Web\Bundle\DaemonBundle\Event\AbstractDaemonEvent;

class EachFiveEvent extends AbstractDaemonEvent
{
}

此扩展使用 PSR-14 实现的事件调度器,因此您需要在 config/services.yaml 中注册 symfony 事件调度器,如下所示

# config/services.yaml
services:
    # ... others declarations

    Psr\EventDispatcher\EventDispatcherInterface: "@event_dispatcher"

使用

此命令使用 事件循环组件,该组件使用 ReactPHP 来运行循环和其他异步任务。

<?php

namespace App\Command;

use M6Web\Bundle\DaemonBundle\Command\DaemonCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DaemonizedCommand extends DaemonCommand
{
    protected function configure()
    {
        $this
            ->setName('daemonized:command')
            ->setDescription('My daemonized command');
    }

    protected function setup(InputInterface $input, OutputInterface $output): void
    {
        // Set up your daemon here

        // Add your own optional callback : called every 10 iterations
        $this->addIterationsIntervalCallback(10, [$this, 'executeEveryTenLoops']);

        // You can add your own Periodic Timer,
        // Here this timer will be called every 0.5s
        $daemon = $this;
        $this->loop->addPeriodicTimer(0.5, function ($timer) use ($daemon) {
            // It's the last loop, cancel the timer.
            if ($daemon->isLastLoop()) {
                $daemon->loop->cancelTimer($timer);
            }
        });
    }

    /**
     * Execute is called at every loop
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln("Iteration");

        // This method helps to give back the CPU to the react-loop.
        // So you can wait between two iterations if your workers has nothing to do.

        $this->setNextIterationSleepingTime(1000000); // Every second
    }

    /**
     * executeEveryTenLoops is called every 10 loops
     */
    protected function executeEveryTenLoops(InputInterface $input, OutputInterface $output): void
    {
        $output->writeln("Iteration " . $this->getLoopCount());
    }
}

您还需要在 services 下声明您的命令。

# config/services
services:
    # ... others declarations

    App\Command\DaemonizedCommand:
        parent: M6Web\Bundle\DaemonBundle\Command\DaemonCommand
        tags:
            - console.command

为了信息,您需要将 autowireautoconfigure 参数(设置为 false),仅当您有服务(在 _default 下)的默认参数时。

运行命令

您可以使用 bin/console 以与其他 Symfony 命令相同的方式运行守护进程命令。DaemonCommand 父类提供了额外的选项

  • --run-once - 只运行一次命令
  • --run-max - 运行命令 x 次
  • --memory-max - 当达到指定的字节数量时,优雅地停止运行命令
  • --shutdown-on-exception - 如果抛出异常,则请求关闭
  • --show-exceptions - 在命令输出流上显示异常

命令事件

守护进程命令触发以下事件

  • DaemonEvents::DAEMON_START
  • DaemonEvents::DAEMON_LOOP_BEGIN
  • DaemonEvents::DAEMON_LOOP_EXCEPTION_STOP
  • DaemonEvents::DAEMON_LOOP_EXCEPTION_GENERAL
  • DaemonEvents::DAEMON_LOOP_MAX_MEMORY_REACHED
  • DaemonEvents::DAEMON_LOOP_ITERATION
  • DaemonEvents::DAEMON_LOOP_END
  • DaemonEvents::DAEMON_STOP