private final function loop()
{
if (file_put_contents($this->getPidPath(), getmypid())) {
$this->parentPID = getmypid();
\Yii::trace('Daemon ' . $this->getProcessName() . ' pid ' . getmypid() . ' started.');
while (!self::$stopFlag) {
if (memory_get_usage() > $this->memoryLimit) {
\Yii::trace('Daemon ' . $this->getProcessName() . ' pid ' . getmypid() . ' used ' . memory_get_usage() . ' bytes on ' . $this->memoryLimit . ' bytes allowed by memory limit');
break;
}
$this->trigger(self::EVENT_BEFORE_ITERATION);
$this->renewConnections();
$jobs = $this->defineJobs();
if ($jobs && !empty($jobs)) {
while (($job = $this->defineJobExtractor($jobs)) !== null) {
//if no free workers, wait
if ($this->isMultiInstance && count(static::$currentJobs) >= $this->maxChildProcesses) {
\Yii::trace('Reached maximum number of child processes. Waiting...');
while (count(static::$currentJobs) >= $this->maxChildProcesses) {
sleep(1);
pcntl_signal_dispatch();
}
\Yii::trace('Free workers found: ' . ($this->maxChildProcesses - count(static::$currentJobs)) . ' worker(s). Delegate tasks.');
}
pcntl_signal_dispatch();
$this->runDaemon($job);
}
} else {
sleep($this->sleep);
}
pcntl_signal_dispatch();
$this->trigger(self::EVENT_AFTER_ITERATION);
}
\Yii::info('Daemon ' . $this->getProcessName() . ' pid ' . getmypid() . ' is stopped.');
return self::EXIT_CODE_NORMAL;
}
$this->halt(self::EXIT_CODE_ERROR, 'Can\'t create pid file ' . $this->getPidPath());
}