Distributed work with Gearman

Post on 15-Jan-2015

3.171 views 0 download

Tags:

description

 

Transcript of Distributed work with Gearman

Distributed work with Gearman

Dominik Jungowski / CHIP Xonio Online GmbH

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.)

Script

Worker

Worker

Worker

Setting up Gearman serverlatest 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 (cont.)ProcessingScript

Script (cont.)Processing

Processing

Processing

Script

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/3907Twitter: @djungowski

Blog: www.phpdevblog.net