Swoole\Server::task(): task method can't be executed without task worker

来源:8-17 赛事直播功能开发-优化

慕少5137873

2019-11-05

在调用task的时候报错,这是为什么呢

<?php


namespace app\admin\controller;


use app\common\lib\redis\Predis;
use app\common\lib\Util;

class Live
{
    public function push(){
        if(empty($_GET)){
            return Util::show(config('code.error'), 'error');
        }
        //print_r($_GET);
        //token md5(content)
        $teams = [
            1=>[
              'name' => '马刺',
              'logo' => '/live/imgs/tem1.png',
            ],
            4 => [
              'name' => '火箭',
              'logo' => '/live/imgs/team2.png',
            ],
        ];
        $data = [
            'type' => intval($_GET['type']),
            'title' => !empty($teamas[$_GET['team_id']]['name']) ? $teams[$_GET['team_id']]['name'] : '直播员',
            'logo' => !empty($teamas[$_GET['team_id']]['logo']) ? $teams[$_GET['team_id']]['logo'] : '',
            'content' => !empty($_GET['content']) ? $_GET['content'] : '',
            'image' => !empty($_GET['image']) ? $_GET['image'] : '',
        ];

        //修改为task任务发布
        $taskData = [
            'method' => 'pushLive',
            'data' => $data,
        ];
        $_POST['http_server']->task($taskData, -1);
        return Util::show(config('code.success'), 'ok');

        //赛况的基本信息
        //获取连接的用户,push 到直播页面

    }

}




<?php


use think\App;

class Ws
{
    CONST HOST = '0.0.0.0';
    CONST PORT = 8811;

    public $ws = null;

    public function __construct()
    {
        //判断redis里面有值
       // \app\common\lib\redis\Predis::getInstance()->del(config('redis.live_redis_key'));
        $this->ws = new  Swoole\Websocket\Server(self::HOST, self::PORT);
        $this->ws->set(
            [
                'enable_static_handler' => true,
                'document_root' => '/data/wwwroot/default/swoole_tp/public/static',
                'worker_num' => 5,
                'task_work_num'=> 4 ,
            ]
        );
        $this->ws->on('open', [$this, 'onOpen']);
        $this->ws->on('message', [$this, 'onMessage']);
        $this->ws->on('WorkerStart', [$this, 'onWorkerStart']);
        $this->ws->on("request", [$this, 'onRequest']);
        $this->ws->on("task", [$this, 'onTask']);
        $this->ws->on("finish", [$this, 'onFinish']);
        $this->ws->on("close", [$this, 'onClose']);

        $this->ws->start();
    }

    /**
     * @param $ws
     * @param $request
     */
    public function onOpen($ws, $request) {
        //todo
        //fd redis [1, 2]
        \app\common\lib\redis\Predis::getInstance()->sAdd(config('redis.live_redis_key'), $request->fd);

    }

    /**
     * @param $server
     * @param $worker_id
     */
    public function onWorkerStart($server, $worker_id){
        //定义应用目录
        define('APP_PATH', __DIR__ . '/../application/');
        //加载框架里的文件
        //require __DIR__ . '/../thinkphp/base.php';
        require __DIR__ . '/../thinkphp/start.php';
    }

    /**
     * request 回调
     * @param $request
     * @param $response
     */
    public function onRequest($request, $response){
        $_SERVER = [];
        //var_dump($request->server);
        if (isset($request->server)) {
            foreach ($request->server as $k => $v) {
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        if (isset($request->header)) {
            foreach ($request->header as $k => $v) {
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        $_GET = [];
        if (isset($request->get)) {
            foreach ($request->get as $k => $v) {
                $_GET[$k] = $v;
            }
        }
        $_FILES = [];
        if (isset($request->files)) {
            foreach ($request->files as $k => $v) {
                $_FILES[$k] = $v;
            }
        }
        $_POST = [];
        if (isset($request->post)) {
            foreach ($request->post as $k => $v) {
                $_POST[$k] = $v;
            }
        }
        $_POST['http_server'] = $this->ws;
        ob_start();
        try {
            think\App::run()->send();
        } catch (\Exception $e) {
            print_r($e);
        }
        $res = ob_get_contents();
        if (ob_get_length() > 0) {
            ob_end_clean();
        }
        $response->end($res);
    }

    /**
     * @param $serv
     * @param $taskId
     * @param $workerId
     * @param $data
     * @return bool
     */
    public function onTask($serv, $taskId, $worker_id, $data){
        // 分发 task任务机制, 让不同的任务,走不同的逻辑
         $obj = new app\common\lib\task\Task;
         $method = $data['method'];
         $flag = $obj->$method($data['data'], $serv);
         return $flag;

    }

    /**
     * @param $serv
     * @param $taskId
     * @param $data
     */
    public function onFinish($serv, $taskId, $data){
        //todo
    }

    /**
     * @param $serv
     * @param $fd
     */
    public function onClose($serv, $fd){
        //todo
        //把用户连接 fd del
        \app\common\lib\redis\Predis::getInstance()->sRem(config('redis.live_redis_key'), $fd);
    }

    /**
     * @param $ws
     * @param $frame
     */
    public function onMessage($ws, $frame){
        //todo
        //$ws->push();
    }
}

new Ws();

报错信息

think\exception\ErrorException Object
(
    [severity:protected] => 2
    [data:protected] => Array
        (
            [Error Context] => Array
                (
                    [data] => Array
                        (
                            [type] => 1
                            [title] => 直播员
                            [logo] => 
                            [content] => vvvvvv
                            [image] => 
                        )

                    [taskData] => Array
                        (
                            [method] => pushLive
                            [data] => Array
                                (
                                    [type] => 1
                                    [title] => 直播员
                                    [logo] => 
                                    [content] => vvvvvv
                                    [image] => 
                                )

                        )

                )

        )

    [message:protected] => Swoole\Server::task(): task method can't be executed without task worker
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /data/wwwroot/default/swoole_tp/application/admin/controller/Live.php
    [line:protected] => 41
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [function] => appError
                    [class] => think\Error
                    [type] => ::
                    [args] => Array
                        (
                            [0] => 2
                            [1] => Swoole\Server::task(): task method can't be executed without task worker
                            [2] => /data/wwwroot/default/swoole_tp/application/admin/controller/Live.php
                            [3] => 41
                            [4] => Array
                                (
                                    [data] => Array
                                        (
                                            [type] => 1
                                            [title] => 直播员
                                            [logo] => 
                                            [content] => vvvvvv
                                            [image] => 
                                        )

                                    [taskData] => Array
                                        (
                                            [method] => pushLive
                                            [data] => Array
                                                (
                                                    [type] => 1
                                                    [title] => 直播员
                                                    [logo] => 
                                                    [content] => vvvvvv
                                                    [image] => 
                                                )

                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [file] => /data/wwwroot/default/swoole_tp/application/admin/controller/Live.php
                    [line] => 41
                    [function] => task
                    [class] => Swoole\Server
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [method] => pushLive
                                    [data] => Array
                                        (
                                            [type] => 1
                                            [title] => 直播员
                                            [logo] => 
                                            [content] => vvvvvv
                                            [image] => 
                                        )

                                )

                            [1] => -1
                        )

                )

            [2] => Array
                (
                    [function] => push
                    [class] => app\admin\controller\Live
                    [type] => ->
                    [args] => Array
                        (
                        )

                )

            [3] => Array
                (
                    [file] => /data/wwwroot/default/swoole_tp/thinkphp/library/think/App.php
                    [line] => 196
                    [function] => invokeArgs
                    [class] => ReflectionMethod
                    [type] => ->
                    [args] => Array
                        (
                            [0] => app\admin\controller\Live Object
                                (
                                )

                            [1] => Array
                                (
                                )

                        )

                )

            [4] => Array
                (
                    [file] => /data/wwwroot/default/swoole_tp/thinkphp/library/think/App.php
                    [line] => 408
                    [function] => invokeMethod
                    [class] => think\App
                    [type] => ::
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [0] => app\admin\controller\Live Object
                                        (
                                        )

                                    [1] => push
                                )

                            [1] => Array
                                (
                                )

                        )

                )

            [5] => Array
                (
                    [file] => /data/wwwroot/default/swoole_tp/thinkphp/library/think/App.php
                    [line] => 295
                    [function] => module
                    [class] => think\App
                    [type] => ::
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [0] => admin
                                    [1] => live
                                    [2] => push
                                )

                            [1] => Array
                                (
                                    [app_debug] => 1
                                    [app_trace] => 
                                    [app_status] => 
                                    [app_multi_module] => 1
                                    [auto_bind_module] => 
                                    [root_namespace] => Array
                                        (
                                        )

                                    [extra_file_list] => Array
                                        (
                                            [0] => /data/wwwroot/default/swoole_tp/thinkphp/helper.php
                                        )

                                    [default_return_type] => html
                                    [default_ajax_return] => json
                                    [default_jsonp_handler] => jsonpReturn
                                    [var_jsonp_handler] => callback
                                    [default_timezone] => PRC
                                    [lang_switch_on] => 
                                    [default_filter] => 
                                    [default_lang] => zh-cn
                                    [class_suffix] => 
                                    [controller_suffix] => 
                                    [default_module] => index
                                    [deny_module_list] => Array
                                        (
                                            [0] => common
                                        )

                                    [default_controller] => Index
                                    [default_action] => index
                                    [default_validate] => 
                                    [empty_controller] => Error
                                    [use_action_prefix] => 
                                    [action_suffix] => 
                                    [controller_auto_search] => 
                                    [var_pathinfo] => s
                                    [pathinfo_fetch] => Array
                                        (
                                            [0] => ORIG_PATH_INFO
                                            [1] => REDIRECT_PATH_INFO
                                            [2] => REDIRECT_URL
                                        )

                                    [pathinfo_depr] => /
                                    [url_html_suffix] => html
                                    [url_common_param] => 
                                    [url_param_type] => 0
                                    [url_route_on] => 1
                                    [route_config_file] => Array
                                        (
                                            [0] => route
                                        )

                                    [route_complete_match] => 
                                    [url_route_must] => 
                                    [url_domain_deploy] => 
                                    [url_domain_root] => 
                                    [url_convert] => 1
                                    [url_controller_layer] => controller
                                    [var_method] => _method
                                    [var_ajax] => _ajax
                                    [var_pjax] => _pjax
                                    [request_cache] => 
                                    [request_cache_expire] => 
                                    [request_cache_except] => Array
                                        (
                                        )

                                    [template] => Array
                                        (
                                            [type] => Think
                                            [view_path] => 
                                            [view_suffix] => html
                                            [view_depr] => /
                                            [tpl_begin] => {
                                            [tpl_end] => }
                                            [taglib_begin] => {
                                            [taglib_end] => }
                                        )

                                    [view_replace_str] => Array
                                        (
                                        )

                                    [dispatch_success_tmpl] => /data/wwwroot/default/swoole_tp/thinkphp/tpl/dispatch_jump.tpl
                                    [dispatch_error_tmpl] => /data/wwwroot/default/swoole_tp/thinkphp/tpl/dispatch_jump.tpl
                                    [exception_tmpl] => /data/wwwroot/default/swoole_tp/thinkphp/tpl/think_exception.tpl
                                    [error_message] => 页面错误!请稍后再试~
                                    [show_error_msg] => 
                                    [exception_handle] => 
                                    [log] => Array
                                        (
                                            [type] => File
                                            [path] => /data/wwwroot/default/swoole_tp/runtime/log/
                                            [level] => Array
                                                (
                                                )

                                        )

                                    [trace] => Array
                                        (
                                            [type] => Html
                                        )

                                    [cache] => Array
                                        (
                                            [type] => File
                                            [path] => /data/wwwroot/default/swoole_tp/runtime/cache/
                                            [prefix] => 
                                            [expire] => 0
                                        )

                                    [session] => Array
                                        (
                                            [id] => 
                                            [var_session_id] => 
                                            [prefix] => think
                                            [type] => 
                                            [auto_start] => 1
                                        )

                                    [cookie] => Array
                                        (
                                            [prefix] => 
                                            [expire] => 0
                                            [path] => /
                                            [domain] => 
                                            [secure] => 
                                            [httponly] => 
                                            [setcookie] => 1
                                        )

                                    [database] => Array
                                        (
                                            [type] => mysql
                                            [dsn] => 
                                            [hostname] => 127.0.0.1
                                            [database] => 
                                            [username] => root
                                            [password] => 
                                            [hostport] => 
                                            [params] => Array
                                                (
                                                )

                                            [charset] => utf8
                                            [prefix] => 
                                            [debug] => 1
                                            [deploy] => 0
                                            [rw_separate] => 
                                            [master_num] => 1
                                            [slave_no] => 
                                            [fields_strict] => 1
                                            [resultset_type] => array
                                            [auto_timestamp] => 
                                            [datetime_format] => Y-m-d H:i:s
                                            [sql_explain] => 
                                        )

                                    [paginate] => Array
                                        (
                                            [type] => bootstrap
                                            [var_page] => page
                                            [list_rows] => 15
                                        )

                                    [app_namespace] => app
                                    [code] => Array
                                        (
                                            [success] => 1
                                            [error] => 0
                                        )

                                    [live] => Array
                                        (
                                            [host] => http://106.13.78.196:8811
                                        )

                                    [queue] => Array
                                        (
                                            [connector] => Sync
                                        )

                                    [redis] => Array
                                        (
                                            [host] => 127.0.0.1
                                            [port] => 6379
                                            [out_time] => 480
                                            [timeOut] => 5
                                            [live_redis_key] => live_game_key
                                        )

                                )

                            [2] => 1
                        )

                )

            [6] => Array
                (
                    [file] => /data/wwwroot/default/swoole_tp/thinkphp/library/think/App.php
                    [line] => 123
                    [function] => exec
                    [class] => think\App
                    [type] => ::
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [type] => module
                                    [module] => Array
                                        (
                                            [0] => admin
                                            [1] => live
                                            [2] => push
                                        )

                                )

                            [1] => Array
                                (
                                    [app_debug] => 1
                                    [app_trace] => 
                                    [app_status] => 
                                    [app_multi_module] => 1
                                    [auto_bind_module] => 
                                    [root_namespace] => Array
                                        (
                                        )

                                    [extra_file_list] => Array
                                        (
                                            [0] => /data/wwwroot/default/swoole_tp/thinkphp/helper.php
                                        )

                                    [default_return_type] => html
                                    [default_ajax_return] => json
                                    [default_jsonp_handler] => jsonpReturn
                                    [var_jsonp_handler] => callback
                                    [default_timezone] => PRC
                                    [lang_switch_on] => 
                                    [default_filter] => 
                                    [default_lang] => zh-cn
                                    [class_suffix] => 
                                    [controller_suffix] => 
                                    [default_module] => index
                                    [deny_module_list] => Array
                                        (
                                            [0] => common
                                        )

                                    [default_controller] => Index
                                    [default_action] => index
                                    [default_validate] => 
                                    [empty_controller] => Error
                                    [use_action_prefix] => 
                                    [action_suffix] => 
                                    [controller_auto_search] => 
                                    [var_pathinfo] => s
                                    [pathinfo_fetch] => Array
                                        (
                                            [0] => ORIG_PATH_INFO
                                            [1] => REDIRECT_PATH_INFO
                                            [2] => REDIRECT_URL
                                        )

                                    [pathinfo_depr] => /
                                    [url_html_suffix] => html
                                    [url_common_param] => 
                                    [url_param_type] => 0
                                    [url_route_on] => 1
                                    [route_config_file] => Array
                                        (
                                            [0] => route
                                        )

                                    [route_complete_match] => 
                                    [url_route_must] => 
                                    [url_domain_deploy] => 
                                    [url_domain_root] => 
                                    [url_convert] => 1
                                    [url_controller_layer] => controller
                                    [var_method] => _method
                                    [var_ajax] => _ajax
                                    [var_pjax] => _pjax
                                    [request_cache] => 
                                    [request_cache_expire] => 
                                    [request_cache_except] => Array
                                        (
                                        )

                                    [template] => Array
                                        (
                                            [type] => Think
                                            [view_path] => 
                                            [view_suffix] => html
                                            [view_depr] => /
                                            [tpl_begin] => {
                                            [tpl_end] => }
                                            [taglib_begin] => {
                                            [taglib_end] => }
                                        )

                                    [view_replace_str] => Array
                                        (
                                        )

                                    [dispatch_success_tmpl] => /data/wwwroot/default/swoole_tp/thinkphp/tpl/dispatch_jump.tpl
                                    [dispatch_error_tmpl] => /data/wwwroot/default/swoole_tp/thinkphp/tpl/dispatch_jump.tpl
                                    [exception_tmpl] => /data/wwwroot/default/swoole_tp/thinkphp/tpl/think_exception.tpl
                                    [error_message] => 页面错误!请稍后再试~
                                    [show_error_msg] => 
                                    [exception_handle] => 
                                    [log] => Array
                                        (
                                            [type] => File
                                            [path] => /data/wwwroot/default/swoole_tp/runtime/log/
                                            [level] => Array
                                                (
                                                )

                                        )

                                    [trace] => Array
                                        (
                                            [type] => Html
                                        )

                                    [cache] => Array
                                        (
                                            [type] => File
                                            [path] => /data/wwwroot/default/swoole_tp/runtime/cache/
                                            [prefix] => 
                                            [expire] => 0
                                        )

                                    [session] => Array
                                        (
                                            [id] => 
                                            [var_session_id] => 
                                            [prefix] => think
                                            [type] => 
                                            [auto_start] => 1
                                        )

                                    [cookie] => Array
                                        (
                                            [prefix] => 
                                            [expire] => 0
                                            [path] => /
                                            [domain] => 
                                            [secure] => 
                                            [httponly] => 
                                            [setcookie] => 1
                                        )

                                    [database] => Array
                                        (
                                            [type] => mysql
                                            [dsn] => 
                                            [hostname] => 127.0.0.1
                                            [database] => 
                                            [username] => root
                                            [password] => 
                                            [hostport] => 
                                            [params] => Array
                                                (
                                                )

                                            [charset] => utf8
                                            [prefix] => 
                                            [debug] => 1
                                            [deploy] => 0
                                            [rw_separate] => 
                                            [master_num] => 1
                                            [slave_no] => 
                                            [fields_strict] => 1
                                            [resultset_type] => array
                                            [auto_timestamp] => 
                                            [datetime_format] => Y-m-d H:i:s
                                            [sql_explain] => 
                                        )

                                    [paginate] => Array
                                        (
                                            [type] => bootstrap
                                            [var_page] => page
                                            [list_rows] => 15
                                        )

                                    [app_namespace] => app
                                    [code] => Array
                                        (
                                            [success] => 1
                                            [error] => 0
                                        )

                                    [live] => Array
                                        (
                                            [host] => http://106.13.78.196:8811
                                        )

                                    [queue] => Array
                                        (
                                            [connector] => Sync
                                        )

                                    [redis] => Array
                                        (
                                            [host] => 127.0.0.1
                                            [port] => 6379
                                            [out_time] => 480
                                            [timeOut] => 5
                                            [live_redis_key] => live_game_key
                                        )

                                )

                        )

                )

            [7] => Array
                (
                    [file] => /data/wwwroot/default/swoole_tp/server/ws.php
                    [line] => 99
                    [function] => run
                    [class] => think\App
                    [type] => ::
                    [args] => Array
                        (
                        )

                )

        )

    [previous:Exception:private] => 
)
写回答

1回答

singwa

2019-11-05

亲爱的同学您好。辛苦您代码贴下。

0
2
久居人下1508182909
回复
慕少5137873
需要设置 task 进程数量 例如"task_worker_num" => 4
2020-09-30
共2条回复

Swoole入门到实战打造高性能赛事直播平台

swoole创始人推荐,借助Swoole打造高性能直播平台

2228 学习 · 1062 问题

查看课程