Dominik Jungowski / CHIP Xonio Online GmbH


Distributed work with
Gearman
Law of two feet
Dominik Jungowski

27 years old

ScrumMaster at CHIP Online

Psychology student at Fernuni
  Hagen
Topics

What is Gearman?

Setting up Gearman

Basic Usage

Job status

Error handling

Managing workers
What is Gearman?
Script   Processing   Script (cont.)
Script   Processing   Script (cont.)
Worker




Script   Worker




         Worker
Setting up Gearman server
        latest version: 0.24
aptitude install gearman-job-server
Setting up PECL Extension
pecl install channel://pecl.php.net/gearman-0.8.0.tgz
extension=gearman.so
user@server:~# gearmand
Basic Usage
Worker
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction(
     ‘imageResize‘,
     array($image, ‘resize‘)
);

while($worker->work()) {
}
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction(
     ‘imageResize‘,
     array($image, ‘resize‘)
);

while($worker->work()) {
}
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction(
     ‘imageResize‘,
     array($image, ‘resize‘)
);

while($worker->work()) {
}
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction(
     ‘imageResize‘,
     array($image, ‘resize‘)
);

while($worker->work()) {
}
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction(
     ‘imageResize‘,
     array($image, ‘resize‘)
);

while($worker->work()) {
}
namespace Cxo;

class Image
{
     public function resize(GearmanJob $job)
     {
          $tmpFile = $job->workload();
          // Resizing takes place here
          ...
          return $finalFileName;
     }
}
namespace Cxo;

class Image
{
     public function resize(GearmanJob $job)
     {
          $tmpFile = $job->workload();
          // Resizing takes place here
          ...
          return $finalFileName;
     }
}
namespace Cxo;

class Image
{
     public function resize(GearmanJob $job)
     {
          $tmpFile = $job->workload();
          // Resizing takes place here
          ...
          return $finalFileName;
     }
}
namespace Cxo;

class Image
{
     public function resize(GearmanJob $job)
     {
          $tmpFile = $job->workload();
          // Resizing takes place here
          ...
          return $finalFileName;
     }
}
Synchronous Jobs
$client = new GearmanClient();
$client->addServer();

$client->do('imageResize', '/tmp/someimage.jpg');
$client = new GearmanClient();
$client->addServer();

$client->do('imageResize', '/tmp/someimage.jpg');
$client = new GearmanClient();
$client->addServer();

$client->do('imageResize', '/tmp/someimage.jpg');
$client = new GearmanClient();
$client->addServer();

$client->do('imageResize', '/tmp/someimage.jpg');
$client->doHigh();
$client->do() returns worker result
Asynchronous Jobs
$client->doBackground(
      'imageResize',
      '/tmp/someimage.jpg'
);
$client->doBackground() returns job handle
Tasks
$client->addTask(
      'imageResize',
      '/tmp/someimage.jpg'
);
$client->addTaskBackground(
      'imageResize',
      '/tmp/someimage.jpg'
);
$client->runTasks();
Scale by adding more workers
   (as long as you‘re not running jobs synchronously)
Script   Processing   Script (cont.)
Processing




Script   Processing   Script (cont.)




         Processing
Job status
$handle = $client->doBackground();
$status = $client->jobStatus($handle);
array(4) {
  [0]=> bool(true)   // Is the job known?
  [1]=> bool(true)   // Is the job running?
  [2]=> int(4)       // Numerator
  [3]=> int(10)      // Denominator
}
$job->sendStatus(4, 10);
Error handling
GEARMAN_SUCCESS
GEARMAN_WORK_FAIL
$worker->returnCode();
$client->returnCode();
$job->sendFail();
$job->sendWarning(‘Something went wrong‘);
$job->sendException(‘Something went wrong‘);
$message = $client->do();
Managing workers
supervisord
Memory consumption
Persistence
“Whuh?“
$client->doJobHandle();
doesn‘t do what it should - and many more functions as well
GearmanClient::setOptions

Return Values: Always returns TRUE
do is a keyword
Thank you!




joind.in: http://joind.in/3907
    Twitter: @djungowski
 Blog: www.phpdevblog.net

Distributed work with Gearman