php supervisor,Supervisor.php

class Supervisor extends Actor

{

public $strategy='OneForOne';

public $intensity=1;

public $period=-1;

//没用

public static function start($args)

{

return static::spawn_opt([$class,'init'], $args, [

'addr'=> Booru::$reg_addr

]);

}

//没用

public static function start_link($args)

{

return static::spawn_opt([$class,'init'], $args, [

'addr' => Booru::$reg_addr,

'link' => self::self(),

]);

}

public function flags($strategy, $intensity , $period )

{

$this->strategy = $strategy;

$this->intensity = $intensity;

$this->period = $period;

}

/**

* @param [type] $classOrfunc 启动函数

* @param [type] $restart 重启方式

* @param [type] $delay 重启间隔

* @return void

*/

public function worker($classOrfunc, $restart = 0, $delay = 0)

{

$this->opts[] = [ $classOrfunc , [ ] ,[

'addr'=>'worker',//进程池创建

'class'=>Actor::class,

'link'=>$this->self(),

] ];

}

/**

* @param [type] $classOrfunc 启动函数

* @param [type] $restart 重启方式

* @param [type] $delay 重启间隔

* @return void

*/

public function service($classOrfunc, $restart = 0, $delay = 0)

{

$this->opts[] = [ $classOrfunc , [ ] ,[

'addr'=>'service',//新进程创建

'link'=>$this->self(),

'main'=>true,

] ];

}

/**

* @param [type] $classOrfunc 启动函数

* @param [type] $restart 重启方式

* @param [type] $delay 重启间隔

* @return void

*/

public function supervise($classOrfunc, $restart = 0, $delay = 0)

{

$this->opts[] = [ $classOrfunc , [ ] ,[

'addr' => Booru::$reg_addr,//

'link' => $this->self(),

'class'=> Supervisor::class,

] ];

}

public function init()

{

return;

}

public function after($result){

$this->trapexit(true);

yield from $this->{'on'.$this->strategy}();

}

public function onOneForAll(){

while(1){

foreach ($this->opts as $index => $value){

$childs[$index] = yield Actor::spawn(...$value);

}

$coid = yield $this->receive(function($atom){

if($atom[0]=='EXIT'){

return $atom[1];

}

});

foreach ($childs as $index=>$coid) {

$this->unlink($coid);

$this->exit($coid,'restart');

}

yield Actor::delay($this->intensity);

}

}

public function onOneForOne(){

foreach ($this->opts as $index => $value){

$childs[$index] = yield Actor::spawn(...$value);

}

while(1){

$coid = yield $this->receive(function($atom){

if($atom[0]=='EXIT'){

return $atom[1];

}

});

$index = array_search($coid,$childs);

if($index===false){

throw new Exception("Error Processing Request", 1);

}

$childs[$index] = yield Actor::spawn(...$this->opts[$index]);

yield Actor::delay($this->intensity);

}

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值