Command, Gearman

Install and use Gearman

Queuing systems are complex in nature and developing them is equally a difficult task. Every programming language has to have some sort of queue system to perform certain tasks. So, here we are going to implement a queue system in PHP by using Gearman PECL Extension.

Installing Gearman

Use the following command to install Gearman package on the centos server. You will need root access.

yum install gearmand.x86_64

After installation is finished, start gearman by using this commnd:

gearmand -d

Now, open whm and then go to Software ยป Module Installers

Select correct php version, and search german by hitting Go button.

Click Instal link to install the gearman pecl extension. Wait for gearman pecl extension installation to finish and then restart your server.

Gearman Example

gearman-client.php

<?php  
	$client = new GearmanClient();
	$client->addServer();
	$result = $client->doBackground("export", json_encode(array(
	  'name' => "value"
	)));
?>

gearman-worker.php

<?php  
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("export", function(GearmanJob $job) {
    $workload = json_decode($job->workload());
    echo $workload->name.'<br>';
});

while ($worker->work());
?>

First visit gearman-client.php file and then gearman-worker.php, it will print the value.

Leave a Reply

Your email address will not be published. Required fields are marked *