Source for file LPeriodicTask.php
Documentation is available at LPeriodicTask.php
* @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
* @copyright 2005, diemeisterei GmbH. All rights reserved.
* @author $Author: schmunk $
* @version $Revision: 233 $ $Date: 2006-05-30 17:14:13 +0200 (Di, 30 Mai 2006) $
* @package Phundament.Components
Prado::using("System.Util.TLogger");
* <module id="garbage_collector" class="LGarbageCollector">
* <scan dir="/public/tmp" probability="0.01">
* <condition fileatime="-1440"/>
* This module is used execute tasks periodically
* Initializes the module.
* This method is required by IModule and is invoked by application.
* This just hooks into Application::onLoadStateComplete, because we have no global state
* @param TXmlElement module configuration
public function init($xmlNode)
$this->configNode = $xmlNode;
$this->Application->onLoadStateComplete[] = array($this, "work");
* It loads scanning information from the module configuration, and executes all wanted tasks
* @param object $sender TApplication
public function work($sender, $param)
foreach($this->configNode->getElementsByTagName('scan') as $node)
$d = $node->getAttribute('dir');
if ($dd = rtrim($d, "/")) $d = $dd; // trim trailing slashes if it's not "/"
Prado :: log("scan node has invalid dir attribute ('$d')!", TLogger::ERROR, "Lithron.LPeriodicTask");
$p = strtolower($node->getAttribute('probability'));
Prado :: log("scan node has no probability (using default 1.0)!", TLogger::WARNING, "Lithron.LPeriodicTask");
$iid = $node->getAttribute('id');
Prado :: log("need id for using an interval! interval ignored", TLogger::ERROR, "Lithron.LPeriodicTask");
else if (($res = $this->strToStamp($i)) == - 1)
Prado :: log("invalid interval given! interval ignored", TLogger::ERROR, "Lithron.LPeriodicTask");
$last = $this->Application->getGlobalState('LPeriodicTask:Interval:'. $iid);
if ($last > $res) $p = 0.0;
$r = strtolower($node->getAttribute('recursive')) != "";
$q = rand(0, 100000) / 100000;
Prado :: log("Doing Scan!", TLogger::NOTICE, "Lithron.LPeriodicTask");
$this->doScan($d, $node, $r);
$this->Application->setGlobalState('LPeriodicTask:Interval:'. $iid, $this->now);
* Executes on scan task by finding the files and working through all conditions and actions
* @param string $dir directory to scan
* @param TXmlElement $node scan node
* @param boolean $rec do a recursive scan
private function doScan($dir, $node, $rec)
$iter = new DirectoryIterator($dir);
if ($file->isDot()) continue;
if ($rec && $file->isDir()) $this->doScan($file->getPathname(), $node, $rec);
$els = $node->getElements();
foreach($els as $cmdnode)
switch($cmdnode->getTagname())
if (!$this->evalCondition($file, $cmdnode))
if (!$this->execAction($file, $cmdnode))
Prado :: log("unknown node under scan node ignored (". $cmdnode->getTagname(). ")!", TLogger::WARNING, "Lithron.LPeriodicTask");
* returns a timestamp from a string converted by strtotime
* @param string $value the string
* @return integer timestamp, -1 on error
private function strToStamp($value)
Prado :: log("unrecognized time value ('$value')! assuming condition met!", TLogger::WARNING, "Lithron.LPeriodicTask");
* Evaluates a condition for a file and returns the result
* @param SplFileInfo $file the file
* @param TXmlElement $condnode the condition node
* @return boolean true if condition is met, false otherwise
private function evalCondition($file, $condnode)
$attrs = $condnode->getAttributes();
foreach($attrs as $key => $value)
if (($res = $this->strToStamp($value)) == - 1) break;
if ($file->getATime() > $res) return false;
if (($res = $this->strToStamp($value)) == - 1) break;
if ($file->getCTime() > $res) return false;
Prado :: log("unknown condition attribute ('$key'). assuming condition met!", TLogger::WARNING, "Lithron.LPeriodicTask");
* Executes an action for a file and returns the result
* @param SplFileInfo $file the file
* @param TXmlElement $condnode the condition node
* @return boolean true if action successfully executed, false otherwise
private function execAction($file, $condnode)
$attrs = $condnode->getAttributes();
foreach($attrs as $key => $value)
if (!touch($file->getPathname()))
Prado :: log("touch action failed on '". $file->getPathname(). "'!", TLogger::ERROR, "Lithron.LPeriodicTask");
Prado :: log("touch action successfull on '". $file->getPathname(). "'!", TLogger::NOTICE, "Lithron.LPeriodicTask");
if (!unlink($file->getPathname()))
Prado :: log("delete action failed on '". $file->getPathname(). "'!", TLogger::ERROR, "Lithron.LPeriodicTask");
Prado :: log("delete action successfull on '". $file->getPathname(). "'!", TLogger::NOTICE, "Lithron.LPeriodicTask");
Prado :: log("unknown action attribute ('$key'). no action taken!", TLogger::WARNING, "Lithron.LPeriodicTask");
|