Skip to content

Commit

Permalink
支持tars平台
Browse files Browse the repository at this point in the history
  • Loading branch information
dpp2009 committed May 23, 2018
1 parent 1b9b70d commit 97459b5
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/.env
@@ -1,14 +1,14 @@
# Server
PFILE=/tmp/swoft.pid
PNAME=php-swoft
PNAME=PHPTest.swoft
TCPABLE=true
CRONABLE=false
CRONABLE=true
AUTO_RELOAD=true
AUTO_REGISTER=false

# HTTP
HTTP_HOST=0.0.0.0
HTTP_PORT=80
HTTP_PORT=8011
HTTP_MODE=SWOOLE_PROCESS
HTTP_TYPE=SWOOLE_SOCK_TCP

Expand Down
35 changes: 35 additions & 0 deletions src/app/Listener/APPStart.php
@@ -0,0 +1,35 @@
<?php


namespace App\Listener;

use Swoft\Bean\Annotation\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Task\Event\TaskEvent;
use Swoft\Event\AppEvent;

use App\Tars\Manage;
use Swoft\Memory\Table;


/**
* Task finish handler
*
* @Listener(AppEvent::APPLICATION_LOADER)
*/
class APPStart implements EventHandlerInterface
{
public static $num = 0;
/**
* @param \Swoft\Event\EventInterface $event
*/
public function handle(EventInterface $event)
{
//服务启动 只上报一次 TODO

$manage = new Manage();
$manage->keepAlive();

}
}
77 changes: 77 additions & 0 deletions src/app/Tars/Manage.php
@@ -0,0 +1,77 @@
<?php


namespace App\Tars;

use \Tars\report\ServerFSync;
use \Tars\report\ServerFAsync;
use \Tars\report\ServerInfo;
use \Tars\Utils;

/**
*
*/
class Manage
{

public function getNodeInfo(){
$conf = $this->getTarsConf();
if( !empty($conf) ){
$node = $conf['tars']['application']['server']['node'];
$nodeInfo = Utils::parseNodeInfo($node);
return $nodeInfo;
}else{
return [];
}
}

public function getTarsConf(){
$tars_conf = dirname(BASE_PATH,2).'/conf/'.env('PNAME').'.config.conf';

if( is_file($tars_conf) ){
$conf = Utils::parseFile($tars_conf);
return $conf;
}else{
var_dump('get tars_conf file error : '.$tars_conf);
return [];
}
}

public function keepAlive()
{
$pname = env('PNAME');
$pname = explode('.',$pname);

$adapter = $pname.'.objAdapter';
$application = $pname[0];
$serverName = $pname[1];
$masterPid = getmypid();

$nodeInfo = $this->getNodeInfo();
if( empty($nodeInfo) ){
var_dump('keepAlive getNodeInfo fail');
return null;
}
$host = $nodeInfo['host'];
$port = $nodeInfo['port'];
$objName = $nodeInfo['objName'];

$serverInfo = new ServerInfo();
$serverInfo->adapter = $adapter;
$serverInfo->application = $application;
$serverInfo->serverName = $serverName;
$serverInfo->pid = $masterPid;

$serverF = new ServerFSync($host, $port, $objName);
$serverF->keepAlive($serverInfo);

$adminServerInfo = new ServerInfo();
$adminServerInfo->adapter = 'AdminAdapter';
$adminServerInfo->application = $application;
$adminServerInfo->serverName = $serverName;
$adminServerInfo->pid = $masterPid;
$serverF->keepAlive($adminServerInfo);

var_dump(' keepalive ');
}
}
34 changes: 34 additions & 0 deletions src/app/Tasks/TarsKeepAliveTask.php
@@ -0,0 +1,34 @@
<?php
namespace App\Tasks;
use App\Lib\DemoInterface;
use App\Models\Entity\User;
use Swoft\App;
use Swoft\Bean\Annotation\Inject;
use Swoft\HttpClient\Client;
use Swoft\Redis\Redis;
use Swoft\Rpc\Client\Bean\Annotation\Reference;
use Swoft\Task\Bean\Annotation\Scheduled;
use Swoft\Task\Bean\Annotation\Task;

use App\Tars\Manage;

/**
* TarsKeepAlive task
*
* @Task("tarsKeepAlive")
*/
class TarsKeepAliveTask
{

/**
*
* @Scheduled(cron="*\/30 * * * * *")
*/
public function cronkeepAliveTask()
{

$manage = new Manage();
$manage->keepAlive();
return 'cron';
}
}
22 changes: 14 additions & 8 deletions src/composer.json
Expand Up @@ -29,7 +29,13 @@
"swoft/i18n": "^1.0",
"swoft/process": "^1.0",
"swoft/memory": "^1.0",
"swoft/service-governance": "^1.0"
"swoft/service-governance": "^1.0",
"phptars/tars-registry": "0.1.0",
"phptars/tars-deploy": "0.1.0",
"phptars/tars-utils": "0.1.0",
"phptars/tars-report": "0.1.0",
"phptars/tars-client": "0.1.0",
"phptars/tars2php": "0.1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
Expand All @@ -52,12 +58,12 @@
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"test": "./vendor/bin/phpunit -c phpunit.xml"
"test": "./vendor/bin/phpunit -c phpunit.xml",
"deploy": "\\Tars\\deploy\\Deploy::run"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
"repositories": [
{"type": "composer", "url": "https://raw.githubusercontent.com/Tencent/Tars/phptars/php/dist/tarsphp.json"},
{"type": "composer", "url": "https://packagist.phpcomposer.com"},
{"packagist": false}
]
}
1 change: 1 addition & 0 deletions src/config/properties/app.php
Expand Up @@ -27,6 +27,7 @@
'App\Process',
'App\Fallback',
'App\WebSocket',
'App\Tasks',
],
'I18n' => [
'sourceLanguage' => '@root/resources/messages/',
Expand Down
25 changes: 25 additions & 0 deletions src/index.php
@@ -0,0 +1,25 @@
<?php
// tars 平台然后文件

//读取tars conf配置

//处理合成 env文件

$args = $_SERVER['argv'];


$swoft_bin = dirname(__FILE__).'/bin/swoft ';
$arg_cmd = $args[2]=='start' ? 'start -d' : $args[2] ;

$cmd = "/data/env/runtime/php-7.1.7/bin/php " . $swoft_bin . $arg_cmd;

exec($cmd, $output, $r);









0 comments on commit 97459b5

Please sign in to comment.