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++;
}
}