&reftitle.examples;
Basic usage Basic Gearman client and worker 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. 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); ?> ]]> 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()); } ?> ]]> &example.outputs.similar;
Basic Gearman client and worker, background Basic Gearman client and worker, background This example shows a very simple client and worker. The client sends a string to the job server as a background job, and the worker reverses the string. Note that since the work is performed asynchronously, the client does not wait for the job to complete and exits (and hence the client never receives the results). addServer(); # run reverse client in the background $job_handle = $gmclient->doBackground("reverse", "this is a test"); if ($gmclient->returnCode() != GEARMAN_SUCCESS) { echo "bad return code\n"; exit; } echo "done!\n"; ?> ]]> 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()); } ?> ]]> &example.outputs.similar;
Basic Gearman client and worker, submitting tasks Basic Gearman client and worker, submitting tasks In this example, the basic reverse client extended to run two tasks in parallel. The reverse worker is unchanged except to add sending of data back during processing. addServer(); # register some callbacks $gmc->setCreatedCallback("reverse_created"); $gmc->setDataCallback("reverse_data"); $gmc->setStatusCallback("reverse_status"); $gmc->setCompleteCallback("reverse_complete"); $gmc->setFailCallback("reverse_fail"); # 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"; function reverse_created($task) { echo "CREATED: " . $task->jobHandle() . "\n"; } function reverse_status($task) { echo "STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() . "/" . $task->taskDenominator() . "\n"; } function reverse_complete($task) { echo "COMPLETE: " . $task->jobHandle() . ", " . $task->data() . "\n"; } function reverse_fail($task) { echo "FAILED: " . $task->jobHandle() . "\n"; } function reverse_data($task) { echo "DATA: " . $task->data() . "\n"; } ?> ]]> 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+1, $workload_size); $job->sendData(substr($workload, $x, 1)); 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()); } ?> ]]> &example.outputs.similar;