-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDaemon.php
157 lines (130 loc) · 4.16 KB
/
Daemon.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace CodeMeme\Bundle\CodeMemeDaemonBundle;
/**
* Daemon is a php5 wrapper class for the PEAR library System_Daemon
*
* PHP version 5
*
* @category CodeMeme
* @package CodeMemeDaemonBundle
* @author Jesse Greathouse <[email protected]>
* @copyright 2011 CodeMeme (https://github.com/organizations/CodeMeme)
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @link https://github.com/CodeMeme/CodeMemeDaemonBundle
*/
use CodeMeme\Bundle\CodeMemeDaemonBundle\System\Daemon as System_Daemon;
use CodeMeme\Bundle\CodeMemeDaemonBundle\System\Daemon\Exception as CodeMemeDaemonBundleException;
class Daemon
{
private $_config = array();
private $_pid;
private $_interval = 2;
public function __construct($options)
{
if (!empty($options)) {
$options = $this->validateOptions($options);
$this->setConfig($options);
} else {
throw new CodeMemeDaemonBundleException('Daemon instantiated without a config');
}
}
private function validateOptions($options)
{
if (null === ($options['appRunAsUID'])) {
throw new CodeMemeDaemonBundleException('Daemon instantiated without user or group');
}
if (!isset($options['appRunAsGID'])) {
try {
$user = posix_getpwuid($options['appRunAsUID']);
$options['appRunAsGID'] = $user['gid'];
} catch (CodeMemeDaemonBundleException $e) {
echo 'Exception caught: ', $e->getMessage(), "\n";
}
}
return $options;
}
public function setConfig($config)
{
$this->_config = $config;
}
public function getPid()
{
if (file_exists($this->_config['appPidLocation'])) {
$fh = fopen($this->_config['appPidLocation'], "r");
$pid = fread($fh, filesize($this->_config['appPidLocation']));
fclose($fh);
return trim($pid);
} else {
return null;
}
}
public function setPid($pid)
{
$this->_pid = $pid;
}
public function setInterval($interval)
{
$this->_interval = $interval;
}
public function getInterval()
{
return $this->_interval;
}
public function getConfig()
{
return $this->_config;
}
public function start()
{
System_Daemon::setOptions($this->getConfig());
System_Daemon::start();
System_Daemon::info('{appName} System Daemon Started at %s',
date("F j, Y, g:i a")
);
$this->setPid($this->getPid());
}
public function reStart()
{
System_Daemon::setOptions($this->getConfig());
$pid = $this->getPid();
System_Daemon::info('{appName} System Daemon flagged for restart at %s',
date("F j, Y, g:i a")
);
$this->stop();
exec("ps ax | awk '{print $1}'", $pids);
while(in_array($pid, $pids, true)) {
unset($pids);
exec("ps ax | awk '{print $1}'", $pids);
$this->iterate(5);
}
System_Daemon::info('{appName} System Daemon Started at %s',
date("F j, Y, g:i a")
);
$this->start();
}
public function iterate($sec) {
System_Daemon::iterate($sec);
}
public function isRunning()
{
if (!System_Daemon::isDying() && $this->_pid != null && $this->_pid == $this->getPid()) {
System_Daemon::iterate($this->_interval);
return true;
} else {
return false;
}
}
public function stop()
{
if (file_exists($this->_config['appPidLocation'])) {
unlink($this->_config['appPidLocation']);
System_Daemon::info('{appName} System Daemon Terminated at %s',
date("F j, Y, g:i a")
);
} else {
System_Daemon::info('{appName} System Daemon Stop flag sent at %s',
date("F j, Y, g:i a")
);
}
}
}