I am trying to get gearman to return a value to php from a function after php makes a request to it using $gmclient->do("somefunction", "somedata"). However, the php client simply times out. The exact code I am using is straight from the php manual. I am using example #1 from http://docs.php.net/manual/en/gearmanclient.do.php
The browser gives me this message:
This webpage is not available.
The webpage at http://yoursite.com/client.php might be temporarily down or it may have moved permanently to a new web address.
More information on this error. Below is the original error message
Error 324 (net::ERR_EMPTY_RESPONSE): Unknown error.
The browser is Chrome if that helps to elaborate the error message.
In case it makes a difference, the worker.php file is being executed in a terminal window using the command "php worker.php". I am running on Ubuntu 9.10 Karmic Koala. I installed gearman using the directions found at http://blog.stuartherbert.com/php/2010/02/26/getting-gearman-up-and-running-on-ubuntu-karmic/
I checked the terminal window and gearman is getting the request and echos the results into the terminal - it just not sent back to the client.
The end goal is to get gearman to return to the client the return value from the function that was executed and display that value to the user.
UPDATE:
As requested, the code is below:
worker.php (the worker)
<?php
echo "Starting
";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
$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...
";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "
";
break;
}
}
function reverse_fn($job)
{
return strrev($job->workload());
}
?>
client.php (client code - this is the page I am loading the browser)
<?php
# Client code
echo "Starting
";
# Create our client object.
$gmclient= new GearmanClient();
# Add default server (localhost).
$gmclient->addServer();
echo "Sending job
";
$result = $gmclient->do("reverse", "Hello!");
echo "Success: $result
";
?>
The comments below where it said it was working.. I repeat, it was NOT working. It only appeared to work because I changed $gmclient->do to $gmclient->doBackground which output the job ID, not the actual result from the function.
FINAL UPDATE (WITH SOLUTION)
After some work, I've figured out that it was not a coding error. Gearman was improperly installed. Instead of using apt-get install, I decided to do things manually. I downloaded the gearmand (c) from the gearman site (http://gearman.org/index.php?id=download). I then used the tutorials on the gearman site as well starting with http://gearman.org/index.php?id=getting_started and then http://gearman.org/index.php?id=gearman_php_extension
Change your code and use GearmanClient::addTask. You can use its return value to implement monitoring process. Check on the other functions that you can use on the GearmanTask class.
//Wrong
$gmworker->addServer();
//Correct
$gmworker->addServer("localhost",4730);