Alpha Framework alpha--util
[ class tree: alpha--util ] [ index: alpha--util ] [ all elements ]

Source for file AlphaCronManager.inc

Documentation is available at AlphaCronManager.inc

  1. <?php
  2.  
  3. // load config
  4. if(!isset($config)) {
  5.     require_once 'AlphaConfig.inc';
  6.     $config AlphaConfig::getInstance();
  7. }
  8.  
  9. require_once $config->get('sysRoot').'alpha/util/AlphaErrorHandlers.inc';
  10.  
  11. /**
  12.  *
  13.  * The main class responsible for running custom cron tasks found under the [webapp]/tasks
  14.  * directory.  This class should be executed from Linux cron via the CLI.
  15.  * 
  16.  * @package alpha::util
  17.  * @since 1.0
  18.  * @author John Collins <dev@alphaframework.org>
  19.  * @version $Id: AlphaCronManager.inc 1341 2011-03-17 15:02:02Z johnc $
  20.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  21.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  22.  *  All rights reserved.
  23.  * 
  24.  *  <pre>
  25.  *  Redistribution and use in source and binary forms, with or
  26.  *  without modification, are permitted provided that the
  27.  *  following conditions are met:
  28.  * 
  29.  *  * Redistributions of source code must retain the above
  30.  *    copyright notice, this list of conditions and the
  31.  *    following disclaimer.
  32.  *  * Redistributions in binary form must reproduce the above
  33.  *    copyright notice, this list of conditions and the
  34.  *    following disclaimer in the documentation and/or other
  35.  *    materials provided with the distribution.
  36.  *  * Neither the name of the Alpha Framework nor the names
  37.  *    of its contributors may be used to endorse or promote
  38.  *    products derived from this software without specific
  39.  *    prior written permission.
  40.  *   
  41.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  42.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  43.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  44.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  45.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  46.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  47.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  48.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  51.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  52.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  53.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54.  *  </pre>
  55.  *  
  56.  */
  57.     /**
  58.      * Trace logger
  59.      * 
  60.      * @var Logger 
  61.      * @since 1.0
  62.      */
  63.     private static $logger null;
  64.     
  65.     /**
  66.      * Constructor
  67.      * 
  68.      * @since 1.0
  69.      */
  70.     public function __construct({
  71.         global $config;        
  72.         
  73.         self::$logger new Logger('AlphaCronManager');            
  74.         self::$logger->setLogFile($config->get('sysRoot').'logs/tasks.log');
  75.         
  76.         self::$logger->debug('>>__construct()');
  77.         
  78.         self::$logger->info('New AlphaCronManager invoked');
  79.  
  80.         $taskList self::getTaskClassNames();
  81.         
  82.         self::$logger->info('Found ['.count($taskList).'] tasks in the directory ['.$config->get('sysRoot').'tasks]');
  83.         
  84.         foreach($taskList as $taskClass{
  85.             self::$logger->info('Loading task ['.$taskClass.']');
  86.             self::loadClassDef($taskClass);
  87.             $task new $taskClass;
  88.             
  89.             $startTime microtime(true);            
  90.             $maxAllowedTime $startTime+$task->getMaxRunTime();
  91.             
  92.             self::$logger->info('Start time is ['.$startTime.'], maximum task run time is ['.$task->getMaxRunTime().']');
  93.             
  94.             // only continue to execute for the task max time
  95.             set_time_limit($task->getMaxRunTime());
  96.             $task->doTask();
  97.             
  98.             self::$logger->info('Done in ['.round(microtime(true)-$startTime5).'] seconds');
  99.         }
  100.         
  101.         self::$logger->info('Finished processing all cron tasks');
  102.         
  103.         self::$logger->debug('<<__construct');
  104.     }
  105.     
  106.     /**
  107.      * Loops over the /tasks directory and builds an array of all of the task
  108.      * class names in the system.
  109.      *
  110.      * @return array 
  111.      * @since 1.0
  112.      */
  113.     public static function getTaskClassNames({
  114.         global $config;
  115.         
  116.         if(self::$logger == null{
  117.             self::$logger new Logger('AlphaCronManager');
  118.             self::$logger->setLogFile($config->get('sysRoot').'logs/tasks.log');
  119.         }
  120.         self::$logger->debug('>>getTaskClassNames()');
  121.         
  122.         global $config;
  123.         
  124.         $classNameArray array();        
  125.         
  126.         $handle opendir($config->get('sysRoot').'tasks');
  127.            
  128.         // loop over the business object directory
  129.         while (false !== ($file readdir($handle))) {
  130.             if (preg_match("/_task.inc/"$file)) {
  131.                 $classname substr($file0-4);
  132.                 
  133.                 array_push($classNameArray$classname);
  134.             }
  135.         }
  136.  
  137.         self::$logger->debug('<<getTaskClassNames ['.var_export($classNameArraytrue).']');
  138.         return $classNameArray;
  139.     }
  140.     
  141.     /**
  142.      * Loads the definition for the task classname provided
  143.      * 
  144.      * @param string $classname 
  145.      * @since 1.0
  146.      */
  147.     public static function loadClassDef($classname{
  148.         global $config;
  149.         
  150.         if(self::$logger == null{
  151.             self::$logger new Logger('AlphaCronManager');
  152.             self::$logger->setLogFile($config->get('sysRoot').'logs/tasks.log');
  153.         }
  154.         self::$logger->debug('>>loadClassDef(classname=['.$classname.'])');
  155.         
  156.         if(file_exists($config->get('sysRoot').'tasks/'.$classname.'.inc'))
  157.             require_once $config->get('sysRoot').'tasks/'.$classname.'.inc';
  158.         else
  159.             throw new IllegalArguementException('The class ['.$classname.'] is not defined anywhere!');
  160.         
  161.         self::$logger->debug('<<loadClassDef');
  162.     }
  163. }
  164.  
  165. // invoke a cron manager object
  166. $processor new AlphaCronManager();
  167.  
  168. ?>

Documentation generated on Thu, 17 Mar 2011 16:43:14 +0000 by phpDocumentor 1.4.3