-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphp.redis.xml
191 lines (176 loc) · 4.5 KB
/
php.redis.xml
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN"
"/usr/share/xml/docbook/schema/dtd/4.5/docbookx.dtd" [
<!ENTITY article.author.xml SYSTEM "../common/article.author.xml">
<!ENTITY book.info.legalnotice.xml SYSTEM "../common/book.info.legalnotice.xml">
<!ENTITY book.info.abstract.xml SYSTEM "../common/book.info.abstract.xml">
]>
<article xml:base="http://netkiller.github.io/journal/"
xmlns="http://docbook.org/ns/docbook" xml:lang="zh-cn">
<articleinfo>
<title>PHP高级编程之发布订阅</title>
<subtitle></subtitle>
&article.author.xml;
<copyright>
<year>2014</year>
<holder>http://netkiller.github.io</holder>
</copyright>
&book.info.legalnotice.xml;
<abstract>
</abstract>
&book.info.abstract.xml;
<keywordset>
<keyword></keyword>
<keyword></keyword>
<keyword></keyword>
<keyword></keyword>
</keywordset>
<pubdate>2014-09-05</pubdate>
<release>$Id$</release>
</articleinfo>
<!--
<section id="what">
<title>what-做什么</title>
</section>
<section id="why">
<title>why-为什么做</title>
</section>
<section id="when">
<title>when-何时做</title>
</section>
<section id="where">
<title>where-何地做</title>
</section>
<section id="who">
<title>who-谁去做</title>
</section>
<section id="how">
<title>how-怎么做</title>
<section>
<title>SECTION</title>
</section>
</section>
-->
<section>
<title>Redis 订阅发布代码</title>
<para>订阅代码</para>
<screen>
<![CDATA[
<?php
$redis = new Redis();
$redis->connect('192.168.2.1',6379);
$channel = $argv[1]; // channel
$redis->subscribe(array('channel'.$channel), 'callback');
function callback($instance, $channelName, $message) {
echo $channelName, "==>", $message,PHP_EOL;
}
]]>
</screen>
<para>发布代码</para>
<screen>
<![CDATA[
<?php
$redis = new Redis();
$redis->connect('192.168.2.1',6379);
$channel = $argv[1]; // channel
$msg = $argv[2]; // msg
$redis->publish('channel'.$channel, $msg);
]]>
</screen>
</section>
<section>
<title>守护进程</title>
<para>上面程序发布端运行后始终处在前台,终端会接收来自键盘的消息,可能会终止程序运行。我们采用守护进程方式,使之进入后台运行。</para>
<screen>
<![CDATA[
<?php
/*
Homepage: http://netkiller.github.io
Author: neo chan <[email protected]>
*/
class Example {
/* config */
const HOST = '192.168.2.1';
const PORT = 6379;
const MAXCONN = 2048;
const pidfile = __CLASS__;
const uid = 80;
const gid = 80;
protected $pool = NULL;
protected $redis = NULL;
public function __construct() {
$this->pidfile = '/var/run/'.self::pidfile.'.pid';
$this->redis = new Redis();
}
private function daemon(){
if (file_exists($this->pidfile)) {
echo "The file $this->pidfile exists.\n";
exit();
}
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
//pcntl_wait($status); //Protect against Zombie children
exit($pid);
} else {
// we are the child
file_put_contents($this->pidfile, getmypid());
posix_setuid(self::uid);
posix_setgid(self::gid);
return(getmypid());
}
}
private function run(){
$this->pool = new Pool(self::MAXCONN, \ExampleWorker::class, []);
$this->redis->connect(self::HOST, self::PORT);
$channel = array('news', 'login', 'logout');
$this->redis->subscribe($channel, 'callback');
function callback($instance, $channelName, $message) {
echo $channelName, "==>", $message,PHP_EOL;
//print_r($message);
$this->pool->submit(new Fee($message));
}
$pool->shutdown();
}
private function start(){
$pid = $this->daemon();
$this->run();
}
private function onestart(){
$this->run();
}
private function stop(){
if (file_exists($this->pidfile)) {
$pid = file_get_contents($this->pidfile);
posix_kill($pid, 9);
unlink($this->pidfile);
}
}
private function help($proc){
printf("%s start | stop | onestart | help \n", $proc);
}
public function main($argv){
if(count($argv) < 2){
printf("please input help parameter\n");
exit();
}
if($argv[1] === 'stop'){
$this->stop();
} else if($argv[1] === 'start'){
$this->start();
} else if ($argv[1] === 'onestart') {
$this->onestart();
} else{
$this->help($argv[0]);
}
}
}
$example = new Example();
$example->main($argv);
]]>
</screen>
<para>收到任务通过$this->pool->submit将任务分配给线程程序处理</para>
</section>
</article>