Gearman & PHP

4,915
-1

Published on

All you need to build large scale applications on distributed architecture using different programming language.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,915
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
41
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Gearman & PHP

  1. 1. Nemanja Krivokapić Software Engineer @ PSTech @NemanjaKr | LinkedIn PHP & Gearman All you need to build large scale web application
  2. 2. What is Gearman?  Generic Application Framework  Job Server  Client / Worker API  Multi-platform and multi-language API support  Support priorities
  3. 3. How does it work?
  4. 4. Multi-server configuration
  5. 5. Jobs vs. Tasks  Matter of perspective  Client delegates task to the server  Job server delegates jobs to the workers
  6. 6. Supported platforms and languages  Platforms: Unix/Windows  Languages supported:     PHP Java C/C++ NodeJS, Python, Go, Lisp...
  7. 7. Gearman server installation tips  Don’t use Ubuntu package for gearman server (outdated and doesn’t work with current PHP PECL extension)  Build it from source: https://launchpad.net/gearmand  Run: gearmand --deamon
  8. 8. Server configuration  All config params are supplied through command line  Important ones:      --port=XXX (default 4730) --deamon --job-retries (default NO LIMIT!) --queue-type (default builtin, not persistent) --listen (default INADDR_ANY)  Use --help
  9. 9. PHP Gearman implementation Yeah – this is cool but HOW CAN I USE IT?
  10. 10. Little bit of shell   Prerequisite: libgearman (if built from source – already included)  PEAR (apt-get install php-pear)  PECL (pear install pecl)*  pecl install gearman  Tell PHP to use gearman client/worker API extension ( extension=gearman.so in php.ini) * For proxy: pear config http_proxy http://
  11. 11. Provided classes  GearmanClient  GearmanTask  GearmanWorker  GearmanJob  GearmanException
  12. 12. GearmanClient  Used for communication with Job Server from your application  Foreground jobs (doHigh, doNormal, doLow)  Background jobs (doHighBackground, doBackground, doLowBackground)  Tasks (replace do* with addTask*)  addTask* returns GearmanTask object, do* returns string!
  13. 13. GearmanWorker  We use it to connect our worker script to Job Server and wait for job  addFunction(string $function_name , callable $function)  work()  Callable provided will receive an GearmanJob object as argument
  14. 14. Running tasks in parallel  You can stack as many tasks using addTask method of GearmanClient.  They will all be executed in parallel when you call runTasks (if there’s enough workers available)  Notice: Using do* methods will execute the task immediately
  15. 15. Client callbacks for workers events  When running foreground tasks you can register callbacks using methods of GearmanClient using methods:      setCompleteCallback, setCreatedCallback, setDataCallback, setExceptionCallback, setFailCallback  They all take single argument – callable object which will receive GearmanTask object as an argument.
  16. 16. SupervisorD – keep it running  PHP worker script will wait for job, do it and die.  Supervisord – Process Control System*  apt-get install supervisor  Create configuration file in /etc/supervisor/supervisord.conf  sudo service supervisor * Project homepage: http://supervisord.org/
  17. 17. SupervisorD alternative  GearmanManager  Easier to setup  PHP wrapper around GearmanWorker  https://github.com/brianlmoon/Gearman Manager
  18. 18. Relevant links     PHP Examples: http://www.php.net/manual/en/gearman.examples. php Gearman Job Server documentation: http://gearman.org/documentation/ Cool tutorials to get started with Gearman on Ubuntu: http://blog.andyburton.co.uk/index.php/201212/compiling-gearman-with-mysql-persistent-storageon-ubuntu-12-04-precise/ http://blog.andyburton.co.uk/index.php/201212/gearman-0-41-with-mysql-persistent-storage-andphp-pecl-0-8-3-with-gearmanmanager-on-ubuntu12-04/
  19. 19. Questions? The End 
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×