【问题标题】:Problem With Gearman Job StatusGearman 作业状态问题
【发布时间】:2011-08-05 17:04:29
【问题描述】:

我有一个 Gearman 服务器正在运行一个需要几分钟才能完成的进程。我正在运行一个进度条来显示完成情况,并尝试使用 Gearman PHP 扩展和 jobStatus() 函数获取进度条的百分比。

作业肯定处于活动状态并已找到,因为前两个字段(已知 + 仍在运行)返回 true。然而,第三个和第四个字段(完成百分比的分子和分母)什么都没有返回。有谁知道为什么会这样或者这些数字是如何计算的?

【问题讨论】:

    标签: php progress-bar gearman


    【解决方案1】:

    worker 是否配置为返回状态?

    如果您自己编写它们,则必须做一些额外的工作才能让它们在执行过程中返回详细信息。

    【讨论】:

    • 啊,太感谢了,不知道需要配置worker。您可以指出有关如何准确配置工作人员的任何资源?
    • 包是你自己写的吗?收到 Job 后,可以返回状态:php.net/manual/en/gearmanjob.sendstatus.php
    【解决方案2】:
    public bool GearmanJob::sendStatus ( int $numerator , int $denominator )
    

    向作业发送状态信息 服务器和任何监听客户端。采用 这指定什么百分比 作业已完成。

    为了能够使用它,您可能还需要稍微改变客户端来处理通信。

    示例

    client.php

    <?php
    global $argc,$argv;
    
    if (!file_exists($argv[1])) {
            echo "File not found\n";
            exit(1);
    }
    
    $gmclient= new GearmanClient();
    $gmclient->addServer();
    do
    {
      $result = $gmclient->do("linecount", file_get_contents($argv[1]));
      # Check for various return packets and errors.
    
      switch($gmclient->returnCode())
      {
        case GEARMAN_WORK_STATUS:
          list($numerator, $denominator)= $gmclient->doStatus();
          echo "Status: " . sprintf("%d%%",($numerator/$denominator)*100)
                 . " complete\r";
          break;
        case GEARMAN_SUCCESS:
          break;
      }
    }
    while($gmclient->returnCode() != GEARMAN_SUCCESS);
    
    echo "\nResult: $result\n";
    

    worker.php

    <?php
    $worker= new GearmanWorker();
    $worker->addServer();
    $worker->addFunction("linecount", "linecount");
    while ($worker->work());
    
        function linecount($job)
        {
                $lines = preg_split('/[\r\n]/',
                           $job->workload(),null,PREG_SPLIT_NO_EMPTY);
                $linecount = count($lines);
                $n = 0;
                foreach ($lines as $line) {
                        usleep(3000);
                        $n++;
                        $job->sendStatus($n,$linecount);
                        $ret++;
                }
                return $ret;
        }
    

    【讨论】:

    • 这段代码正常吗?我测试它没有任何成功
    • 它曾在 2011 年与 gearmand 0.20 一起测试并发现可以正常工作。现在 gearmand 1.2 发布了,很可能在这两个版本之间对 API 进行了更改。
    • 你设法在客户端获得状态?我的根本不工作..gearman api几乎没有改变..你的代码工作得很好..我只是想知道我的齿轮箱安装不正确
    • 啊,我的客户根本没有收到来自服务器的任何输入,returnCode 不工作..谢谢你的输入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2013-02-05
    相关资源
    最近更新 更多