Basic usage

Example #1 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
“;

# 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
“;
break;
case GEARMAN_WORK_STATUS:
list($numerator, $denominator)= $gmclient->doStatus();
echo “Status: $numerator/$denominator complete
“;
break;
case GEARMAN_WORK_FAIL:
echo “Failed
“;
exit;
case GEARMAN_SUCCESS:
break;
default:
echo “RET: ” . $gmclient->returnCode() . ”
“;
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…
“;
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo “return_code: ” . $gmworker->returnCode() . ”
“;
break;
}
}

function reverse_fn($job)
{
echo “Received job: ” . $job->handle() . ”
“;

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

echo “Workload: $workload ($workload_size)
“;

# 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 "; $job->sendStatus($x, $workload_size);
sleep(1);
}

$result= strrev($workload);
echo “Result: $result
“;

# 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());
}

?>

The above example will output something similar to:

% php reverse_worker.php
Starting
Waiting for job…
Received job: H:foo.local:36
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

% php reverse_client.php
Starting
Sending job
Status: 1/6 complete
Status: 2/6 complete
Status: 3/6 complete
Status: 4/6 complete
Status: 5/6 complete
Status: 6/6 complete
Success: !olleH

——————————————-
Basic Gearman client and worker, background

Example #1 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
“;
exit;
}

echo “done!
“;

?>
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)
{
echo “Received job: ” . $job->handle() . ”
“;

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

echo “Workload: $workload ($workload_size)
“;

# 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 "; $job->sendStatus($x, $workload_size);
sleep(1);
}

$result= strrev($workload);
echo “Result: $result
“;

# 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());
}

?>

Basic Gearman client and worker, submitting tasks

Example #1 Basic Gearman client and worker, submitting tasks

In this exmple, 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() . ”
“;
exit;
}

echo “DONE
“;

function reverse_created($task)
{
echo “CREATED: ” . $task->jobHandle() . ”
“;
}

function reverse_status($task)
{
echo “STATUS: ” . $task->jobHandle() . ” – ” . $task->taskNumerator() .
“/” . $task->taskDenominator() . ”
“;
}

function reverse_complete($task)
{
echo “COMPLETE: ” . $task->jobHandle() . “, ” . $task->data() . ”
“;
}

function reverse_fail($task)
{
echo “FAILED: ” . $task->jobHandle() . ”
“;
}

function reverse_data($task)
{
echo “DATA: ” . $task->data() . ”
“;
}

?>
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)
{
echo “Received job: ” . $job->handle() . ”
“;

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

echo “Workload: $workload ($workload_size)
“;

# 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 "; $job->sendStatus($x+1, $workload_size);
$job->sendData(substr($workload, $x, 1));
sleep(1);
}

$result= strrev($workload);
echo “Result: $result
“;

# 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());
}

?>

The above example will output something similar to:

% php reverse_worker.php
Starting
Waiting for job…
Received job: H:foo.local:45
Workload: foo (3)
1/3 complete
2/3 complete
3/3 complete
Result: oof
Received job: H:foo.local:44
Workload: bar (3)
1/3 complete
2/3 complete
3/3 complete
Result: rab

% php reverse_client_task.php
CREATED: H:foo.local:44
CREATED: H:foo.local:45
STATUS: H:foo.local:45 – 1/3
DATA: f
STATUS: H:foo.local:45 – 2/3
DATA: o
STATUS: H:foo.local:45 – 3/3
DATA: o
COMPLETE: H:foo.local:45, oof
STATUS: H:foo.local:44 – 1/3
DATA: b
STATUS: H:foo.local:44 – 2/3
DATA: a
STATUS: H:foo.local:44 – 3/3
DATA: r
COMPLETE: H:foo.local:44, rab
DONE

- EOF -