miksoftware/daemon-bundle

Symfony2 Daemon Bundle

Installs: 103 106

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 1

Forks: 1

Open Issues: 0

Type:symfony-bundle

0.9.3 2014-05-05 22:27 UTC

This package is not auto-updated.

Last update: 2024-04-27 12:59:45 UTC


README

PayPayl donate button Latest Stable Version Latest Unstable Version License

DeamonBundle allows to easily convert your Symfony2 console scripts into system daemons. To use DaemonBundle pcntl is required to be configured in your PHP binary.

DaemonBundle is a based in the PEAR library System_Daemon which was created by Kevin Vanzonneveld and it is highly inspaired by uecode/daemon-bundle.

System_Daemon Package

System_Daemon is a PHP class that allows developers to create their own daemon applications on Linux systems. The class is focussed entirely on creating and spawning standalone daemons. More information can be found at:

Installation

  1. Add this bundle to the composer.json file of your project.

    php composer.phar require miksoftware/daemon-bundle dev-master
  2. Add the bundle to your application kernel.

    // app/AppKernel.php
    public function registerBundles()
    {
        return array(
            // ...
            new MikSoftware\DaemonBundle\MikSoftwareDaemonBundle()
            // ...
        );
    }
  3. Configure the bundle by adding parameters to the config.yml or parameters.yml file:

    # app/config/config.yml
    
    mik_software_daemon:
        daemons:
            <command name>: ~

Configuration Parameters

To change any configuration setting, you could add required parameters to your project config. Following, an example of a minimal required configuration.

mik_software_daemon:
    daemons:
        <daemon_id_name>: ~

Other available parameters (with default values):

mik_software_daemon:
    daemons:
        <daemon_id_name>: ~
            appName: systemDaemon
            appDir: %kernel.root_dir%
            appDescription: System Daemon
            logDir: %kernel.logs_dir%
            authorName: ~
            authorEmail: ~
            appPidDir: %kernel.cache_dir%/daemons/
            sysMaxExecutionTime: 0
            sysMaxInputTime: 0
            sysMemoryLimit: 1024M
            appUser: www-data
            appGroup: www-data
            appRunAsGID: 1000
            appRunAsUID: 1000
            logVerbosity: info

Usage

  1. Create a Symfony2 command that extends from DaemonizedCommand:

    <?php
    
    namespace <Application>\<Bundle>\Command;
    
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\Console\Input\InputArgument;
    use MikSoftware\DaemonBundle\Commnad\DaemonizedCommand;
    
    class <Name>Command extends DaemonizedCommand
    {
        protected function configureDaemonCommand()
        {
            $this
                ->setName('<name>')
                ->setDescription('<description>')
                ->setHelp('Usage <info>php app/console <name> start|stop|restart</info>');
        }
    
        /**
         * Starts asynchronous release:deploy commands for queue items.
         */
        protected function daemonLogic()
        {
            // Log something
            $this->getContainer()->get('logger')->info('Daemon is running!');
            // And then sleep for 5 seconds
            $this->daemon->iterate(1);
        }
    }
  2. Run your daemon

    mik@mbp:~$ php app/console <name> start
    mik@mbp:~$ php app/console <name> stop
    mik@mbp:~$ php app/console <name> restart

The methods start, stop and restart will be automatically added to your Symfony2 command. It is possible to add new methods to your command as follow:

<?php
    // ...
    class <Name>Command extends DaemonizedCommand
    {
        protected function configureDaemonCommand()
        {
            $this->addMethods(array('status'));        
            $this
                ->setName('<name>')
                ->setDescription('<description>')
                ->setHelp('Usage <info>php app/console <name> start|stop|restart</info>');
        }
            
        // ...
            
        protected function status()
        {
            // TODO: implement the new method
        }
    }

License

DaemonBundle is completly open and it is released under the terms of BSD 3-Clause.

Copyright (c) 2013-2014, Michele Nucci All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the M1KSoftware and Michele Nucci nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY MICHELE NUCCI ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MICHELE NUCCI BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.