Phundament-Components
[ class tree: Phundament-Components ] [ index: Phundament-Components ] [ all elements ]

Source for file LPeriodicTask.php

Documentation is available at LPeriodicTask.php

  1. <?php
  2. /**
  3.  * Class file.
  4.  *
  5.  * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  6.  * @copyright 2005, diemeisterei GmbH. All rights reserved.
  7.  * @author $Author: schmunk $
  8.  * @version $Revision: 233 $  $Date: 2006-05-30 17:14:13 +0200 (Di, 30 Mai 2006) $
  9.  * @package Phundament.Components
  10.  * @subpackage Modules
  11.  */
  12. Prado::using("System.Util.TLogger");
  13.  
  14. /**
  15.  * LPeriodicTask class
  16.  *
  17.  * <code>
  18.  * <module id="garbage_collector" class="LGarbageCollector">
  19.  *   <scan dir="/public/tmp" probability="0.01">
  20.  *        <condition fileatime="-1440"/>
  21.  *     <action delete="1"/>
  22.  *   </scan>
  23.  * </module>
  24.  * </code>
  25.  *
  26.  * This module is used execute tasks periodically
  27.  *
  28.  * @package System.Util
  29.  * @subpackage Modules
  30.  */
  31. class LPeriodicTask extends TModule
  32. {
  33.     /**
  34.      * timestamp now
  35.      */
  36.     private $now 0;
  37.  
  38.     /**
  39.      * global state updates
  40.      */
  41.     private $configNode;
  42.  
  43.     /**
  44.      * Initializes the module.
  45.      * This method is required by IModule and is invoked by application.
  46.      * This just hooks into Application::onLoadStateComplete, because we have no global state
  47.      * earlier
  48.      * @param TXmlElement module configuration
  49.      */
  50.     public function init($xmlNode)
  51.     {
  52.         $this->configNode $xmlNode;
  53.         $this->Application->onLoadStateComplete[array($this"work");
  54.     }
  55.  
  56.     /**
  57.      * Does the modules work.
  58.      * It loads scanning information from the module configuration, and executes all wanted tasks
  59.      * @param object $sender TApplication
  60.      * @param unused $param 
  61.      */
  62.     public function work($sender$param)
  63.     {
  64.         $this->now time();
  65.  
  66.         foreach($this->configNode->getElementsByTagName('scan'as $node)
  67.         {
  68.             $d $node->getAttribute('dir');
  69.             if ($dd rtrim($d"/")) $d $dd;    // trim trailing slashes if it's not "/"
  70.             if (!$d || !is_dir($d))
  71.             {
  72.                 Prado :: log("scan node has invalid dir attribute ('$d')!"TLogger::ERROR"Lithron.LPeriodicTask");
  73.                 continue;
  74.             }
  75.             $p strtolower($node->getAttribute('probability'));
  76.             if ($p === "")
  77.             {
  78.                 Prado :: log("scan node has no probability (using default 1.0)!"TLogger::WARNING"Lithron.LPeriodicTask");
  79.                 $p 1.0;
  80.             }
  81.             $i strtolower($node->getAttribute('interval'));
  82.             if ($i)
  83.             {
  84.                 $iid $node->getAttribute('id');
  85.                 if (!$iid)
  86.                     Prado :: log("need id for using an interval! interval ignored"TLogger::ERROR"Lithron.LPeriodicTask");
  87.                 else if (($res $this->strToStamp($i)) == -1)
  88.                     Prado :: log("invalid interval given! interval ignored"TLogger::ERROR"Lithron.LPeriodicTask");
  89.                 else
  90.                 {
  91.                     $last $this->Application->getGlobalState('LPeriodicTask:Interval:'.$iid);
  92.                     if ($last $res$p 0.0;
  93.                 }
  94.             }
  95.             $r strtolower($node->getAttribute('recursive')) != "";
  96.  
  97.             $q rand(0100000100000;
  98.             if ($q $pcontinue;
  99.             Prado :: log("Doing Scan!"TLogger::NOTICE"Lithron.LPeriodicTask");
  100.             $this->doScan($d$node$r);
  101.  
  102.             if ($iid)
  103.                 $this->Application->setGlobalState('LPeriodicTask:Interval:'.$iid$this->now);
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Executes on scan task by finding the files and working through all conditions and actions
  109.      * @param string $dir directory to scan
  110.      * @param TXmlElement $node scan node
  111.      * @param boolean $rec do a recursive scan
  112.      */
  113.     private function doScan($dir$node$rec)
  114.     {
  115.             $iter new DirectoryIterator($dir);
  116.             foreach($iter as $file)
  117.             {
  118.                 if ($file->isDot()) continue;
  119.                 if ($rec && $file->isDir()) $this->doScan($file->getPathname()$node$rec);
  120.  
  121.                 $els $node->getElements();
  122.                 foreach($els as $cmdnode)
  123.                 {
  124.                     switch($cmdnode->getTagname())
  125.                     {
  126.                         case "condition":
  127.                             if (!$this->evalCondition($file$cmdnode))
  128.                                 continue 3;
  129.                             break;
  130.                         case "action":
  131.                             if (!$this->execAction($file$cmdnode))
  132.                                 continue 3;
  133.                             break;
  134.                         default:
  135.                             Prado :: log("unknown node under scan node ignored (".$cmdnode->getTagname().")!"TLogger::WARNING"Lithron.LPeriodicTask");
  136.                     }
  137.                 }
  138.             }
  139.     }
  140.  
  141.     /**
  142.      * returns a timestamp from a string converted by strtotime
  143.      * @param string $value the string
  144.      * @return integer timestamp, -1 on error
  145.      */
  146.     private function strToStamp($value)
  147.     {
  148.         $res strtotime($value$this->now);
  149.         if ($res == -1)
  150.             Prado :: log("unrecognized time value ('$value')! assuming condition met!"TLogger::WARNING"Lithron.LPeriodicTask");
  151.         return $res;
  152.     }
  153.  
  154.  
  155.     /**
  156.      * Evaluates a condition for a file and returns the result
  157.      * @param SplFileInfo $file the file
  158.      * @param TXmlElement $condnode the condition node
  159.      * @return boolean true if condition is met, false otherwise
  160.      */
  161.     private function evalCondition($file$condnode)
  162.     {
  163.         $attrs $condnode->getAttributes();
  164.         foreach($attrs as $key => $value)
  165.         {
  166.             switch($key)
  167.             {
  168.                 case "fileatime":
  169.                     if (($res $this->strToStamp($value)) == -1break;
  170.                     if ($file->getATime($resreturn false;
  171.                     break;
  172.                 case "filectime":
  173.                     if (($res $this->strToStamp($value)) == -1break;
  174.                     if ($file->getCTime($resreturn false;
  175.                     break;
  176.                 default:
  177.                     Prado :: log("unknown condition attribute ('$key'). assuming condition met!"TLogger::WARNING"Lithron.LPeriodicTask");
  178.             }
  179.         }
  180.         return true;
  181.     }
  182.  
  183.     /**
  184.      * Executes an action for a file and returns the result
  185.      * @param SplFileInfo $file the file
  186.      * @param TXmlElement $condnode the condition node
  187.      * @return boolean true if action successfully executed, false otherwise
  188.      */
  189.     private function execAction($file$condnode)
  190.     {
  191.         $attrs $condnode->getAttributes();
  192.         foreach($attrs as $key => $value)
  193.         {
  194.             switch($key)
  195.             {
  196.                 case "touch":
  197.                     if (!touch($file->getPathname()))
  198.                         Prado :: log("touch action failed on '".$file->getPathname()."'!"TLogger::ERROR"Lithron.LPeriodicTask");
  199.                     else
  200.                         Prado :: log("touch action successfull on '".$file->getPathname()."'!"TLogger::NOTICE"Lithron.LPeriodicTask");
  201.                     break;
  202.                 case "delete":
  203.                     if (!unlink($file->getPathname()))
  204.                         Prado :: log("delete action failed on '".$file->getPathname()."'!"TLogger::ERROR"Lithron.LPeriodicTask");
  205.                     else
  206.                         Prado :: log("delete action successfull on '".$file->getPathname()."'!"TLogger::NOTICE"Lithron.LPeriodicTask");
  207.                     break;
  208.                 default:
  209.                     Prado :: log("unknown action attribute ('$key'). no action taken!"TLogger::WARNING"Lithron.LPeriodicTask");
  210.             }
  211.         }
  212.         return true;
  213.     }
  214.  
  215.  
  216. }
  217.  
  218. ?>

Documentation generated on Sun, 25 Feb 2007 16:11:32 +0100 by phpDocumentor 1.3.1