src/Task.php
public static function deliver(
        string $taskName,
        string $methodName,
        array $params = [],
        string $type = self::TYPE_CO,
        int $timeout = 3
    ) {
        if (! in_array($type, [static::TYPE_CO, static::TYPE_ASYNC], false)) {
            throw new TaskException('Invalid task type.');
        }
        $data = TaskHelper::pack($taskName, $methodName, $params, $type);
        if (! App::isWorkerStatus() && ! App::isCoContext()) {
            return static::deliverByQueue($data);
        }

        if (! App::isWorkerStatus() && App::isCoContext()) {
            throw new TaskException('Deliver in non-worker environment, please deliver the task via HTTP request.');
        }

        $server = App::$server->getServer();
        switch ($type) {
            case static::TYPE_CO:
                $tasks[0] = $data;
                $profileKey = 'task' . '.' . $taskName . '.' . $methodName;
                App::profileStart($profileKey);
                $result = $server->taskCo($tasks, $timeout);
                App::profileEnd($profileKey);
                return $result;
                break;
            case static::TYPE_ASYNC:
            default:
                // Deliver async task
                return $server->task($data);
                break;
        }
    }
public static function deliver(
        string $taskName,
        string $methodName,
        array $params = [],
        string $type = self::TYPE_CO,
        int $timeout = 3
    ) {
        if (! in_array($type, [static::TYPE_CO, static::TYPE_ASYNC], false)) {
            throw new TaskException('Invalid task type.');
        }
        $data = TaskHelper::pack($taskName, $methodName, $params, $type);
        if (! App::isWorkerStatus() && ! App::isCoContext()) {
            return static::deliverByQueue($data);
        }

        if (! App::isWorkerStatus() && App::isCoContext()) {
            throw new TaskException('Deliver in non-worker environment, please deliver the task via HTTP request.');
        }

        $server = App::$server->getServer();
        switch ($type) {
            case static::TYPE_CO:
                $tasks[0] = $data;
                $profileKey = 'task' . '.' . $taskName . '.' . $methodName;
                App::profileStart($profileKey);
                $result = $server->taskCo($tasks, $timeout);
                App::profileEnd($profileKey);
                return $result;
                break;
            case static::TYPE_ASYNC:
            default:
                // Deliver async task
                return $server->task($data);
                break;
        }
    }
public static function deliver ( string $ taskName , string $ methodName , array $ params = [ ] , string $ type = self :: TYPE_CO , int $ timeout = 3 ) { if ( ! in_array ( $ type , [ static :: TYPE_CO , static :: TYPE_ASYNC ] , false ) ) { throw new TaskException ( 'Invalid task type.' ) ; } $ data = TaskHelper :: pack ( $ taskName , $ methodName , $ params , $ type ) ; if ( ! App :: isWorkerStatus ( ) && ! App :: isCoContext ( ) ) { return static :: deliverByQueue ( $ data ) ; } if ( ! App :: isWorkerStatus ( ) && App :: isCoContext ( ) ) { throw new TaskException ( 'Deliver in non-worker environment, please deliver the task via HTTP request.' ) ; } $ server = App :: $ server -> getServer ( ) ; switch ( $ type ) { case static :: TYPE_CO : $ tasks [ 0 ] = $ data ; $ profileKey = 'task' . '.' . $ taskName . '.' . $ methodName ; App :: profileStart ( $ profileKey ) ; $ result = $ server -> taskCo ( $ tasks , $ timeout ) ; App :: profileEnd ( $ profileKey ) ; return $ result ; break ; case static :: TYPE_ASYNC : default : // Deliver async task return $ server -> task ( $ data ) ; break ; } }
Deliver a coroutine or async task

@return bool|array|int
@throws TaskException
Deliver a coroutine or async task