This example shows a very simple client and worker. The client sends a string
to the job server, and the worker reverses the string and sends it back.
The job is performed synchronously.
<?php
# Create our client object. $gmclient= new GearmanClient();
# Add default server (localhost). $gmclient->addServer();
echo "Sending job\n";
# Send reverse job do { $result = $gmclient->doNormal("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: echo "Success: $result\n"; break; default: echo "RET: " . $gmclient->returnCode() . "\n"; exit; } } while($gmclient->returnCode() != GEARMAN_SUCCESS);
?>
<?php
echo "Starting\n";
# 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");
# 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); }
An example of a client is invalid. Progress will not be displayed on the client side, as doNormal method is synchronous. Code will be executed only after the completion doNormal. <?php case GEARMAN_WORK_STATUS: list($numerator, $denominator)= $gmclient->doStatus(); echo "Status: $numerator/$denominator complete\n"; break; ?>