Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jul 29, 2018
1 parent d9ea05d commit d49c0e0
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/Swoole/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/7/29
* Time: 下午3:37
*/

namespace EasySwoole\EasySwoole\Swoole\Process;


use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\EasySwoole\Swoole\Time\Time;
use Swoole\Process;

abstract class AbstractProcess
{
private $swooleProcess;
private $processName;
private $async = null;
private $args = [];

function __construct(string $processName,array $args,$async = true)
{
$this->async = $async;
$this->args = $args;
$this->processName = $processName;
$this->swooleProcess = new \swoole_process([$this,'__start'],false,2);
ServerManager::getInstance()->getSwooleServer()->addProcess($this->swooleProcess);
}

public function getProcess():Process
{
return $this->swooleProcess;
}

/*
* 仅仅为了提示:在自定义进程中依旧可以使用定时器
*/
public function addTick($ms,callable $call):?int
{
return Time::loop(
$ms,$call
);
}

public function clearTick(int $timerId)
{
Time::clear($timerId);
}

public function delay($ms,callable $call):?int
{
return Time::delay(
$ms,$call
);
}

/*
* 服务启动后才能获得到pid
*/
public function getPid():?int
{
if(isset($this->swooleProcess->pid)){
return $this->swooleProcess->pid;
}else{
return null;
}
}

function __start(Process $process)
{
if(PHP_OS != 'Darwin'){
$process->name($this->getProcessName());
}

if (extension_loaded('pcntl')) {
pcntl_async_signals(true);
}

Process::signal(SIGTERM,function ()use($process){
$this->onShutDown();
swoole_event_del($process->pipe);
$this->swooleProcess->exit(0);
});
if($this->async){
swoole_event_add($this->swooleProcess->pipe, function(){
$msg = $this->swooleProcess->read(64 * 1024);
$this->onReceive($msg);
});
}
$this->run($this->swooleProcess);
}

public function getArgs():array
{
return $this->args;
}

public function getArg($key)
{
if(isset($this->args[$key])){
return $this->args[$key];
}else{
return null;
}
}

public function getProcessName()
{
return $this->processName;
}

public abstract function run(Process $process);
public abstract function onShutDown();
public abstract function onReceive(string $str);

}
24 changes: 24 additions & 0 deletions src/Swoole/Task/TaskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\EasySwoole\Swoole\PipeMessage\Message;
use EasySwoole\Trigger\Trigger;

class TaskManager
Expand All @@ -27,6 +28,29 @@ public static function async($task,$finishCallback = null,$taskWorkerId = -1)
return ServerManager::getInstance()->getSwooleServer()->task($task,$taskWorkerId,$finishCallback);
}

public static function processAsync($task)
{
$conf = ServerManager::getInstance()->getSwooleServer()->setting;
if(!isset($conf['task_worker_num'])){
return false;
}
$taskNum = $conf['task_worker_num'];
$workerNum = $conf['worker_num'];
if($task instanceof \Closure){
try{
$task = new SuperClosure($task);
}catch (\Throwable $throwable){
Trigger::throwable($throwable);
return false;
}
}
$message = new Message();
$message->setCommand('TASK');
$message->setData($task);
$workerId = mt_rand($workerNum,($workerNum+$taskNum)-1);
ServerManager::getInstance()->getSwooleServer()->sendMessage(\swoole_serialize::pack($message),$workerId);
return true;
}

public static function sync($task,$timeout = 0.5,$taskWorkerId = -1)
{
Expand Down

0 comments on commit d49c0e0

Please sign in to comment.