Task Scheduling and Asynchronous Processing Evolved.  Zend Server Job Queue By   Sam Hennessy  – Zend Professional Service...
Offline Processing <ul><li>Why? </li></ul><ul><ul><li>Perceived performance </li></ul></ul><ul><ul><li>Managing load </li>...
Offline Processing Examples <ul><li>Sending e-mails </li></ul><ul><li>Processing orders </li></ul><ul><li>Indexing new con...
Blog Example Post comment Save in the DB Rebuild RSS feeds Send email notifications Display “Thank You” message
Blog Example Post comment Save in the DB Rebuild RSS feeds Send email notifications Display thank you message Queue Tasks ...
Architectural Overview PHP + Job Queue Extension TCP or Unix Socket Job Queue Damon Persistent Storage (SQLite) HTTP Reque...
Goals Of Zend Job Queues <ul><li>Ability to off-load PHP code execution </li></ul><ul><li>Jobs should never be “lost” </li...
The  ZendJobQueue  class <ul><li>The  ZendJobQueue  class contains almost all the PHP API for Job Queue </li></ul><ul><li>...
Creating Jobs <ul><li>All jobs are created using the  ZendJobQueue::createHttpJob()  method </li></ul><ul><li>Passing a pa...
Passing Parameters <ul><li>Simple parameters can be passed as part of the query string </li></ul><ul><li>Complex parameter...
Passing Parameters (example) $params  = array(     'cart'  => array(       'items'  => array(        array( 'id'  =>  324 ...
Accessing Parameters <ul><li>Inside the Job code, use the  ZendJobQueue::getCurrentJobParams()  static method to get the j...
Additional Job Options <ul><li>The 3 rd  parameter of  createHttpJob  is an associative array of parameters: </li></ul><ul...
Creating Deferred Jobs <ul><li>You can set a job's (estimated) execution time  by passing the  schedule_time  option: </li...
Creating Recurring Jobs <ul><li>You can create a recurring job from API using the  schedule  option </li></ul><ul><li>This...
Reporting Logical Failures <ul><li>A logical failure happens when something has programatically failed </li></ul><ul><ul><...
Job HTTP Request POST /job.php HTTP/1.0 Host: localhost Content-Type: text/json Content-Length: 34 Connection: close Accep...
Job HTTP Response HTTP/1.1 200 OK Date: Thu, 10 Sep 2009 17:29:20 GMT Server: Apache/2.2.9 (Debian) PHP/5.2.10 X-Powered-B...
Live Demo
Questions?
Thank You! <ul><li>Please come find me if you have any more questions </li></ul>
Upcoming SlideShare
Loading in...5
×

Task Scheduling and Asynchronous Processing Evolved. Zend Server Job Queue

9,949

Published on

Find out how Zend Server's Job Queues can give you the tools to effetely manage and debug the running of offline PHP tasks. Whether you need code that will execute at set intervals or you're offloading work from your web servers. Zend Server gives you a system that lets you develop the tasks easily as well as monitor and debug simply.

Published in: Technology
1 Comment
7 Likes
Statistics
Notes
  • please keep downloading enable..
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
9,949
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Transcript of "Task Scheduling and Asynchronous Processing Evolved. Zend Server Job Queue"

  1. 1. Task Scheduling and Asynchronous Processing Evolved. Zend Server Job Queue By Sam Hennessy – Zend Professional Services Consultant
  2. 2. Offline Processing <ul><li>Why? </li></ul><ul><ul><li>Perceived performance </li></ul></ul><ul><ul><li>Managing load </li></ul></ul>Any work that isn’t needed for giving feedback to the user should be done offline.
  3. 3. Offline Processing Examples <ul><li>Sending e-mails </li></ul><ul><li>Processing orders </li></ul><ul><li>Indexing new content </li></ul><ul><li>Cleaning the database or file system </li></ul><ul><ul><li>Either periodically or after some user action </li></ul></ul><ul><li>Pulling an RSS feed every hour </li></ul><ul><li>Checking exchange rates every day </li></ul><ul><li>Generating periodical reports </li></ul>
  4. 4. Blog Example Post comment Save in the DB Rebuild RSS feeds Send email notifications Display “Thank You” message
  5. 5. Blog Example Post comment Save in the DB Rebuild RSS feeds Send email notifications Display thank you message Queue Tasks Job Queue
  6. 6. Architectural Overview PHP + Job Queue Extension TCP or Unix Socket Job Queue Damon Persistent Storage (SQLite) HTTP Request Executing Server
  7. 7. Goals Of Zend Job Queues <ul><li>Ability to off-load PHP code execution </li></ul><ul><li>Jobs should never be “lost” </li></ul><ul><li>Ability to queue jobs from PHP API </li></ul><ul><li>Ability to create recurring & deferred jobs </li></ul><ul><li>Management GUI </li></ul>
  8. 8. The ZendJobQueue class <ul><li>The ZendJobQueue class contains almost all the PHP API for Job Queue </li></ul><ul><li>To perform most tasks, you will need to connect to a Job Queue server by instantiating a ZendJobQueue object: </li></ul>// Connect to the default JQ server $queue  = new  ZendJobQueue (); // Connect to any other JQ server $queue  = new  ZendJobQueue ( &quot;tcp://1.2.3.4:5678&quot; );
  9. 9. Creating Jobs <ul><li>All jobs are created using the ZendJobQueue::createHttpJob() method </li></ul><ul><li>Passing a partial URL will create the job with $_SERVER['HTTP_HOST'] in the host nam e </li></ul>$queue  = new  ZendJobQueue (); $queue -> createHttpJob ( 'http://backend.local/jobs/somejob.php' ); $queue -> createHttpJob ( '/jobs/otherjob.php' );
  10. 10. Passing Parameters <ul><li>Simple parameters can be passed as part of the query string </li></ul><ul><li>Complex parameters can also be passed </li></ul><ul><ul><li>Pass any serializable data as the 2 nd parameter of createHttpJob() </li></ul></ul><ul><ul><li>Data will be JSON-encoded – so anything which can be represented as JSON can be used </li></ul></ul>$queue -> createHttpJob ( '/jobs/otherjob.php?foo=bar&uid=1792' );
  11. 11. Passing Parameters (example) $params = array(    'cart'  => array(      'items'  => array(       array( 'id'  =>  324 ,  'qty'  =>  1 ,  'price'  =>  19.95 ),       array( 'id'  =>  75 ,  'qty'  =>  2 ,  'price'  =>  14.95 ,           'size'  =>  'XL' )     ),      'total'     =>  49.85 ,      'coupon'    =>  null ,      'giftwrap'  =>  true     ),    'user'  =>  $user ); $queue -> createHttpJob (    'http://backend/jobs/checkout.php' ,  $params );
  12. 12. Accessing Parameters <ul><li>Inside the Job code, use the ZendJobQueue::getCurrentJobParams() static method to get the job's parameters: </li></ul><ul><li>You can also json_decode() the raw POST data </li></ul>$params  =  ZendJobQueue :: getCurrentJobParams (); $params  =  json_decode ( file_get_contents ( 'php://input' ));
  13. 13. Additional Job Options <ul><li>The 3 rd parameter of createHttpJob is an associative array of parameters: </li></ul><ul><ul><li>name </li></ul></ul><ul><ul><li>priority </li></ul></ul><ul><ul><li>persistent </li></ul></ul><ul><ul><li>predecessor </li></ul></ul><ul><ul><li>http_headers </li></ul></ul><ul><ul><li>schedule </li></ul></ul><ul><ul><li>schedule_time </li></ul></ul>
  14. 14. Creating Deferred Jobs <ul><li>You can set a job's (estimated) execution time by passing the schedule_time option: </li></ul><ul><li>The job will not run before the specified time </li></ul>// Process the form at 3:00 am $runAt  =  date ( 'Y-m-d h:i:s' ,  strtotime ( '+1 day 3:00am' )); $options  = array(    'schedule_time'  =>  $runAt ); $queue -> createHttpJob ( 'http://backend/jobs/process.php' ,     $_POST ,  $options );
  15. 15. Creating Recurring Jobs <ul><li>You can create a recurring job from API using the schedule option </li></ul><ul><li>This option takes a cron-like expression that specifies scheduling </li></ul>// Run on Sunday, Monday, and Tuesday at midnight    $jq -> createHttpJob ( 'http://localhost/jobs/feed/405' ,  null , array(         'schedule'  =>  '0 0 * * 0,1,2'    ));      // Run every other day of the month at 2:30pm    $jq -> createHttpJob ( 'http://localhost/jobs/feed/405' ,  null , array(         'schedule'  =>  '30 14 */2 * *'    ));
  16. 16. Reporting Logical Failures <ul><li>A logical failure happens when something has programatically failed </li></ul><ul><ul><li>e.g. you were unable to send an e-mail or connect to a SOAP web service </li></ul></ul><ul><ul><li>Unlike an execution failure which is a technical failure (e.g. 500 error from server) </li></ul></ul><ul><li>Logical failures need to be reported by the programmer: </li></ul>ZendJobQueue :: setCurrentJobStatus ( ZendJobQueue :: OK );  if (!  doTheImportantStuff ()) {    ZendJobQueue :: setCurrentJobStatus ( ZendJobQueue :: ERROR ); }
  17. 17. Job HTTP Request POST /job.php HTTP/1.0 Host: localhost Content-Type: text/json Content-Length: 34 Connection: close Accept: */* USER_AGENT: Zend Server Job Queue/4.1 Cookie: SESS273df6b85bb9a37ced781f806219fc6a=7k6atppt8akccooobs26htsim7; ZDEDebuggerPresent=php,phtml,php3 {&quot;var1&quot;:15,&quot;nested&quot;:[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]}
  18. 18. Job HTTP Response HTTP/1.1 200 OK Date: Thu, 10 Sep 2009 17:29:20 GMT Server: Apache/2.2.9 (Debian) PHP/5.2.10 X-Powered-By: PHP/5.2.10 ZendServer/4.0 Set-Cookie: ZDEDebuggerPresent=php,phtml,php3; path=/ X-Job-Queue-Status: 1 Something is wrong! Vary: Accept-Encoding Content-Length: 152 Connection: close Content-Type: text/html array(2) { [&quot;var1&quot;]=> int(15) [&quot;nested&quot;]=> array(3) { [0]=> string(1) &quot;a&quot; [1]=> string(1) &quot;b&quot; [2]=> string(1) &quot;c&quot; } }
  19. 19. Live Demo
  20. 20. Questions?
  21. 21. Thank You! <ul><li>Please come find me if you have any more questions </li></ul>

×