distributed work with gearman · dominik jungowski 27 years old scrum coach at inovex gmbh...

67
Distributed work with Gearman Dominik Jungowski / inovex GmbH

Upload: others

Post on 29-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Distributed work with Gearman

Dominik Jungowski / inovex GmbH

Page 2: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Law of two feet

Page 3: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Dominik Jungowski

27 years old

Scrum Coach at inovex GmbH

Psychology student at Fernuni Hagen

Page 4: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Topics

What is Gearman?

Setting up Gearman

Basic Usage

Job status

Error handling

Managing workers

Page 5: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

What is Gearman?

Page 6: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen
Page 7: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Script Processing Script (cont.)

Page 8: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Script Processing Script (cont.)

Page 9: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Script

Worker

Worker

Worker

Page 10: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Setting up Gearman serverlatest version: 0.24

Page 11: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

aptitude install gearman-job-server

Page 12: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Setting up PECL Extension

Page 13: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 14: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

extension=gearman.so

Page 15: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

user@server:~# gearmand

Page 16: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Basic Usage

Page 17: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Worker

Page 18: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 19: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 20: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 21: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 22: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 23: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

namespace Cxo;

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

Page 24: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

namespace Cxo;

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

Page 25: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

namespace Cxo;

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

Page 26: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

namespace Cxo;

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

Page 27: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Synchronous Jobs

Page 28: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 29: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 30: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 31: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

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

Page 32: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$client->doHigh();

Page 33: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$client->do() returns worker result

Page 34: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Asynchronous Jobs

Page 35: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 36: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$client->doBackground() returns job handle

Page 37: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Tasks

Page 38: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 39: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 40: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$client->runTasks();

Page 41: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 42: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen
Page 43: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Script (cont.)ProcessingScript

Page 44: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Script (cont.)Processing

Processing

Processing

Script

Page 45: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Job status

Page 46: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$handle = $client->doBackground();

Page 47: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 48: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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 · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$job->sendStatus(4, 10);

Page 50: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Error handling

Page 51: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

GEARMAN_SUCCESS

Page 52: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

GEARMAN_WORK_FAIL

Page 53: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$worker->returnCode();

Page 54: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$client->returnCode();

Page 55: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$job->sendFail();

Page 56: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 57: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 58: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

$message = $client->do();

Page 59: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Managing workers

Page 60: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

supervisord

Page 61: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Memory consumption

Page 62: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Persistence

Page 63: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

“Whuh?“

Page 64: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

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

Page 65: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

GearmanClient::setOptions

Return Values: Always returns TRUE

Page 66: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

do is a keyword

Page 67: Distributed Work with Gearman · Dominik Jungowski 27 years old Scrum Coach at inovex GmbH Psychology student at Fernuni Hagen

Thank you!

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

Blog: www.agileblog.org