Advertisement
Guest User

Zend Framework + Beanstalkd/Pheanstalk Worker

a guest
Mar 18th, 2011
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. <?php
  2.  
  3. // -------------------------------------------------------------------------------
  4. // Bootstrap a Zend Framework application
  5. // -------------------------------------------------------------------------------
  6.  
  7. /**
  8.   * Define application constants
  9.   */
  10. define('DS', DIRECTORY_SEPARATOR);
  11. define('PS', PATH_SEPARATOR);
  12.  
  13. defined('BASE_PATH')
  14.     or define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
  15.    
  16. defined('APP_PATH')
  17.     or define('APP_PATH', realpath(dirname(__FILE__) . '/../application'));
  18.  
  19. defined('APP_ENV')
  20.     or define('APP_ENV', 'development');
  21.  
  22. $paths[] = BASE_PATH . DS . 'library';
  23. $paths[] = APP_PATH . DS . 'models';
  24. set_include_path(implode(PS, $paths) . PS . get_include_path());
  25.  
  26.    
  27. // register Pheanstalk class loader
  28. require_once('Pheanstalk/pheanstalk_init.php');
  29. require_once('Thumbnail.php');
  30.  
  31.  
  32. require_once 'Ash/Application.php';
  33. $application = new Ash_Application(APP_ENV, APP_PATH . DS . 'configs' . DS . 'application.ini');
  34. $application->bootstrap('config')
  35.             ->bootstrap('log');
  36.  
  37. /**
  38.   * Setup auth - perform the tasks as if we were an Member
  39.   */
  40. $auth = Zend_Auth::getInstance();
  41. $auth->getStorage()->write(array('role' => 'Member'));
  42.  
  43.  
  44. // -------------------------------------------------------------------------------
  45. // Now get to work!
  46. // -------------------------------------------------------------------------------
  47. $host = isset(Ash_Ash::getConfig()->beanstalk->host)
  48.             ? Ash_Ash::getConfig()->beanstalk->host
  49.             : '127.0.0.1';
  50. $tube = isset(Ash_Ash::getConfig()->beanstalk->thumbnailTask->tube)
  51.             ? Ash_Ash::getConfig()->beanstalk->thumbnailTask->tube
  52.             : 'thumbnails';
  53.  
  54. // create a new Pheanstalk client that should be listening to the same Beanstalkd host
  55. $beanstalk = new Pheanstalk($host);
  56.  
  57. /**
  58.   * only watch the tube dedicated towards thumb nailing and ignore the 'default' tube
  59.   */
  60. if ($tube == 'default') {
  61.     $beanstalk->watch($tube);
  62. } else {
  63.     $beanstalk->watch($tube)->ignore('default');
  64. }
  65.  
  66. /**
  67.   * Define the size of the thumbnails to be produced (assuming square images)
  68.   */
  69. $thumbnailSizes = array(40, 60, 30, 25, 180);
  70.  
  71.  
  72. /**
  73.   * start processing jobs!
  74.   */
  75. while ($job = $beanstalk->reserve()) {
  76.     Ash_Ash::log("New WorkerThumbnail Job started for {$job->getData()}");
  77.    
  78.     $filename = $job->getData();
  79.    
  80.     if (!empty($filename)) {
  81.         // if the file exists then do the thumbnailing
  82.         if (file_exists(Ash_Ash::getConfig()->appSettings->uploadPath . $filename)) {
  83.             // Start Vars
  84.             $cacheDir = Ash_Ash::getConfig()->appSettings->cachePath . $filename . '/';
  85.             $cacheUrl = '/media/cache/' . $filename . '/';
  86.            
  87.             switch (true) {
  88.                 // if the cached dir exists but is not writable
  89.                 case (file_exists($cacheDir) && !is_writable($cacheDir)):
  90.                     chmod($cacheDir, 0777);
  91.                     break;
  92.                 // Check for image's cache folder, make one if none exists
  93.                 case (!file_exists($cacheDir)):
  94.                     mkdir($cacheDir, 0777, true);
  95.                     break;
  96.             }
  97.            
  98.             // create thumbnails for each of the sizes
  99.             foreach ($thumbnailSizes as $size) {
  100.                 $thumbFilename = $size . 'x' . $size . '_' . $filename;
  101.  
  102.                 // Check if this size exists, if not, resize and crop it
  103.                 if (!file_exists($cacheDir . $thumbFilename)) {
  104.                        $thumb = new Thumbnail(Ash_Ash::getConfig()->appSettings->uploadPath . $filename);
  105.                        $thumb->resize(null, null, $size);
  106.                        $thumb->cropFromCenter($size);
  107.                        $thumb->save($cacheDir . $thumbFilename, 100);
  108.                 }
  109.             }
  110.  
  111.             /**
  112.              * Done with the job so delete it!
  113.              */
  114.             Ash_Ash::log("Deleting WorkerThumbnail Job for {$job->getData()}");
  115.             $beanstalk->delete($job);
  116.         } else {
  117.             // bury the job and make a log entry
  118.             $beanstalk->bury($job);
  119.             Ash_Ash::log("WorkerThumbnail was forced to BURY the job for {$job->getData()} "
  120.                 . "because the file didn't exist. Job details: \n" . print_r($job,true));
  121.         }
  122.     } else {
  123.         // bury the job and make a log entry
  124.         $beanstalk->bury($job);
  125.         Ash_Ash::log("WorkerThumbnail was forced to BURY the job for {$job->getData()} "
  126.             . "because the filepath was EMPTY! Job details: \n" . print_r($job,true));
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement