cadyrov / yii2-daemons
daemons on php structure
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:yii2-extension
pkg:composer/cadyrov/yii2-daemons
Requires
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2025-10-22 16:16:56 UTC
README
daemons on php structure
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist cadyrov/yii2-daemons "*"
or add
"cadyrov/yii2-daemons": "*"
to the require section of your composer.json file.
Install
Once the extension is installed, simply use it in your code by :
- Create in you console controllers path file ObserverController.php with following content:
<?php
namespace console\controllers;
class ObserverController extends \cadyrov\daemons\controllers\ObserverController
{
    /**
     * @return array
     */
    protected function defineJobs()
    {
        sleep($this->sleep);
        //TODO: modify list, or get it from config, it does not matter
        $daemons = [
            ['className' => 'OneDaemonController', 'enabled' => true],
            ['className' => 'AnotherDaemonController', 'enabled' => false]
        ];
        return $daemons;
    }
}
- No one checks the Watcher. Watcher should run continuously. Add it to your crontab:
* * * * * /path/to/yii/project/yii observer-daemon --demonize=1
Observer can't start twice, only one instance can work in the one moment.
Usage
Create new daemons
- Create in you console controllers path file {NAME}DaemonController.php with following content:
<?php
namespace console\controllers;
use \cadyrov\daemons\Daemon;
class {NAME}DaemonController extends Daemon
{
    /**
     * @return array
     */
    protected function defineJobs()
    {
        /*
        TODO: return task list, extracted from DB, queue managers and so on.
        Extract tasks in small portions, to reduce memory usage.
        */
    }
    /**
     * @return jobtype
     */
    protected function doJob($job)
    {
        /*
        TODO: implement you logic
        Don't forget to mark task as completed in your task source
        */
    }
}
- Implement logic.
- Add new daemon to daemons list in watcher.