5

I have a Gearman server running a process which takes a few minutes to finish. I'm running a progress bar to show completion, and am attempting to get the percentages for the bar using the Gearman PHP extension and the jobStatus() function.

The job is definitely active and found, as the first two fields (known + still running) return to true. However the third and fourth fields (numerator and denominator of completion percentage) return with nothing. Does anyone know why this might be or how these numbers are computed?

Aaron Marks
  • 375
  • 2
  • 7
  • 19

2 Answers2

3
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;
    }
Peter Lindqvist
  • 10,122
  • 3
  • 41
  • 60
  • is this code functioning? i test it without any success – slier Nov 29 '14 at 16:33
  • It was once tested and found in working order together with gearmand 0.20 in 2011. Now that gearmand 1.2 is released, it is likely there are changes made to the API between those two releases. – Peter Lindqvist Dec 01 '14 at 10:09
  • u manage to get the status in the client? mine not working at all..gearman api barely change..ur code working just fine..i just wonder either i have improper gearman installation – slier Dec 01 '14 at 10:51
  • ah ic, my client didnt receive any input from the server at all, `returnCode` not working..thx for ur input – slier Dec 01 '14 at 11:55
0

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.

preinheimer
  • 3,712
  • 20
  • 34
  • Ah, thanks so much, didn't know the worker needed to be configured. Any resources you can point to as to how exactly to configure the worker? – Aaron Marks Apr 14 '11 at 17:17
  • Did you write the package yourself? When you receive a Job, you can return status: http://www.php.net/manual/en/gearmanjob.sendstatus.php – preinheimer Apr 14 '11 at 17:39