Problem With Gearman Job Status

荒凉一梦 提交于 2019-12-03 00:41:50
public bool GearmanJob::sendStatus ( int $numerator , int $denominator )

Sends status information to the job server and any listening clients. Use this to specify what percentage of the job has been completed.

To be able to use it, you will probably also have alter the client a bit to handle the communication.

Example

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;
    }

Is the worker configured to return status?

If you write them yourself you have to do a bit of extra work to get them to return details as they go though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!