forked from filsh/yii-daemon
/
SystemDaemon.php
72 lines (57 loc) · 2 KB
/
SystemDaemon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
require_once dirname(__FILE__) . '/lib/System/Daemon.php';
class SystemDaemon extends \CApplicationComponent
{
const LOG_FILE_NAME = 'daemon.log';
public $appName;
public $sysMaxExecutionTime = 0;
public $sysMaxInputTime = 0;
public $sysMemoryLimit = '128M';
public $logFile = '';
public $runAsGID = 0;
public $runAsUID = 0;
public function init()
{
if(!$this->appName) {
throw new CException('Invalid application name for daemon.');
}
if ($this->logFile=='') {
$this->logFile = implode(DIRECTORY_SEPARATOR,
array(Yii::getPathOfAlias('application'),
'runtime',
self::LOG_FILE_NAME
));
}
if(!file_exists($this->logFile)) {
if (!touch($this->logFile)) {
throw new CException('Invalid log file ' . $this->logFile);
}
}
Yii::registerAutoloader(array('System_Daemon', 'autoload'));
System_Daemon::setOptions(array(
'appName' => $this->appName,
'appDir' => Yii::getPathOfAlias('application'),
'sysMaxExecutionTime' => $this->sysMaxExecutionTime,
'sysMaxInputTime' => $this->sysMaxInputTime,
'sysMemoryLimit' => $this->sysMemoryLimit,
'appRunAsGID' => $this->runAsGID,
'appRunAsUID' => $this->runAsUID,
'logLocation' => $this->logFile
));
}
/**
* Any requests to set or get attributes or call methods on this class that
* are not found are redirected to the {@link System_Daemon} object.
*
* @param string $name the method name
* @param array $parameters
* @return mixed
*/
public function __call($name, $parameters) {
if(method_exists('System_Daemon', $name)) {
return call_user_func_array(array('System_Daemon', $name), $parameters);
} else {
return parent::__call($name, $parameters);
}
}
}