Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gearman Deep Dive - PHPTek 2012

Gearman Deep Dive - PHPTek 2012

With every passing year the data that companies and application store grows astronomically, both through more data logging for business analysis and the consumption of 3rd party data (mashups), but how can those companies make sense of all that data and turn it into something useful that is constantly updating and evolving? Gearman is a very good and simple solution to this data problem. Gearman allows you to farm out work to other machines better suited to do the job at hand in a language and platform agnostic manner. During the talk I will tell stories of 2 different products where I faced a huge data processing challenges and how I solved them with Gearman. I will do demos and live coding of Gearman workers, a deep dive into the system!

Welcome to the Age of Data, we have cookies.

Helgi Þorbjörnsson

May 25, 2012
Tweet

More Decks by Helgi Þorbjörnsson

Other Decks in Programming

Transcript

  1. Your Client Code Gearman Client API (C, PHP, Perl, MySQL

    UDF, ...) Gearman Job Server gearmand Gearman Worker API (C, PHP, Perl, Python, ...) Your Worker Code Your App Gearman
  2. Good for Processing (Images, text) Logs (Web Server, etc) Data

    Mining Mass Emails Intensive transformation
  3. Good for Processing (Images, text) Logs (Web Server, etc) Data

    Mining Mass Emails Intensive transformation Search
  4. “It all depends on how we look at things, and

    not how they are in themselves.” - Carl G. Jung
  5. Your Client Code Gearman Job Server gearmand Your Worker Code

    Your App Gearman Gearman Client API (C, PHP, Perl, MySQL UDF, ...) Gearman Worker API (C, PHP, Perl, Python, ...)
  6. Your Client Code Gearman Job Server gearmand Your Worker Code

    Your App Gearman Gearman Client API (C, PHP, Perl, MySQL UDF, ...) Gearman Worker API (C, PHP, Perl, Python, ...)
  7. Your Client Code Gearman Job Server gearmand Your Worker Code

    Your App Gearman Gearman Client API (C, PHP, Perl, MySQL UDF, ...) Gearman Worker API (C, PHP, Perl, Python, ...)
  8. <?php define('NET_GEARMAN_JOB_PATH', __DIR__ . '/../tasks/'); define('NET_GEARMAN_JOB_CLASS_PREFIX', ''); require_once 'Net/Gearman/Worker.php'; try

    { $worker = new Net_Gearman_Worker(array('localhost:4370', 'localhost:4371')); $worker->addAbility('Update'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }
  9. <?php define('NET_GEARMAN_JOB_PATH', __DIR__ . '/../tasks/'); define('NET_GEARMAN_JOB_CLASS_PREFIX', ''); require_once 'Net/Gearman/Worker.php'; try

    { $worker = new Net_Gearman_Worker(array('localhost:4370', 'localhost:4371')); $worker->addAbility('Update'); $worker->addAbility('SaveDB'); $worker->addAbility('SaveMemcache'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }
  10. ✓ Memcache ✓ Drizzle ✓ MySQL ✓ PostgresSQL ✓ SQLite

    3 ✓ TokyoCabinet ✓ Redis (in dev) ✓ MongoDB (in dev) Server Persistence
  11. gearmand -d -q libsqlite3 --libsqlite3-db=/tmp/demo/gearman.db --listen=localhost --port=4370 gearmand -d -q

    libsqlite3 --libsqlite3-db=/tmp/demo/gearman.db --listen=localhost --port=4371
  12. <?php define('NET_GEARMAN_JOB_PATH', __DIR__ . '/../tasks/'); define('NET_GEARMAN_JOB_CLASS_PREFIX', ''); require_once 'Net/Gearman/Worker.php'; try

    { $worker = new Net_Gearman_Worker(array('localhost:4370', 'localhost:4371')); $worker->addAbility('Update'); $worker->addAbility('SaveDB'); $worker->addAbility('SaveMemcache'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }
  13. <?php try { // Do all the gearman magic here.

    kick off a job. Be the client. $servers = array( 'localhost:4370', 'localhost:4371' ); require_once 'Net/Gearman/Client.php'; $client = new Net_Gearman_Client($servers); // Emulate that a single client signs in. $clients = range(1, 50); $c = $clients[rand(0, 49)]; $jobs = $client->Update($c); echo "Added client $c to the queue with the handler $jobs\n"; } catch (Net_Gearman_Exception $e) { echo 'Could not connect to any server'; }
  14. <?php require_once __DIR__ . '/../Base.php'; class Update extends Base {

    public function run($args) { $this->connect(); // Fetch Salary information for this client $this->client->WebserviceSalary($args); // Fetch all the Point of Sale information for this client $this->client->WebservicePos($args); return true; } }
  15. <?php class WebserviceSalary extends Net_Gearman_Job_Common { public function run($args) {

    $data = @file_get_contents('http://gearman.talks.conf/salary.json'); if (!$data) { return false; } $file = 'salary-' . rand(1,9999) . '-' . time(); file_put_contents('/tmp/demo/raw/' . $file, $data); return true; } }
  16. ✓ Makes sure processes are running ✓ Can start more

    than one of same process ✓ Restarts crashed processes
  17. ✓ Makes sure processes are running ✓ Can start more

    than one of same process ✓ Restarts crashed processes ✓ Simple to configure and manage
  18. ✓ Makes sure processes are running ✓ Can start more

    than one of same process ✓ Restarts crashed processes ✓ Simple to configure and manage ✓ Each worker type has a different config
  19. [program:gearman-worker-save-update] command=php /var/www/gearman/workers/worker1.php numprocs=10 process_name=%(program_name)s-%(process_num)s autorestart=true [program:gearman-worker-ws-1] command=php /var/www/gearman/workers/worker2.php numprocs=5

    process_name=%(program_name)s-%(process_num)s autorestart=true [program:gearman-worker-ws-2] command=php /var/www/gearman/workers/worker3.php numprocs=5 process_name=%(program_name)s-%(process_num)s autorestart=true
  20. ✓ Makes sure processes are running ✓ Can start more

    than one of same process ✓ Restarts crashed processes ✓ Simple to configure and manage ✓ Each worker type has a different config
  21. [GearmanManager] host=localhost:4370,localhost:4371 ; workers can be found in this dir

    worker_dir=/var/www/gearman/tasks ; Reload workers as new code is available auto_update=1 ; 20 workers will do all jobs count=20 ; Each job will have minimum 1 worker that does only that job dedicated_count=1 [Webservice_Pos] count=10 [Webservice_Salary] count=10