PHP code example of consik / yii2-daemons

1. Go to this page and download the library: Download consik/yii2-daemons library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

consik / yii2-daemons example snippets



class TestDaemon implements DaemonInterface
{
	private $stop = false;
	private $counter = 0;

	public function startDaemon()
	{
		while (!$this->stop) {
			$counter++;
			sleep(1)
		}
	}

	public function stopDaemon()
	{
		$this->stop = true;
	}
}


namespace app\daemons;

use consik\yii2daemons\daemons\AbstractLoopDaemon;
use consik\yii2daemons\daemons\behaviors\LoopDaemonSignalsBehavior;
use consik\yii2daemons\service\ServiceConfigInterface;

class TestDaemon extends AbstractLoopDaemon implements ServiceConfigInterface
{
    public $serviceConfig = [];
	protected $i = 1;
    protected $iterationTimeout = 10;

    public function getServiceConfig()
    {
        return $this->serviceConfig;
    }

	public function behaviors()
    {
        return[
            [
                'class' => LoopDaemonSignalsBehavior::className(),
                'signalHandlers' => [SIGTERM => [$this, 'stopDaemon']]
            ]
        ];
    }

    public function stopDaemon()
    {
        echo $this->i;
        parent::stopDaemon();
    }

    protected function iterate()
    {
        $this->i++;
    }
}


...
'controllerMap' => [
        'service' => [
            'class' => '\consik\yii2daemons\service\ServiceController',
			'daemons' => [
                'testDaemon' => [
                    'class' => 'app\daemons\TestDaemon',
                ]
            ]
		]
	]
...
 yii service/systemd-file DaemonName

...
[
	'SectionName' => [
		'ParamName' => 'ParamValue'
	]
]
...

		...
		'controllerMap' => [
		'service' => [
		    'class' => '\consik\yii2daemons\service\ServiceController',
				'daemons' => [
					'testDaemon' => [
						'class' => 'app\daemons\TestDaemon',
						'serviceConfig' => [
							'Service' => ['Type' => 'forking']
						],
					]
				]
			]
		]
		...
	

		public function getServiceConfig()
		{
			return [
				'Service' => ['Type' => 'forking']
			];
		}
	

		...
		'controllerMap' => [
			'service' => [
			    'class' => '\consik\yii2daemons\service\ServiceController',
			    'commonServiceConfig' => [
						'Service' => ['Type' => 'forking']
					],
			],
		]
		...
	
$this->callSignalDispatch()
 sudo bash vendor\consik\yii2-daemons\service\systemd.register ServiceName "$(php yii service/systemd-file DaemonName)" 
sudo php yii service/status DaemonName
sudo php yii service/stop DaemonName

	public $serviceConfig = [];
	public function getServiceConfig()
	{
		return $this->serviceConfig;
	}