2017 © Pedro Peláez
 

symfony-bundle daemon-bundle

bundle sf easing creating daemon commands

image

m6web/daemon-bundle

bundle sf easing creating daemon commands

  • Monday, June 18, 2018
  • by M6Web
  • Repository
  • 24 Watchers
  • 44 Stars
  • 92,557 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 11 Forks
  • 0 Open issues
  • 22 Versions
  • 6 % Grown

The README.md

Table of Contents, (*1)

DaemonBundle Build Status

Allows you to create daemonized commands with the React event-loop component., (*2)

Installation

Via composer :, (*3)

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, (*4)

For more information about installation of plugin refers the documentation of symfony for your version., (*5)

Configuration

You can optionally define events which are triggered each X iterations:, (*6)

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:, (*7)

<?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:, (*8)

# 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., (*9)

<?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:, (*10)

# 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), (*11)

Run command

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

  • --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 :, (*13)

  • 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

The Versions

18/06 2018

dev-master

9999999-dev

bundle sf easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

18/06 2018

v4.1.0

4.1.0.0

bundle sf easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

25/04 2018

v4.0.1

4.0.1.0

bundle sf easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

25/04 2018

dev-feature/add-event-loop-documentation

dev-feature/add-event-loop-documentation

bundle sf easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

09/04 2018

v4.0.0

4.0.0.0

bundle sf easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

29/03 2018

dev-feature/new-version-event-loop-react

dev-feature/new-version-event-loop-react

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

26/02 2018

v3.0.3

3.0.3.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

16/02 2018

dev-fix/travis-builds

dev-fix/travis-builds

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

16/02 2018

v3.0.2

3.0.2.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

08/11 2016

v3.0.1

3.0.1.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

10/10 2016

v3.0.0

3.0.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

29/09 2016

v2.0.1

2.0.1.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

29/06 2016

v2.0.0

2.0.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

02/12 2015

v1.4.0

1.4.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

05/05 2015

v1.3.2

1.3.2.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

23/01 2015

v1.3.1

1.3.1.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

30/10 2014

v1.3.0

1.3.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

03/10 2014

1.2.0

1.2.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

13/06 2014

v1.1.0

1.1.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

05/06 2014

v1.0.0

1.0.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

27/05 2014

v0.1.1

0.1.1.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour

13/05 2014

v0.1.0

0.1.0.0

bundle sf2 easing creating daemon commands

  Sources   Download

The Requires

 

The Development Requires

by Olivier Mansour