问题
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?
回答1:
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;
}
回答2:
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.
来源:https://stackoverflow.com/questions/5666561/problem-with-gearman-job-status