1. Go to this page and download the library: Download advissmedia/yii2-daemon 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/ */
advissmedia / yii2-daemon example snippets
php composer.phar
namespace console\controllers;
use \advissmedia\daemon\controllers\WatcherDaemonController;
class DaemonsSupervisorController extends WatcherDaemonController
{
/**
* @return array
*/
protected function getDaemonsList()
{
//TODO: modify list, or get it from config, it does not matter
$daemons = [
['className' => 'FirstDaemonController', 'enabled' => true, 'hardKill' => false],
['className' => 'AnotherDaemonController', 'enabled' => false, 'hardKill' => true]
];
return $daemons;
}
}
namespace console\controllers;
use \advissmedia\daemon\controllers\WatcherDaemonController;
class DaemonsSupervisorController extends WatcherDaemonController
{
/**
* @return array
*/
protected function getDaemonsList()
{
$string = @file_get_contents("/url/or/uri/to/json/file.json");
if ($string === false || empty($daemons = json_decode($string,true)))
return [];
return $daemons;
}
}
namespace console\controllers;
use \advissmedia\daemon\DaemonController;
class {NAME}DaemonController extends DaemonController
{
/**
* @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
*/
}
const DEF_FIRST_JOB = 1;
const DEF_SOCOND_JOB = 3;
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
/*
* Set type of jobs list can be processed
*/
$this->isJobsListPersistent = true;
/*
* Array connection name for renew connections for daemon process
*/
$this->connections = ['pgdb', 'sqltdb'];
}
/**
* @return array
*/
protected function defineJobs()
{
/*
TODO: return task list, extracted from DB, queue managers or jast array.
Extract tasks in small portions, to reduce memory usage.
*/
return [
['job_id' => self::DEF_FIRST_JOB],
['job_id' => self::DEF_SECOND_JOB]
];
}
/**
* @param array $job
* @return bool
* @throws \Exception
* @throws \Throwable
* @throws \yii\db\Exception
*/
protected function doJob($job)
{
/*
TODO: implement you logic
Don't forget to mark task as completed in your task source
*/
if (array_key_exists('job_id', $job)) {
switch ($job['job_id']) {
case self::DEF_FIRST_JOB:
$ret = $this->processFirstJob();
break;
case self::DEF_SECOND_JOB:
$ret = $this->processSecondJob();
break;
default:
$ret = false;
}
return $ret;
}
return false;
}
}