-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
159 lines (137 loc) · 4.13 KB
/
server.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
158
159
<?php
declare(ticks = 1);
error_reporting(E_ALL^E_NOTICE);
class Server
{
public $client;
public $mpid=0;
public $rpid=0;
public $works=[];
public $new_index=0;
public function __construct(){
swoole_process::signal(SIGTERM, function($sig) {
//必须为false,非阻塞模式
while($ret = swoole_process::wait(false)) {
echo "PID={$ret['pid']}\n";
}
});
require_once "./src/client.php";
try {
swoole_set_process_name(sprintf('php-ps:%s', 'master'));
$this->mpid = posix_getpid();
$this->run();
$this->processWait();
}catch (\Exception $e){
die('ALL ERROR: '.$e->getMessage());
}
}
/**
* acitve reactor process
*
*/
public function run(){
$reactorProcess = new swoole_process(function(swoole_process $worker){
swoole_set_process_name(sprintf('php-ps:%s','reactor'));
while(true)
{
$this->checkMpid($worker);
sleep(1);
$this->client = new Beanstalk\Client();
$this->client->connect();
if(!$this->activeSubProcess())
{
echo "Waiting for the data ...\n";
continue;
}
$this->client->disconnect();
}
}, false, false);
$this->rpid = $reactorProcess->start();
return $this->rpid;
}
/**
* active subprocess
* @var array $tubeList
* @return bool|boolean
*/
public function activeSubProcess(){
$this->client->ignore('default');
$tubeList = $this->client->listTubes();
if (!empty($tubeList))
{
foreach($tubeList as $tube)
{
if ($tube=='default'){
continue;
}
$this->CreateSubProcess($tube);
swoole_process::wait();
}
}
return false;
}
public function CreateSubProcess($tube,$index=null){
$process = new swoole_process(function(swoole_process $worker)use($index,$tube){
if(is_null($index)){
$index=$this->new_index;
$this->new_index++;
}
swoole_set_process_name(sprintf('php-ps:%s',$index));
echo "worker id:$index start\n";
$this->handleData($tube,$worker);
echo "worker id:$index exit\n";
}, false, false);
$pid=$process->start();
$this->works[$index]=$pid;
return $pid;
}
public function handleData($tube,&$worker){
$this->client->watch($tube);
$stats = $this->client->stats();
if ($stats['current-jobs-buried']>0)
{
$this->client->kick($stats['current-jobs-buried']);
}
while (true) {
if ($stats['current-jobs-ready'] == 0){
break;
}
$job = $this->client->reserve();
//处理任务
echo $job['body']."\n";
$this->client->delete($job['id']);
}
echo "finish process data\n";
}
public function checkMpid(&$worker){
if(!swoole_process::kill($this->mpid,0)){
$worker->exit();
if (file_exists('./log/app.log')) {
file_put_contents('./log/app.log','Master process exited, I [{$worker[\'pid\']}] also quit\n',FILE_APPEND);
}
}
}
public function rebootProcess($ret){
$pid=$ret['pid'];
if($pid==$this->rpid){
$new_pid=$this->run();
echo "rebootProcess: {$pid}={$new_pid} Done\n";
return;
}
throw new \Exception('rebootProcess Error: no pid');
}
public function processWait(){
while(true) {
if($this->rpid)
{
$ret = swoole_process::wait();
if ($ret) {
$this->rebootProcess($ret);
}
}else{
break;
}
}
}
}
$server = new Server();