m6web/daemon-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

m6web/daemon-bundle

Composer 安装命令:

composer require m6web/daemon-bundle

包简介

bundle sf easing creating daemon commands

README 文档

README

Table of Contents

DaemonBundle Build Status

Allows you to create daemonized commands with the React event-loop component.

Installation

Via composer :

composer require m6web/daemon-bundle

Note:

  • If you are using a symfony version >= 4.3 use the lastest version
  • For symfony versions between 2.3 and 3.0, you can use m6web/daemon-bundle:^1.4
  • For PHP versions >=5.5.9 and <7.0 support, you can use m6web/daemon-bundle:^3.0

For more information about installation of plugin refers the documentation of symfony for your version.

Configuration

You can optionally define events which are triggered each X iterations:

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

Your event need to extends the AbstractDaemonEvent like following:

<?php

namespace Path\From\Your\Project\Event;

use M6Web\Bundle\DaemonBundle\Event\AbstractDaemonEvent;

class EachFiveEvent extends AbstractDaemonEvent
{
}

This bundle use the PSR-14 implementation for event dispatcher so you need to register the symfony event dispatcher in your config/services.yaml like this:

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

    Psr\EventDispatcher\EventDispatcherInterface: "@event_dispatcher"

Usage

This command uses the event-loop component which ReactPHP is used to run loops and other asynchronous tasks.

<?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());
    }
}

You also need to declare your command under the services:

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

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

For information, you need to declare the autowire and autoconfigure parameters (to false) only if you have defaults parameters for services (under _default)

Run command

You can run a daemonized command as any other Symfony command with bin/console. DaemonCommand parent class provide additional options :

  • --run-once - Run the command just once
  • --run-max - Run the command x time
  • --memory-max - Gracefully stop running command when given memory volume, in bytes, is reached
  • --shutdown-on-exception - Ask for shutdown if an exception is thrown
  • --show-exceptions - Display exceptions on command output stream

Command events

Daemonized command trigger the following events :

  • 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

m6web/daemon-bundle 适用场景与选型建议

m6web/daemon-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 344.85k 次下载、GitHub Stars 达 59, 最近一次更新时间为 2014 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 m6web/daemon-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 m6web/daemon-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 m6web/daemon-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 344.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 60
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 59
  • Watchers: 45
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-05-06