<?php
/**
* Created by PhpStorm.
* User: 一只耳丨HABBY
* Date: 2015/11/27
* Time: 17:18
*/
define('FRAMEPATH', __DIR__);
global $argv;
class Server
{
public $server;
public $work_num = 2;
public $task_num = 12;
public function onWorkerStart($server, $worker_id)
{
global $argv;
if ($worker_id >= $server->setting['worker_num']) {
swoole_set_process_name('php ' . $argv[0] . ': task');
} else {
swoole_set_process_name('php ' . $argv[0] . ': worker');
}
}
public function onStart()
{
global $argv;
swoole_set_process_name('php ' . $argv[0] . ': master ');
}
public function onManagerStart()
{
global $argv;
swoole_set_process_name('php ' . $argv[0] . ': manager');
}
public function run()
{
$this->server = new swoole_http_server('0.0.0.0', 777);
$this->server->set(array('worker_num' => 2, 'task_worker_num' => $this->task_num, 'daemonize' => true, 'log_file' => './log_swoole'));
$this->server->on('Start', function () {
$this->onStart();
});
$this->server->on('ManagerStart', function () {
$this->onManagerStart();
});
$this->server->on('WorkerStart', function ($ser, $worker_id) {
$this->onWorkerStart($ser, $worker_id);
});
$this->server->on('Request', function ($request, $response) {
$put_content = $request->rawContent();
$this->server->task($put_content);
$response->end("<h1>Hello Swoole!</h1>");
});
$this->server->on('Task', function ($ser, $task_id, $from_id, $data) {
$data = json_decode($data);
if (empty($data)) {
return false;
}
$project_name = isset($data->project->name) ? $data->project->name : '';
if (empty($project_name)) {
return false;
}
if(!empty($data->ref)){
//执行本地的同步
$path = iniSet::getPathConf($data->project->name);
if(!is_dir($path)){
$update_local_cmd = 'git clone '.$data->project->git_ssh_url.' -b dev '.$path;
}else{
$update_local_cmd = 'cd '.$path.' && git pull';
}
file_put_contents('/www/server/gitlab_hook_swoole/log/'.date('Ymd').'.log',date('Y-m-d H:i:s').':'.$update_local_cmd.PHP_EOL,FILE_APPEND);
exec($update_local_cmd);
exec("chown -R www:www /www/htdocs/gutou_publish/");
//执行本地同步到线上的
$path = '/www/code_git_updata_data/' . $project_name . '.git';
if(!is_dir($path)){
$cmd = 'cd /www/code_git_updata_data/ && git clone --mirror '.$data->repository->git_ssh_url.' && '.'cd '.$project_name.'.git &&';
}else{
$cmd ='cd /www/code_git_updata_data/'.$project_name.'.git && git remote update &&';
}
$cmd .='git push --mirror git@gitlab.dev.epet.com:root/'.$project_name.'.git';
file_put_contents('/www/server/gitlab_hook_swoole/log/'.date('Ymd').'.log',date('Y-m-d H:i:s').':'.$cmd.PHP_EOL,FILE_APPEND);
exec($cmd);
}
});
$this->server->on('Finish', function ($ser, $task_id, $data) {
});
$this->server->start();
}
}
$a = new Server();
$a->run();
/* 读取配置文件 */
class iniSet{
private static $_config = [];
public static function getPathConf($key){
if(!isset(static::$_config[$key]) || empty(static::$_config[$key])){
$path = parse_ini_file('pathConfig.ini');
static::$_config = $path;
}
return static::$_config[$key];
}
}