Skip to content

Commit

Permalink
路由调度dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Aug 8, 2017
1 parent 9e1af56 commit c2409b9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
}],
"require": {
"monolog/monolog": "1.23.0",
"swiftmailer/swiftmailer":"6.0.1"
"swiftmailer/swiftmailer":"6.0.1",
"smarty/smarty": "3.1.30"
},
"autoload":{
"psr-4":{
Expand Down
7 changes: 2 additions & 5 deletions score/AutoReload/autoReload.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,9 @@ public function init() {
}, true);

$process_pid = $process->start();

$this->swoole_pid = intval($process->read());
var_dump($this->swoole_pid);
swoole_process::wait();

// 测试是否可以发邮件

if(!is_int($this->swoole_pid) || !$this->swoole_pid) {
// 线上记录日志模式和调试模式
$this->putLog("swoole已经停止....,请手动启动swoole!",'error');
Expand All @@ -182,7 +178,8 @@ public function init() {
"body" =>"swoole可能发送错误已经停止,请手动启动"
]);
return;
}
}

}catch(Exception $e) {
// 线上环境这里可以写发邮件通知
$this->putLog("无法检测swoole_pid,无法重启",'error');
Expand Down
20 changes: 20 additions & 0 deletions score/Websocket/Dispatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Swoolefy\Websocket;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class Dispatch {

public $require_uri = null;
/**
* __construct
* @param
*/
public function __construct() {

}

public function dispatch($request, $response) {
$response->end('<h3>swoole is start</h3>');
}
}
83 changes: 70 additions & 13 deletions score/Websocket/Webserver.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace Swoolefy\Websock;
namespace Swoolefy\Websocket;
include_once "../../vendor/autoload.php";

use Swoole\WebSocket\Server as WebSockServer;
use Swoole\Process as swoole_process;
use Swoolefy\Websocket\Dispatch;

class Webserver {

Expand All @@ -18,8 +21,8 @@ class Webserver {
*/
public $conf = [
'reactor_num' => 1, //reactor thread num
'worker_num' => 1, //worker process num
'max_request' => 1,
'worker_num' => 2, //worker process num
'max_request' => 2,
'daemonize' => 0
];

Expand Down Expand Up @@ -49,33 +52,52 @@ class Webserver {
*/
public $monitorPort = 9501;

public $dir = __DIR__;

public function __construct(array $config=[]) {

$conf = array_merge($this->conf,$config);
var_dump($conf);
$this->conf = array_merge($this->conf,$config);

$this->webserver = new WebSockServer($this->host, $this->webPort);

$this->webserver->set($conf);
$this->webserver->set($this->conf);
}

public function start() {
/**
* start回调
*/
$this->webserver->on('Start',function(WebSockServer $server) {
$this->setMasterProcessName();
});

/**
* managerstart回调
*/
$this->webserver->on('ManagerStart',function(WebSockServer $server) {
$this->setManagerProcessName();
});

/**
* 启动worker进程监听回调,设置定时器
*/
$this->webserver->on('WorkerStart',function(WebSockServer $server, $worker_id){
// 创建定时器
$this->timer_id = swoole_timer_tick(3000,[$this,"timer_callback"]);
// 重新设置进程名称
$this->setWorkerProcessName($worker_id);

});

/**
* 接受http请求
*/
$this->webserver->on('request',function($request, $response) {
$tpl = file_get_contents(__DIR__.'/../App/View/test.html');
$response->end($tpl);
});

$this->webserver->on('open', function (WebSockServer $server, $request) {

if($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
return $response->end();
}
// 请求调度
call_user_func_array(array(new Dispatch(), "dispatch"), array($request, $response));
});

$this->webserver->on('message', function (WebSockServer $server, $frame) {
Expand All @@ -88,7 +110,10 @@ public function start() {

$this->webserver->start();
}


/**
* timer_callback
*/
public function timer_callback() {
$process_timer = new swoole_process([$this,'callback_function'], true);
$process_pid = $process_timer->start();
Expand Down Expand Up @@ -116,9 +141,41 @@ public function timer_callback() {
}
}

/**
* callback_function
* @param swoole_process $worker
*/
public function callback_function(swoole_process $worker) {
$worker->exec('/bin/bash', array($this->monitorShellFile,$this->monitorPort));
}

/**
* setWorkerProcessName设置worker进程名称
*/
private function setWorkerProcessName($worker_id) {
// 设置worker的进程
if($worker_id >= $this->conf['worker_num']) {
swoole_set_process_name("php-monitor-task_worker".$worker_id);
}else {
swoole_set_process_name("php-monitor-worker".$worker_id);
}

}

/**
* setMasterProcessName设置主进程名称
*/
private function setMasterProcessName() {
swoole_set_process_name("php-monitor-master");
}

/**
* setManagerProcessName设置管理进程名称
*/
private function setManagerProcessName() {
swoole_set_process_name("php-monitor-manager");
}

}

$websock = new Webserver();
Expand Down
2 changes: 2 additions & 0 deletions score/start.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
include_once "../vendor/autoload.php";

// 设置当前进程的名称
cli_set_process_title("php-autoreload-swoole-server");
// 创建进程服务实例
$daemon = new Swoolefy\daemon();

Expand Down

0 comments on commit c2409b9

Please sign in to comment.