Commit 575888db authored by chenchuanwen's avatar chenchuanwen

bridgeReload

parent 5eb2c0d7
<?php
/**
*
* 脚本
*
* @author ccw <31435391@qq.com>
*/
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../../../')); //指向public的上一级
$http = new swoole_http_server("0.0.0.0", 9501);
/**
* 测试在 $server 外部注册全局自定义属性, 看看会不会被覆盖
*/
/**
* 测试在 $server 外部注册全局自定义属性, 看看会不会被覆盖
*/
$http->myWorkerVar = 'global';
$http->set(array(
'worker_num' => 5,
'daemonize' => false,
));
// 服务器启动时执行一次
$http->on('Start', function (swoole_http_server $server) {
echo '';
});
// 服务器启动时执行一次
$http->on('ManagerStart', function (swoole_http_server $server) {
echo 'ManagerStart: ' . PHP_EOL . PHP_EOL;
});
// 每个 Worker 进程启动或重启时都会执行
$http->on('WorkerStart', function (swoole_http_server $server, $workerId) {
// 通过重新加载外部文件来重载代码和释放之前占用的内存
//include_once __DIR__ . DIRECTORY_SEPARATOR .'workerstart.php';
// 下面这些直接写在当前文件中的代码即便重载也不会变化
echo 'WorkerStart: ' . PHP_EOL . PHP_EOL;
echo ' Worker ID: ' . $workerId . PHP_EOL . PHP_EOL;
// 启动服务器后, 去掉下面这行注释, 然后 reload , 该语句也不会执行的
//echo ' reloaded ! ' . PHP_EOL . PHP_EOL;
// 应该把这里的回调事件代码写在另一个文件中来 include 而不是直接写在这里
// 注意即便是 include_once , reload 也会重新加载的, 但在你的逻辑控制中是有效的
});
// 每次连接时(相当于每个浏览器第一次打开页面时)执行一次, reload 时连接不会断开, 也就不会再次触发该事件
$http->on('Connect', function (swoole_http_server $server, $fd, $reactorThreadId) {
echo 'Worker ID: '. $server->worker_id . ';'.'fd: ' . $fd . ' , fromId: ' . $reactorThreadId . PHP_EOL;
});
// 浏览器连接服务器后, 页面上的每个请求均会执行一次,
// 每次打开链接页面默认都是接收两个请求, 一个是正常的数据请求, 一个 favicon.ico 的请求
$http->on('request', function ($request, $response) use($http) {
require_once APPLICATION_PATH . '/scripts/crontab/common.php';
require APPLICATION_PATH . '/scripts/crontab/push/bridgeBase.php';
});
$http->start();
<?php
/**
*
* 脚本
*
* @author ccw <31435391@qq.com>
*/
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../../../')); //指向public的上一级
require APPLICATION_PATH . '/scripts/crontab/baseCli.php';
require APPLICATION_PATH . '/scripts/crontab/common.php';
error_reporting(E_ALL ^ E_NOTICE);
class bridgeStart
{
/* config */
const LISTEN = "tcp://192.168.2.15:5555";
const MAXCONN = 100;
const pidfile = __CLASS__;
const uid = 81;
const gid = 81;
/**/
protected $pool = NULL;
protected $zmq = NULL;
public function __construct()
{
$this->pidfile = '/var/run/' . self::pidfile . '.pid';
}
$http = new swoole_http_server("0.0.0.0", 9501);
private function daemon()
{
if (file_exists($this->pidfile)) {
echo "The file $this->pidfile exists.\n";
exit();
}
/**
* 测试在 $server 外部注册全局自定义属性, 看看会不会被覆盖
*/
$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());
}
}
public function callback($instance,$channelName,$message){
$orderService=\Business\Order\OrderServiceModel::getInstance();
$orderService->testPush($message);
}
protected function status(){
if (file_exists($this->pidfile)) {
$pid = file_get_contents($this->pidfile);
printf("%s already running, pid = %s\n", $this->argv[0], $pid);
}else{
printf("%s haven't running\n", $this->argv[0]);
}
}
private function start()
{
$pid = $this->daemon();
$conf = \Yaf\Registry::get('config')->get('redis.database.params');
$http = new swoole_http_server("0.0.0.0", 9501);
$http->on('request', function ($request, $response) {
try{
$respData=$request->post;
// echo json_encode($respData);
/**
* 测试在 $server 外部注册全局自定义属性, 看看会不会被覆盖
*/
if(isset($respData['type'])&&$respData['type']==1){
$info=$respData['content'];
$memberDao=$info['className']::getInstance(\Our\DbNameConst::masterDBConnectName);
echo json_encode($info);
if(empty($info['params'])){
$res=call_user_func_array(array($memberDao,$info['method']),array());
}else{
$res=call_user_func_array(array($memberDao,$info['method']),$info['params']);
}
$res=(isset($res)&&!empty($res))?$res:false;
unset($memberDao);
\Mysql\LinkMySQLModel::unsetDbConecet();
if($res!==false){
echo 'success';
$response->end(json_encode(array('status'=>1,'message'=>'执行成功','data'=>$res)));
}else{
echo 'fail1';
$response->end(json_encode(array('status'=>0,'message'=>'执行失败')));
}
}else{
echo 'fail2';
$response->end(json_encode(array('status'=>0,'message'=>'执行失败')));
}
}catch(Exception $ex){
throw new Exception($ex->getMessage(),$ex->getCode());
}
});
$http->start();
$http->myWorkerVar = 'global';
}
private function reload(){
if (file_exists($this->pidfile)) {
$pid = file_get_contents($this->pidfile);
//posix_kill(posix_getpid(), SIGHUP);
posix_kill($pid, SIGHUP);
}
}
protected function restart(){
$this->stop();
sleep(1);
$this->start();
}
$http->set(array(
'worker_num' => 5,
'daemonize' => false,
));
private function stop()
{
// 服务器启动时执行一次
$http->on('Start', function (swoole_http_server $server) {
echo '';
});
if (file_exists($this->pidfile)) {
$pid = file_get_contents($this->pidfile);
posix_kill($pid, 9);
unlink($this->pidfile);
}
}
// 服务器启动时执行一次
$http->on('ManagerStart', function (swoole_http_server $server) {
echo 'ManagerStart: ' . PHP_EOL . PHP_EOL;
});
private function help($proc)
{
printf("%s start | stop | help | restart | reload \n", $proc);
}
private $argv;
public function main($argv)
{
$this->argv=$argv;
if (count($argv) < 2) {
printf("please input help parameter\n");
exit();
}
if ($argv[1] === 'stop') {
// 每个 Worker 进程启动或重启时都会执行
$http->on('WorkerStart', function (swoole_http_server $server, $workerId) {
// 通过重新加载外部文件来重载代码和释放之前占用的内存
//include_once __DIR__ . DIRECTORY_SEPARATOR .'workerstart.php';
// 下面这些直接写在当前文件中的代码即便重载也不会变化
echo 'WorkerStart: ' . PHP_EOL . PHP_EOL;
echo ' Worker ID: ' . $workerId . PHP_EOL . PHP_EOL;
// 启动服务器后, 去掉下面这行注释, 然后 reload , 该语句也不会执行的
//echo ' reloaded ! ' . PHP_EOL . PHP_EOL;
// 应该把这里的回调事件代码写在另一个文件中来 include 而不是直接写在这里
// 注意即便是 include_once , reload 也会重新加载的, 但在你的逻辑控制中是有效的
});
// 每次连接时(相当于每个浏览器第一次打开页面时)执行一次, reload 时连接不会断开, 也就不会再次触发该事件
$http->on('Connect', function (swoole_http_server $server, $fd, $reactorThreadId) {
echo 'Worker ID: '. $server->worker_id . ';'.'fd: ' . $fd . ' , fromId: ' . $reactorThreadId . PHP_EOL;
});
// 浏览器连接服务器后, 页面上的每个请求均会执行一次,
// 每次打开链接页面默认都是接收两个请求, 一个是正常的数据请求, 一个 favicon.ico 的请求
$http->on('request', function ($request, $response) use($http) {
require_once APPLICATION_PATH . '/scripts/crontab/common.php';
require APPLICATION_PATH . '/scripts/crontab/push/bridgeBase.php';
});
$http->start();
$this->stop();
} else if ($argv[1] === 'start') {
$this->start();
} else if($argv[1] === 'restart'){
$this->restart();
}else if($argv[1] === 'status'){
$this->status();
}else if($argv[1]==='reload'){
$this->reload();
}
else{
$this->help($argv[0]);
}
}
}
$cgse = new bridgeStart();
$cgse->main($argv);
\ No newline at end of file
<?php
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../../../')); //指向public的上一级
require APPLICATION_PATH . '/scripts/crontab/baseCli.php';
require APPLICATION_PATH . '/scripts/crontab/common.php';
error_reporting(E_ALL ^ E_NOTICE);
class bridgeStart
{
/* config */
const LISTEN = "tcp://192.168.2.15:5555";
const MAXCONN = 100;
const pidfile = __CLASS__;
const uid = 81;
const gid = 81;
/**/
protected $pool = NULL;
protected $zmq = NULL;
public function __construct()
{
$this->pidfile = '/var/run/' . self::pidfile . '.pid';
}
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());
}
}
public function callback($instance,$channelName,$message){
$orderService=\Business\Order\OrderServiceModel::getInstance();
$orderService->testPush($message);
}
protected function status(){
if (file_exists($this->pidfile)) {
$pid = file_get_contents($this->pidfile);
printf("%s already running, pid = %s\n", $this->argv[0], $pid);
}else{
printf("%s haven't running\n", $this->argv[0]);
}
}
private function start()
{
$pid = $this->daemon();
$conf = \Yaf\Registry::get('config')->get('redis.database.params');
$http = new swoole_http_server("0.0.0.0", 9501);
$http->on('request', function ($request, $response) {
try{
$respData=$request->post;
// echo json_encode($respData);
if(isset($respData['type'])&&$respData['type']==1){
$info=$respData['content'];
$memberDao=$info['className']::getInstance(\Our\DbNameConst::masterDBConnectName);
echo json_encode($info);
if(empty($info['params'])){
$res=call_user_func_array(array($memberDao,$info['method']),array());
}else{
$res=call_user_func_array(array($memberDao,$info['method']),$info['params']);
}
$res=(isset($res)&&!empty($res))?$res:false;
unset($memberDao);
\Mysql\LinkMySQLModel::unsetDbConecet();
if($res!==false){
echo 'success';
$response->end(json_encode(array('status'=>1,'message'=>'执行成功','data'=>$res)));
}else{
echo 'fail1';
$response->end(json_encode(array('status'=>0,'message'=>'执行失败')));
}
}else{
echo 'fail2';
$response->end(json_encode(array('status'=>0,'message'=>'执行失败')));
}
}catch(Exception $ex){
throw new Exception($ex->getMessage(),$ex->getCode());
}
});
$http->start();
}
private function reload(){
if (file_exists($this->pidfile)) {
$pid = file_get_contents($this->pidfile);
//posix_kill(posix_getpid(), SIGHUP);
posix_kill($pid, SIGHUP);
}
}
protected function restart(){
$this->stop();
sleep(1);
$this->start();
}
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 | help | restart | reload \n", $proc);
}
private $argv;
public function main($argv)
{
$this->argv=$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] === 'restart'){
$this->restart();
}else if($argv[1] === 'status'){
$this->status();
}else if($argv[1]==='reload'){
$this->reload();
}
else{
$this->help($argv[0]);
}
}
}
$cgse = new bridgeStart();
$cgse->main($argv);
\ No newline at end of file
......@@ -33,6 +33,7 @@ function callback($instance,$channelName,$message){
pcntl_signal_dispatch();
if (Signal::get() == SIGHUP) {
$instance->unsubscribe(array($channelName));
$instance->close();
Signal::reset();
}
......
......@@ -80,6 +80,7 @@ class Daemon extends Logger {
//echo "\n This signal is called. [$signo] \n";
printf("The process has been reload.\n");
Signal::set($signo);
$this->run();
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment