murshed ahmmad khan (usamurai) on May 1st, 2013
  • Sharebar

Gearman flow chart

After running a simple PHP Gearman client script, it throws the error below:

PHP Warning: GearmanClient::do(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed to send server-options packet -> libgearman/connection.cc:429 in /home/work/public_html/site/gearman_client.php on line 14

Solution:

The default host and port of Gearman is “localhost” and 4730 respectively. Normally you don’t need to mention it explicitly according to the documentation. But apart from the documentation, I had to explicitly mention it in both client and worker script to make it work.

$worker->addServer(“localhost”,4730);

Ref: You might also want to look into this post.

Sample Worker script: (/usr/bin/php ./gearman_worker.php)

addServer(“localhost”,4730);
// Register the processing function
$worker->addFunction(“process_string”, “processString”);
// Trigger the infinite loop
while ($worker->work());

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

// The main function that processes the job
function processString($job)
{
// get the workload – it’s a string. use serialization to pass objects
$param = $job->workload();
return “You passed the string: {$param} \n It had: “.strlen($param).” chars \n”;
}
?>

Sample Client script: (/usr/bin/php ./gearman_client.php)

addServer(“localhost”,4730);
// print the output of the job. first parameter is the job name, second one is the parameter
print $client->do(“process_string”, “Hello Gearman from Client!”);

/*
# set some arbitrary application data
$data[‘foo’] = ‘bar’;

# add two tasks
$task= $gmc->addTask(“reverse”, “foo”, $data);
$task2= $gmc->addTaskLow(“reverse”, “bar”, NULL);

# run the tasks in parallel (assuming multiple workers)
if (! $gmc->runTasks())
{
echo “ERROR ” . $gmc->error() . “\n”;
exit;
}

echo “DONE\n”;

*/
?>

I’ll add a more complete example script later.

Hope, it’ll help someone!

Leave a Reply