Distributed work with Gearman

67
Distributed work with Gearman Dominik Jungowski / CHIP Xonio Online GmbH

description

 

Transcript of Distributed work with Gearman

Page 1: Distributed work with Gearman

Distributed work with Gearman

Dominik Jungowski / CHIP Xonio Online GmbH

Page 2: Distributed work with Gearman

Law of two feet

Page 3: Distributed work with Gearman

Dominik Jungowski

27 years old

ScrumMaster at CHIP Online

Psychology student at Fernuni Hagen

Page 4: Distributed work with Gearman

Topics

What is Gearman?

Setting up Gearman

Basic Usage

Job status

Error handling

Managing workers

Page 5: Distributed work with Gearman

What is Gearman?

Page 6: Distributed work with Gearman
Page 7: Distributed work with Gearman

Script Processing Script (cont.)

Page 8: Distributed work with Gearman

Script Processing Script (cont.)

Page 9: Distributed work with Gearman

Script

Worker

Worker

Worker

Page 10: Distributed work with Gearman

Setting up Gearman serverlatest version: 0.24

Page 11: Distributed work with Gearman

aptitude install gearman-job-server

Page 12: Distributed work with Gearman

Setting up PECL Extension

Page 13: Distributed work with Gearman

pecl install channel://pecl.php.net/gearman-0.8.0.tgz

Page 14: Distributed work with Gearman

extension=gearman.so

Page 15: Distributed work with Gearman

user@server:~# gearmand

Page 16: Distributed work with Gearman

Basic Usage

Page 17: Distributed work with Gearman

Worker

Page 18: Distributed work with Gearman

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

while($worker->work()) {}

Page 19: Distributed work with Gearman

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

while($worker->work()) {}

Page 20: Distributed work with Gearman

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

while($worker->work()) {}

Page 21: Distributed work with Gearman

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

while($worker->work()) {}

Page 22: Distributed work with Gearman

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

while($worker->work()) {}

Page 23: Distributed work with Gearman

namespace Cxo;

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

Page 24: Distributed work with Gearman

namespace Cxo;

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

Page 25: Distributed work with Gearman

namespace Cxo;

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

Page 26: Distributed work with Gearman

namespace Cxo;

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

Page 27: Distributed work with Gearman

Synchronous Jobs

Page 28: Distributed work with Gearman

$client = new GearmanClient();$client->addServer();

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

Page 29: Distributed work with Gearman

$client = new GearmanClient();$client->addServer();

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

Page 30: Distributed work with Gearman

$client = new GearmanClient();$client->addServer();

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

Page 31: Distributed work with Gearman

$client = new GearmanClient();$client->addServer();

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

Page 32: Distributed work with Gearman

$client->doHigh();

Page 33: Distributed work with Gearman

$client->do() returns worker result

Page 34: Distributed work with Gearman

Asynchronous Jobs

Page 35: Distributed work with Gearman

$client->doBackground( 'imageResize', '/tmp/someimage.jpg');

Page 36: Distributed work with Gearman

$client->doBackground() returns job handle

Page 37: Distributed work with Gearman

Tasks

Page 38: Distributed work with Gearman

$client->addTask( 'imageResize', '/tmp/someimage.jpg');

Page 39: Distributed work with Gearman

$client->addTaskBackground( 'imageResize', '/tmp/someimage.jpg');

Page 40: Distributed work with Gearman

$client->runTasks();

Page 41: Distributed work with Gearman

Scale by adding more workers(as long as you‘re not running jobs synchronously)

Page 42: Distributed work with Gearman
Page 43: Distributed work with Gearman

Script (cont.)ProcessingScript

Page 44: Distributed work with Gearman

Script (cont.)Processing

Processing

Processing

Script

Page 45: Distributed work with Gearman

Job status

Page 46: Distributed work with Gearman

$handle = $client->doBackground();

Page 47: Distributed work with Gearman

$status = $client->jobStatus($handle);

Page 48: Distributed work with Gearman

array(4) { [0]=> bool(true) // Is the job known? [1]=> bool(true) // Is the job running? [2]=> int(4) // Numerator [3]=> int(10) // Denominator}

Page 49: Distributed work with Gearman

$job->sendStatus(4, 10);

Page 50: Distributed work with Gearman

Error handling

Page 51: Distributed work with Gearman

GEARMAN_SUCCESS

Page 52: Distributed work with Gearman

GEARMAN_WORK_FAIL

Page 53: Distributed work with Gearman

$worker->returnCode();

Page 54: Distributed work with Gearman

$client->returnCode();

Page 55: Distributed work with Gearman

$job->sendFail();

Page 56: Distributed work with Gearman

$job->sendWarning(‘Something went wrong‘);

Page 57: Distributed work with Gearman

$job->sendException(‘Something went wrong‘);

Page 58: Distributed work with Gearman

$message = $client->do();

Page 59: Distributed work with Gearman

Managing workers

Page 60: Distributed work with Gearman

supervisord

Page 61: Distributed work with Gearman

Memory consumption

Page 62: Distributed work with Gearman

Persistence

Page 63: Distributed work with Gearman

“Whuh?“

Page 64: Distributed work with Gearman

$client->doJobHandle();doesn‘t do what it should - and many more functions as well

Page 65: Distributed work with Gearman

GearmanClient::setOptions

Return Values: Always returns TRUE

Page 66: Distributed work with Gearman

do is a keyword

Page 67: Distributed work with Gearman

Thank you!

joind.in: http://joind.in/3907Twitter: @djungowski

Blog: www.phpdevblog.net