Vishal Suri
Vishal Suri

Reputation: 455

How to use Net_Gearman in a PHP application?

I have a PHP application in which I want to use Gearman for time consuming tasks. I searched a lot and found Net_Gearman as PHP API containing client and worker classes.

What should I do next in order to use Net_Gearman? I have no knowledge of Linux and Perl.

Upvotes: 0

Views: 1482

Answers (1)

Malcolm Jones
Malcolm Jones

Reputation: 1482

Couple things to get you on your way (I used some notes from https://github.com/lenn0x/net_gearman and my own knowledge of how Gearman works)

  • First of all you need to make sure you have gearmand installed/running correctly on your linux machine.

    Ubuntu - http://www.lornajane.net/posts/2011/installing-gearman-for-php-and-ubuntu

    Centos/Redhat - A simple sudo yum install gearmand libgearman should do the trick. There's a number of guides out there about installing from source; you probably do not want to do that.

Client:

Create a client script which will connect to your gearmand process and submit jobs to it:

require_once 'Net/Gearman/Client.php';

$client = new Net_Gearman_Client('localhost:7003');
$client->someBackgroundJob(array(
    'userid' => 5555,
    'action' => 'new-comment'
));

Library:

Create a library / another script to handle the actual job (in this case, someBackgroundJob):

<?php

class Net_Gearman_Job_someBackgroundJob extends Net_Gearman_Job_Common
{
    public function run($args)
    {
        if (!isset($args['userid']) || !isset($args['action'])) {
            throw new Net_Gearman_Job_Exception('Invalid/Missing arguments');
        }

        // Insert a record or something based on the $args

        return array(); // Results are returned to Gearman, except for 
                        // background jobs like this one.
    }
}

?>

Worker:

Finally, you need a worker to process this job. You could make it a cron that runs every minute if you want until there aren't any jobs to process and at that moment it should just hang there till it gets another job:

<?php

require_once 'Net/Gearman/Worker.php';

$worker = new Net_Gearman_Worker('localhost:7003');
$worker->addAbility('someBackgroundJob');
$worker->beginWork();

?>

Troubleshooting

Make sure gearmand is running on your server. You can run the following command on your Linux terminal:

[root@dev7 ~]# ps aux | grep gearman nobody

1826  0.0  0.1 406792  2236 ?        Ssl  Aug18  10:00 gearmand -d -u nobody -L 0.0.0.0 -p 4730 -P /var/run/gearmand/gearmand.pid -l /var/log/gearman/log root 4320  0.0  0.0 103240   944 pts/2    R+   16:16   0:00 grep --color=auto gearman
  • If Gearman is running, then you should see the process running like above. If it's not, you need to start it up... Ubuntu: service gearman-job-server start Centos/Redhat: service gearmand start
  • See what jobs are in the queue! Run the following:

    (echo status ; sleep 1) | nc 127.0.0.1 4730
    

This assumes your gearmand server is running on the same machine and that you have netcat installed.

Upvotes: 4

Related Questions