Commit 17192060 authored by 夏彬's avatar 夏彬

修改vbot 支持重启退出以及请求重启或者退出

parent 96f71272
<?php
/**
* Created by PhpStorm.
* User: xiabin
* Date: 2018/5/23
* Time: 3:14 PM
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/conf/main.php';
$vbot = new Hanson\Vbot\Foundation\Vbot($options);
$vbot->messageHandler->setHandler(function ($message) use ($vbot) {
$vbot->console->log("收到消息");
});
$vbot->server->restart();
<?php
/**
* Created by PhpStorm.
* User: xiabin
* Date: 2018/5/23
* Time: 3:14 PM
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/conf/main.php';
$vbot = new Hanson\Vbot\Foundation\Vbot($options);
$vbot->messageHandler->setHandler(function ($message) use ($vbot) {
$vbot->console->log("收到消息");
});
$vbot->server->stop();
......@@ -45,6 +45,7 @@ class MessageHandler
if (!$this->handleCheckSync($checkSync[0], $checkSync[1])) {
if ($server) {
$server->shutdown();
} else {
break;
......@@ -89,6 +90,7 @@ class MessageHandler
{
if (in_array($retCode, [1100, 1101, 1102, 1205])) { // 微信客户端上登出或者其他设备登录
$this->vbot->log->error('微信客户端上登出或者其他设备登录或者异常退出');
$this->vbot->console->log('vbot exit normally.');
$this->vbot->cache->forget('session.'.$this->vbot->config['session']);
......
......@@ -44,6 +44,35 @@ class Server
}
}
public function restart()
{
if ($this->vbot->config['swoole.status']) {
$this->vbot->swoole->shutdown();
if (!$this->tryLogin()) {
$this->cleanCookies();
$this->login();
}
$this->init();
if ($this->vbot->config['swoole.status']) {
$this->vbot->swoole->run();
} else {
$this->vbot->messageHandler->listen();
}
}else{
$this->vbot->console->log('swoole 未开启暂不支持');
}
}
public function stop(){
$this->vbot->swoole->shutdown();
}
/**
* 尝试登录.
*
......
......@@ -13,11 +13,28 @@ class Swoole
*/
private $vbot;
/**
* 主进程id
*/
const MASTER_PROCESS_ID_KEY = 'master_process_id_key';
public function __construct(Vbot $vbot)
{
$this->vbot = $vbot;
}
public function shutdown()
{
$pid = $this->vbot->cache->get(self::MASTER_PROCESS_ID_KEY);
$this->vbot->console->log("shutdown pid : $pid");
if ($pid !== NULL) {
if (\swoole_process::kill($pid, 0)) {
\swoole_process::kill($pid);
}
}
}
public function run()
{
$server = new SwooleServer($this->vbot->config->get('swoole.ip', '127.0.0.1'), $this->vbot->config->get('swoole.port', 8866));
......@@ -25,15 +42,69 @@ class Swoole
$handleProcess = new Process(function ($worker) use (&$server) {
$this->vbot->messageHandler->listen($server);
});
$handleProcess->start();
$processId = $handleProcess->start();
$this->vbot->console->log("vbot process id: $processId");
$server->on('receive', function (SwooleServer $server, $fd, $from_id, $data) {
$response = $this->vbot->api->handle($data);
$this->vbot->console->log(" receive data:" . $data);
$request = explode("\r\n\r\n", $data);
if (!$request[1]) {
$response = $this->vbot->api->handle($data);
} else {
$data = json_decode($request[1], TRUE);
if (!isset($data['action']) || !isset($data['params'])) {
$response = $this->vbot->api->handle($data);
} else {
if ($data['action'] === 'system') {
switch ($data['params']) {
case 'shutdown':
$this->vbot->console->log(" system shutdown ");
$response = 'system shutdown';
$server->shutdown();
break;
case 'reload':
$this->vbot->console->log(" system reload ");
$response = 'system reload';
$server->reload();
break;
default:
$response = "system params error not in shutdown or reload";
}
} else {
$response = $this->vbot->api->handle($data);
}
}
}
$response = $this->makeResponse($response);
$server->send($fd, $response);
});
$server->on('shutdown', function (SwooleServer $server) use ($processId) {
$this->vbot->console->log("server shutdown trigger");
$this->vbot->cache->delete(self::MASTER_PROCESS_ID_KEY);
\swoole_process::kill($processId);
});
$server->on('start', function (SwooleServer $server) {
$this->vbot->console->log("server start trigger");
$mid = $server->master_pid;
$this->vbot->console->log("master process id $mid");
$this->vbot->cache->set(self::MASTER_PROCESS_ID_KEY, $mid,1440*30);
});
$server->start();
exit;
}
......@@ -43,15 +114,15 @@ class Swoole
$data = json_encode($data);
$headers = [
'Server' => 'Swoole',
'Content-Type' => 'application/json',
'Server' => 'Swoole',
'Content-Type' => 'application/json',
'Content-Length' => strlen($data),
];
$response[] = 'HTTP/1.1 200';
foreach ($headers as $key => $val) {
$response[] = $key.':'.$val;
$response[] = $key . ':' . $val;
}
$response[] = '';
......
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