easy-swoole / easyswoole

swoole,easyswoole,swoole framework
https://www.easyswoole.com/
Apache License 2.0
4.74k stars 510 forks source link

task异步返回-2 #453

Open candivan opened 3 years ago

candivan commented 3 years ago

swoole version 4.6.2 php version 7.4.14 easyswoole version 3.4.3-dev

'TASK' => [ 'workerNum' => 1, 'maxRunningNum' => 2, //这边设置2 下面例子测试100个 只有两个是正常的 其他都是返回-2 'timeout' => 1, ]

` class CustomTask implements TaskInterface {

protected $data;

public function __construct($data)
{
    // 保存投递过来的数据
    $this->data = $data;
}

function run(int $taskId, int $workerIndex)
{
    // TODO: Implement run() method.
   //  var_dump("t=$taskId" . ",w=$workerIndex" . ",接收到数据" . json_encode($this->data));
    co::sleep(0.1); //这边sleep就会出现-2
    return "ok";
}

function onException(\Throwable $throwable, int $taskId, int $workerIndex)
{
    // TODO: Implement onException() method.
    $s = sprintf("%s:%d %s (%d) [%s]", $throwable->getFile(), $throwable->getLine(), $throwable->getMessage(), $throwable->getCode(), get_class($throwable));
    Logger::getInstance()->error("t=" . $taskId . ",w=" . $workerIndex . "d=" . $s);
}

} `

测试100个任务 ` $task = \EasySwoole\EasySwoole\Task\TaskManager::getInstance();

    $data = [];
    for ($i = 1; $i < 100; $i++) {
        $data[] = 'custom' . $i;
    }

    foreach ($data as $k => $v) {
        $result = $task->async(new CustomTask(['user' => $v]), function ($reply, $taskId, $workerIndex) {
            echo "r=$reply" . ",t=$taskId", ",w=$workerIndex";
        });
        var_dump("返回值:" . $result);
    }

`

Player626 commented 3 years ago

-2属于对的啊.你task进程只有1个,而maxRunningNum是2.只允许最大同时跑2task

candivan commented 3 years ago

-2属于对的啊.你task进程只有1个,而maxRunningNum是2.只允许最大同时跑2task

嗯 看代码注释 是正确的 只是之前看文档说明是错的(https://www.easyswoole.com/Components/Component/task.html -2 投递数据解包失败,当投递数据传输时数据异常时会报错,此错误为组件底层错误,一般不会出现)还有个疑问是 投递任务为什么不是放队列去执行而是让他失败

kiss291323003 commented 3 years ago

文档这个会跟进下,因为队列的话,涉及到万一服务重启导致任务丢失,而业务层认为已经投递了一定会执行,那就是问题了