Gearman PHP - SendStatus dont work

345 views
Skip to first unread message

Raga

unread,
Sep 11, 2012, 7:43:42 AM9/11/12
to gea...@googlegroups.com
Hello,
I've got Ubuntu 12.10 and instaled gearman 0.33 from repositories (apt-get install gearman) + PHP extension (pecl install gearman).

I want worker to send status back to client. There is example of code from php.net.pl.
The worker use sendStatus() function and the client should display the status, but it seems not.

It prints out:

client.php (there should be also info about status)

Sending job

Result: !olleH



worker.php
Received job: H:COMP-NAME:2
Workload: Hello! (6)
Sending status: 1/6 complete
Sending status: 2/6 complete
Sending status: 3/6 complete
Sending status: 4/6 complete
Sending status: 5/6 complete
Sending status: 6/6 complete
Result: !olleH


Do you have the same problem? Is it Gearman bug or PHP extension bug?


See the code below from:
http://php.net/manual/en/gearmanclient.donormal.php


worker.php

<?php

echo
"Starting\n";

# Create our worker object.
$gmworker
= new GearmanWorker();

# Add job server (gearman-job-server).
$gmworker
->addServer();

# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker
->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
 
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
 
{
    echo
"return_code: " . $gmworker->returnCode() . "\n";
   
break;
 
}
}

function reverse_fn($job)
{
  echo
"Received job: " . $job->handle() . "\n";

  $workload
= $job->workload();
  $workload_size
= $job->workloadSize();

  echo
"Workload: $workload ($workload_size)\n";

 
# This status loop is not needed, just showing how it works
 
for ($x= 0; $x < $workload_size; $x++)
 
{
    echo
"Sending status: " . ($x + 1) . "/$workload_size complete\n";
    $job
->sendStatus($x, $workload_size);
    sleep
(1);
 
}

  $result
= strrev($workload);
  echo
"Result: $result\n";

 
# Return what we want to send back to the client.
 
return $result;
}

# A much simpler and less verbose version of the above function would be:
function reverse_fn_fast($job)
{
 
return strrev($job->workload());
}

?>

client.php


<?php

# Create our client object.
$gmclient
= new GearmanClient();

# Add job server (gearman-job-server).
$gmclient
->addServer();

echo
"Sending job\n";

# Send reverse job
do
{
  $result
= $gmclient->do("reverse", "Hello!");

 
# Check for various return packets and errors.
 
switch($gmclient->returnCode())
 
{
   
case GEARMAN_WORK_DATA:
      echo
"Data: $result\n";
     
break;
   
case GEARMAN_WORK_STATUS:
      list
($numerator, $denominator)= $gmclient->doStatus();
      echo
"Status: $numerator/$denominator complete\n";
     
break;
   
case GEARMAN_WORK_FAIL:
      echo
"Failed\n";
     
exit;
   
case GEARMAN_SUCCESS:
     
break;
   
default:
      echo
"RET: " . $gmclient->returnCode() . "\n";
     
exit;
 
}
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);

echo "Success: $result\n";
?>

client.php
server.php

Raga

unread,
Sep 12, 2012, 3:07:14 AM9/12/12
to gea...@googlegroups.com
... or maybe any of you can post here example code with sending status back functionality from worker to client (so client can see it).
Not for background task, just for normal one.
I would really appreciate that, its quite important for me.

Clint Byrum

unread,
Sep 12, 2012, 1:47:22 PM9/12/12
to gearman
Excerpts from Raga's message of 2012-09-11 04:43:42 -0700:
> Hello,
> I've got Ubuntu 12.10 and instaled gearman 0.33 from repositories (apt-get
> install gearman) + PHP extension (pecl install gearman).
>

Side note: Ubuntu 12.10 is in beta. Were I building an app that
I expected to live for more than 18 months, I would choose 12.04.1,
which has 4 years and 8 months of support left. You can always backport
newer gearmand's to it. There's even a script to do this automatically
in ubuntu-dev-tools. Just type

backportpackage -d precise gearman-job-server

> I want worker to send status back to client. There is example of code from
> php.net.pl.
> The worker use sendStatus() function and the client should display the
> status, but it seems not.
>
> It prints out:
>
> client.php (there should be also info about status)
>
> Sending job
>
> Result: !olleH
>
>
> worker.php
> Received job: H:COMP-NAME:2
> Workload: Hello! (6)
> Sending status: 1/6 complete
> Sending status: 2/6 complete
> Sending status: 3/6 complete
> Sending status: 4/6 complete
> Sending status: 5/6 complete
> Sending status: 6/6 complete
> Result: !olleH
>
>
> Do you have the same problem? Is it Gearman bug or PHP extension bug?
>
>
> See the code below from:
> http://php.net/manual/en/gearmanclient.donormal.php
>
> *worker.php*
> *
> client.php*
Must be something wrong with the PECL code, because this is basically
exactly a PHP port of examples/reverse_client.cc, which I just checked,
works fine.

Evgeniy

unread,
Sep 13, 2012, 9:43:12 AM9/13/12
to gea...@googlegroups.com

Raga

unread,
Sep 13, 2012, 1:59:42 PM9/13/12
to gea...@googlegroups.com
Yes you had the same problem.
Currently I don't care about that right now.
I've managed to get my status and data to work with functions:
client.php
$gmclient= new GearmanClient();
$gmclient
->addServer($config['gearman_host'], $config['gearman_port']);
$gmclient
->setCreatedCallback("reverse_created");
$gmclient
->setDataCallback("reverse_data");
$gmclient
->setStatusCallback("reverse_status")
// ...

$job_handle
= $gmclient->addTask("process_image",serialize($params), null,$uid);

// ...

   
if (!$gmclient->runTasks()){
       $error
= sprint('Error occured');        
   
}

function reverse_created($task)
{    
    echo
"CREATED: " . $task->jobHandle() . "<br>";
   
}

function reverse_status($task)
{
    echo
"STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() .
         
"/" . $task->taskDenominator() . "<br>";
}


// ...

worker.php

$job->sendStatus($foo, $bar);
$job->sendData($data);

Sending status and data this way, works fine with PHP
Reply all
Reply to author
Forward
0 new messages