   'phpDocumentor\Descriptor\FileDescriptor * hash f81245f94c8eca4a0dae5f84b3ab7aac * pathCoroutine/Task.php	 * source<?php
/**
 * 请求的协程
 *
 * @author camera360_server@camera360.com
 * @copyright Chengdu pinguo Technology Co.,Ltd.
 */

namespace PG\MSF\Coroutine;

use Exception;
use PG\MSF\Helpers\Context;
use PG\MSF\Controllers\Controller;
use PG\AOP\MI;

/**
 * Class Task
 * @package PG\MSF\Coroutine
 */
class Task
{
    // use property and method insert
    use MI;

    /**
     * @var \Generator 协程任务的迭代器
     */
    protected $routine;

    /**
     * @var bool 任务销毁标识
     */
    public $destroy = false;

    /**
     * @var \SplStack 协程嵌套栈
     */
    protected $stack;

    /**
     * @var Controller 请求的控制器
     */
    protected $controller;

    /**
     * @var string 任务ID
     */
    protected $id;

    /**
     * @var \Throwable 迭代过程中的异常
     */
    protected $exception;

    /**
     * @var callable|null 迭代完成时执行回调函数
     */
    protected $callBack;

    /**
     * 初始化方法
     *
     * @param \Generator $routine 待调度的迭代器实例
     * @param Context $context 请求的上下文对象
     * @param Controller $controller 当前请求控制器名称
     * @param $callBack callable|null 迭代器执行完成后回调函数
     */
    public function __construct(\Generator $routine, Context &$context, Controller &$controller, callable $callBack = null)
    {
        $this->routine    = $routine;
        $this->context    = $context;
        $this->controller = $controller;
        $this->stack      = new \SplStack();
        $this->id         = $context->getLogId();
        $this->callBack   = $callBack;
    }

    /**
     * 重置迭代器
     *
     * @param \Generator $routine 迭代器实例
     * @return $this
     */
    public function resetRoutine(\Generator $routine = null)
    {
        $this->routine = null;
        $this->routine = $routine;
        return $this;
    }

    /**
     * 获取callback
     *
     * @return mixed
     */
    public function getCallBack()
    {
        return $this->callBack;
    }

    /**
     * 重置callback
     *
     * @param callable|null $callBack 迭代器执行完成后回调函数
     * @return $this
     */
    public function resetCallBack(callable $callBack = null)
    {
        $this->callBack = null;
        $this->callBack = $callBack;
        return $this;
    }

    /**
     * 获取controller
     *
     * @return Controller
     */
    public function getController()
    {
        return $this->controller;
    }

    /**
     * 设置调度时产生的异常
     *
     * @param \Throwable $exception 异常实例
     */
    public function setException(\Throwable $exception)
    {
        $this->exception = $exception;
    }

    /**
     * 请求的协程调度
     */
    public function run()
    {
        try {
            if ($this->exception) {
                throw $this->exception;
            }

            $value = $this->routine->current();
            //嵌套的协程
            if ($value instanceof \Generator) {
                $this->stack->push($this->routine);
                $this->routine = $value;
                $value = null;
                return;
            }

            if ($value != null && $value instanceof IBase) {
                if ($value->isTimeout()) {
                    try {
                        $value->throwTimeOutException();
                    } catch (\Exception $e) {
                        $this->handleTaskTimeout($e, $value);
                    }
                    unset($value);
                    $this->routine->send(false);
                } else {
                    $result = $value->getResult();
                    if ($result !== CNull::getInstance()) {
                        $this->routine->send($result);
                    }

                    while (!$this->routine->valid() && !empty($this->stack) && !$this->stack->isEmpty()) {
                        try {
                            $result = $this->routine->getReturn();
                        } catch (\Throwable $e) {
                            // not todo
                            $result = false;
                        }
                        $this->routine = null;
                        $this->routine = $this->stack->pop();
                        $this->routine->send($result);
                    }
                }
            } else {
                if ($this->routine instanceof \Generator && $this->routine->valid()) {
                    $this->routine->send($value);
                } else {
                    try {
                        $result = $this->routine->getReturn();
                    } catch (\Throwable $e) {
                        // not todo
                        $result = false;
                    }

                    if (!empty($this->stack) && !$this->stack->isEmpty()) {
                        $this->routine = null;
                        $this->routine = $this->stack->pop();
                    }
                    $this->routine->send($result);
                }
            }
        } catch (\Throwable $e) {
            $this->exception = null;
            if (empty($value)) {
                $value = '';
            }
            $runTaskException = $this->handleTaskException($e, $value);

            if ($runTaskException instanceof \Throwable) {
                if ($this->controller) {
                    $this->controller->onExceptionHandle($runTaskException);
                } else {
                    $this->routine->throw($runTaskException);
                }
            }

            unset($value);
        }
    }

    /**
     * 处理协程任务的超时
     *
     * @param \Throwable $e 异常实例
     * @param mixed $value 当前迭代的值
     * @return \Throwable
     */
    public function handleTaskTimeout(\Throwable $e, $value)
    {
        if ($value != '') {
            $logValue = '';
            dumpInternal($logValue, $value, 0, false);
            $message = '' . $logValue . ' message: ' . $e->getMessage();
        } else {
            $message = 'message: ' . $e->getMessage();
        }

        $this->getContext()->getLog()->warning($message);

        if (!empty($value) && $value instanceof IBase && method_exists($value, 'destroy')) {
            $value->destroy();
        }

        return $e;
    }

    /**
     * 处理协程任务的异常
     *
     * @param \Throwable $e 异常实例
     * @param mixed $value 当前迭代的值
     * @return bool|Exception|\Throwable
     */
    public function handleTaskException(\Throwable $e, $value)
    {
        while (!empty($this->stack) && !$this->stack->isEmpty()) {
            $this->routine = $this->stack->pop();
            try {
                $this->routine->throw($e);
                break;
            } catch (\Exception $e) {
            }
        }

        if (!empty($value) && $value instanceof IBase && method_exists($value, 'destroy')) {
            $value->destroy();
        }

        if (!empty($this->stack) && $this->stack->isEmpty()) {
            return $e;
        }

        return true;
    }

    /**
     * [isFinished 判断该task是否完成]
     * @return boolean [description]
     */
    public function isFinished()
    {
        return (empty($this->stack) || $this->stack->isEmpty()) && !$this->routine->valid();
    }

    /**
     * 获取协程任务当前正在运行的迭代器
     *
     * @return \Generator
     */
    public function getRoutine()
    {
        return $this->routine;
    }

    /**
     * 销毁
     */
    public function destroy()
    {
        if (!empty($this->id)) {
            getInstance()->scheduler->taskMap[$this->id] = null;
            unset(getInstance()->scheduler->taskMap[$this->id]);
            getInstance()->scheduler->IOCallBack[$this->id] = null;
            unset(getInstance()->scheduler->IOCallBack[$this->id]);
            $this->stack      = null;
            $this->controller = null;
            $this->id         = null;
            $this->callBack   = null;
        }
    }
}
 * namespaceAliases#phpDocumentor\Descriptor\Collection * items	Exception
\ExceptionContext\PG\MSF\Helpers\Context
Controller\PG\MSF\Controllers\ControllerMI
\PG\AOP\MI * includes	  * constants	  * functions	 
 * classes	\PG\MSF\Coroutine\Task(phpDocumentor\Descriptor\ClassDescriptor	 * parent * implements	  * abstract * final	  * properties	routine+phpDocumentor\Descriptor\PropertyDescriptor" * types 
 * default 	 * static * visibility	protected * fqsen\PG\MSF\Coroutine\Task::routine * name * namespace 
 * package
 * summary * description * fileDescriptor  * line * tags	var	 *phpDocumentor\Descriptor\Tag\VarDescriptor * variableName	 3phpDocumentor\Descriptor\Type\UnknownTypeDescriptor&
\Generator&.*协程任务的迭代器	 * errors	 4	  * inheritedElement destroy"  false!"public$\PG\MSF\Coroutine\Task::destroy&6' ()*+ ,!-	.	 /0	 /phpDocumentor\Descriptor\Type\BooleanDescriptor &.*任务销毁标识4	 4	 5 stack"   !"#$\PG\MSF\Coroutine\Task::stack&<' ()*+ ,&-	.	 /0	 1&	\SplStack&.*协程嵌套栈4	 4	 5 
controller"   !"#$"\PG\MSF\Coroutine\Task::controller&@' ()*+ ,+-	.	 /0	 1&&.*请求的控制器4	 4	 5 id"   !"#$\PG\MSF\Coroutine\Task::id&C' ()*+ ,0-	.	 /0	 .phpDocumentor\Descriptor\Type\StringDescriptor &.*任务ID4	 4	 5 	exception"   !"#$!\PG\MSF\Coroutine\Task::exception&G' ()*+ ,5-	.	 /0	 1&
\Throwable&.*迭代过程中的异常4	 4	 5 callBack"   !"#$ \PG\MSF\Coroutine\Task::callBack&K' ()*+ ,:-	.	 /0	 1&callable1&null&.*!迭代完成时执行回调函数4	 4	 5 
 * methods	__construct)phpDocumentor\Descriptor\MethodDescriptor"!"8 * arguments	$routine+phpDocumentor\Descriptor\ArgumentDescriptor	 * method"p	 1&2   * byReference * isVariadic$&T' ()*待调度的迭代器实例+ , -	 4	 5 $contextUV"p	 1&  WX$&Z' ()*请求的上下文对象+ , -	 4	 5 $controllerUV"p	 1&  WX$&\' ()*当前请求控制器名称+ , -	 4	 5 	$callBackUV"p	  NWX$&^' ()*2callable|null 迭代器执行完成后回调函数+ , -	 4	 5 $%\PG\MSF\Coroutine\Task::__construct()&Q' ()初始化方法*+ ,D-	param	 ,phpDocumentor\Descriptor\Tag\ParamDescriptor0T"t&b*Y4	 c0Z"|&b*[4	 c0\"&b*]4	 c0^"&b*_4	 return	 4	 5 resetRoutineR"!"8S	TUV"	 1&2 NWX$&T' ()*迭代器实例+ , -	 4	 5 $&\PG\MSF\Coroutine\Task::resetRoutine()&e' ()重置迭代器*+ ,T-	b	 c0T"&b*f4	 d	 -phpDocumentor\Descriptor\Tag\ReturnDescriptor	 1&$this&d*4	 4	 5 getCallBackR"!"8S	 $%\PG\MSF\Coroutine\Task::getCallBack()&k' ()获取callback*+ ,`-	d	 i	 1&mixed&d*4	 b	 4	 5 resetCallBackR"!"8S	^UV"	 1&M1&N NWX$&^' ()*$迭代器执行完成后回调函数+ , -	 4	 5 $'\PG\MSF\Coroutine\Task::resetCallBack()&o' ()重置callback*+ ,k-	b	 c0^"&b*p4	 d	 i	 1&j&d*4	 4	 5 getControllerR"!"8S	 $'\PG\MSF\Coroutine\Task::getController()&s' ()获取controller*+ ,w-	d	 i	 1&&d*4	 b	 4	 5 setExceptionR"!"8S	
$exceptionUV#	 1&I  WX$&w' ()*异常实例+ , -	 4	 5 $&\PG\MSF\Coroutine\Task::setException()&v' ()设置调度时产生的异常*+ ,-	b	 c0w#&b*x4	 d	 4	 5 runR"!"8S	 $\PG\MSF\Coroutine\Task::run()&{' ()请求的协程调度*+ ,-	d	 b	 4	 5 handleTaskTimeoutR"!"8S	$eUV#"	 1&I  WX$&' ()*x+ , -	 4	 5 $valueUV#"	 1&n  WX$&' ()*当前迭代的值+ , -	 4	 5 $+\PG\MSF\Coroutine\Task::handleTaskTimeout()&~' ()处理协程任务的超时*+ ,-	b	 c0#&&b*x4	 c0#.&b*4	 d	 i	 1&I&d*4	 4	 5 handleTaskExceptionR"!"8S	UV#I	 1&I  WX$&' ()*x+ , -	 4	 5 UV#I	 1&n  WX$&' ()*+ , -	 4	 5 $-\PG\MSF\Coroutine\Task::handleTaskException()&' ()处理协程任务的异常*+ ,-	b	 c0#M&b*x4	 c0#U&b*4	 d	 i	 : 1&1&I&d*4	 4	 5 
isFinishedR"!"8S	 $$\PG\MSF\Coroutine\Task::isFinished()&' ()&[isFinished 判断该task是否完成]*+ ,-	d	 i	 : &d*[description]4	 b	 4	 5 
getRoutineR"!"8S	 $$\PG\MSF\Coroutine\Task::getRoutine()&' ()0获取协程任务当前正在运行的迭代器*+ ,-	d	 i	 1&2&d*4	 b	 4	 5 6R"!"8S	 $!\PG\MSF\Coroutine\Task::destroy()&6' ()销毁*+ ,&-	d	 b	 4	 5  * usedTraits	 $&Task'\PG\MSF\Coroutine(PG\MSF\Coroutine)
Class Task*+" ,-	package	 &phpDocumentor\Descriptor\TagDescriptor&*4	 
subpackage	 4	 5  * interfaces	 	 * traits	 
 * markers	 $&Task.php' (Default)请求的协程*+ , -	author	 -phpDocumentor\Descriptor\Tag\AuthorDescriptor&*camera360_server@camera360.com4	 	copyright	 &*"Chengdu pinguo Technology Co.,Ltd.4	 	 &*4	 	 4	 5 