Skip to content

Commit

Permalink
初步可用版本完成
Browse files Browse the repository at this point in the history
  • Loading branch information
osgochina committed Sep 1, 2016
1 parent 26d4ef3 commit e72197e
Show file tree
Hide file tree
Showing 55 changed files with 119 additions and 1,868 deletions.
14 changes: 11 additions & 3 deletions src/centre/App/Exec.php → src/App/Exec.php
Expand Up @@ -7,19 +7,27 @@
*/

namespace App;


use Lib\LoadTasks;
use Lib\Process;
use Lib\TermLog;

class Exec
{
/**
* 中心服通知worker需要运行任务
* @param $task
* @return bool
*/
public static function run($task)
{
Process::create_process($task);
return Process::create_process($task);
}

/**
* worker回调中心服 任务执行状态
* @param $tasks
* @return array
*/
public static function notify($tasks)
{
if (empty($tasks) || count($tasks) <= 0){
Expand Down
2 changes: 1 addition & 1 deletion src/centre/App/Robot.php → src/App/Robot.php
Expand Up @@ -13,7 +13,7 @@ class Robot
{

/**
* 注册服务
* worker向中心服注册服务
* @param $ip
* @param $port
* @return array
Expand Down
25 changes: 25 additions & 0 deletions src/centre/App/Tasks.php → src/App/Tasks.php
Expand Up @@ -13,6 +13,10 @@
class Tasks
{

/**
* 获取任务列表
* @return array
*/
public static function getList()
{
$data = [];
Expand All @@ -24,6 +28,11 @@ public static function getList()
return array('data' => $data);
}

/**
* 获取单个任务
* @param $id
* @return array
*/
public static function get($id)
{
$tasks = LoadTasks::getTasks();
Expand All @@ -35,6 +44,11 @@ public static function get($id)
return ["code"=>0,"data"=>$task];
}

/**
* 添加任务
* @param $task
* @return array
*/
public static function add($task)
{
$task["id"] = Donkeyid::getInstance()->dk_get_next_id();
Expand All @@ -45,6 +59,12 @@ public static function add($task)
return ["code"=>0,"data"=>$ids,"msg"=>"保存成功"];
}

/**
* 修改任务
* @param $id
* @param $task
* @return array
*/
public static function update($id,$task)
{
if (!LoadTasks::updateTask($id,$task)){
Expand All @@ -53,6 +73,11 @@ public static function update($id,$task)
return ["code"=>0,"data"=>'',"msg"=>"更新成功"];
}

/**
* 删除任务
* @param $id
* @return array
*/
public static function delete($id)
{
if (!LoadTasks::delTask($id)){
Expand Down
@@ -1,5 +1,6 @@
<?php
/**
* 中心服服务
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-19
Expand Down
6 changes: 6 additions & 0 deletions src/centre/Lib/Donkeyid.php → src/Lib/Donkeyid.php
@@ -1,6 +1,7 @@
<?php
namespace Lib;
/**
* 获取唯一id
* Created by PhpStorm.
* User: ClownFish
* Email: 187231450#qq.com
Expand Down Expand Up @@ -106,6 +107,11 @@ public function dk_get_next_id()
return $id;
}

/**
* 解析id,获取id的组成元素
* @param $id
* @return mixed
*/
public function dk_parse_id($id)
{
$ret["time"] = ($id>>self::TIMESTAMP_LEFT_SHIFT)+($this->epoch*1000);
Expand Down
1 change: 1 addition & 0 deletions src/centre/Lib/Flog.php → src/Lib/Flog.php
@@ -1,5 +1,6 @@
<?php
/**
* 记录运行日志
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-19
Expand Down
1 change: 1 addition & 0 deletions src/centre/Lib/LoadTasks.php → src/Lib/LoadTasks.php
@@ -1,6 +1,7 @@
<?php

/**
* task任务的管理类
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-18
Expand Down
@@ -1,6 +1,7 @@
<?php
namespace Lib;
/**
* 解析Crontab格式
* Created by PhpStorm.
* User: ClownFish 187231450@qq.com
* Date: 14-12-27
Expand Down
4 changes: 4 additions & 0 deletions src/centre/Lib/Process.php → src/Lib/Process.php
@@ -1,5 +1,6 @@
<?php
/**
* worker服务中 新创建一个进程去执行命令
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-22
Expand Down Expand Up @@ -91,6 +92,7 @@ public static function notify()
/**
* 创建一个子进程
* @param $task
* @return bool
*/
public static function create_process($task)
{
Expand All @@ -100,7 +102,9 @@ public static function create_process($task)
if (($pid = $process->start())) {
TermLog::log("task开始执行:".json_encode($task),$task["id"]);
self::$table->set($pid,["taskId"=>$task["id"],"status"=>self::PROCESS_START,"start"=>microtime(true)]);
return true;
}
return false;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/centre/Lib/Robot.php → src/Lib/Robot.php
@@ -1,5 +1,6 @@
<?php
/**
* 中心服中的任务分发
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-22
Expand Down Expand Up @@ -65,6 +66,7 @@ public static function clean()
}
}


private static function loadIps()
{
foreach (self::$table as $k=>$v){
Expand All @@ -73,7 +75,7 @@ private static function loadIps()
}

/**
* 运行任务
* 执行任务
* @param $task
* @return bool|null
*/
Expand All @@ -91,6 +93,12 @@ public static function Run($task)
return true;
}

/**
* 分发任务
* @param $robot
* @param $task
* @return bool
*/
private static function sendTask($robot,$task)
{
TermLog::log("task发送给:".$robot["ip"].":".$robot["port"],$task["id"]);
Expand All @@ -110,6 +118,10 @@ private static function sendTask($robot,$task)
return true;
}

/**
* 选择能执行任务的worker
* @return bool
*/
private static function selectWorker()
{
$num = count(self::$ips);
Expand Down
5 changes: 5 additions & 0 deletions src/centre/Lib/Service.php → src/Lib/Service.php
@@ -1,4 +1,9 @@
<?php
/**
* soa服务的客户端工具类
* Class Service
* @package Lib
*/
namespace Lib;
use Swoole;
class Service extends Swoole\Client\SOA
Expand Down
1 change: 1 addition & 0 deletions src/centre/Lib/Tasks.php → src/Lib/Tasks.php
@@ -1,5 +1,6 @@
<?php
/**
* 管理需要处理的任务
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-19
Expand Down
1 change: 1 addition & 0 deletions src/centre/Lib/TermLog.php → src/Lib/TermLog.php
@@ -1,5 +1,6 @@
<?php
/**
* 任务生命周期日志记录
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-31
Expand Down
File renamed without changes.
11 changes: 8 additions & 3 deletions src/centre/Lib/WorkerServer.php → src/Lib/WorkerServer.php
@@ -1,5 +1,6 @@
<?php
/**
* worker 服务
* Created by PhpStorm.
* User: liuzhiming
* Date: 16-8-19
Expand All @@ -11,10 +12,10 @@

class WorkerServer extends Swoole\Protocol\SOAServer
{



public function onMasterStart($serv)
{
//连接中心服注册服务
$this->register();
}

Expand All @@ -40,6 +41,7 @@ public function onWorkerStart($server, $worker_id)
function onTask($serv, $task_id, $from_id, $data)
{
if ($data == "notify"){
//通知中心服
Process::notify();
}
return true;
Expand All @@ -50,6 +52,9 @@ function onFinish($serv, $task_id, $data)
}


/**
* 连接中心服注册服务
*/
public function register()
{
$listenHost = \Lib\Util::listenHost();
Expand All @@ -66,7 +71,7 @@ public function call($request, $header)
//初始化日志
Flog::startLog($request['call']);
Flog::log("call:".$request['call'].",params:".json_encode($request['params']));
$ret = parent::call($request, $header); // TODO: Change the autogenerated stub
$ret = parent::call($request, $header);
Flog::log($ret);
Flog::endLog();
Flog::flush();
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/centre/centre.php → src/centre.php
Expand Up @@ -11,7 +11,6 @@

Swoole\Network\Server::setPidFile(__DIR__ . '/logs/centre.pid');


Swoole\Network\Server::start(function ()
{
$logger = new Swoole\Log\FileLog(['file' => __DIR__ . '/logs/centre.log']);
Expand Down
24 changes: 0 additions & 24 deletions src/centre/App/Cron.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/centre/configs/dev/crontab.php

This file was deleted.

41 changes: 0 additions & 41 deletions src/centre/test/callTest.php

This file was deleted.

0 comments on commit e72197e

Please sign in to comment.