Skip to content

Commit

Permalink
Adding Task framework & classes. Cron now is task based. Better loadi…
Browse files Browse the repository at this point in the history
…ng of enviornment
  • Loading branch information
Evan Tahler committed Apr 16, 2011
1 parent 3bbd144 commit 0dff136
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 231 deletions.
9 changes: 5 additions & 4 deletions API/CONFIG.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ $CONFIG['NoLogActions'][] = "A_Blocked_Action";
$CONFIG['NoLogAPIKeys'] = array();
$CONFIG['NoLogAPIKeys'][] = "A_Blocked_APIKey";
$CONFIG['LogTable'] = "LOG";
$CONFIG['LOG_DB'] = $CONFIG['DB']; // There can be a seperate DB in use for logging. The LogTable table is expected to be within this database.

/*********************************************************/
// mySQL database
Expand All @@ -64,16 +63,18 @@ $CONFIG['dbhost'] = "127.0.0.1";
$CONFIG['dbuser'] = "root";
$CONFIG['dbpass'] = "";

$CONFIG['LOG_DB'] = $CONFIG['DB']; // There can be a seperate DB in use for logging. The LogTable table is expected to be within this database.

$CONFIG['MySQLLogFile'] = $CONFIG['LogFolder']."SQL.txt"; // comment me out for no logging of mySQL commands

/*********************************************************/
// CRON
$CONFIG['CronLogFile'] = "log/CRON_LOG.txt";
$CONFIG['CronLogFile'] = $CONFIG['LogFolder']."CRON_LOG.txt";
$CONFIG['MaxLogFileSize'] = 1048576 * 1; // 1MB

$CONFIG['LogsToCheck'] = array(); // log files that might get big that you want to automatically truncate
$CONFIG['LogsToCheck'][] = $CONFIG['App_dir'].$CONFIG['CronLogFile'];
$CONFIG['LogsToCheck'][] = $CONFIG['App_dir'].$CONFIG['MySQLLogFile'];
$CONFIG['LogsToCheck'][] = $CONFIG['CronLogFile'];
$CONFIG['LogsToCheck'][] = $CONFIG['MySQLLogFile'];

/*********************************************************/
// CACHE
Expand Down
102 changes: 12 additions & 90 deletions API/CRON.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/***********************************************
DAVE PHP API
https://github.com/evantahler/PHP-DAVE-API
Expand All @@ -11,106 +10,29 @@
***********************************************/
// Cron example: */1 * * * * /usr/bin/php /path/to/CRON.php > /path/to/CRON_LOG.txt

// show errors on scrern
ini_set("display_errors","1");
error_reporting (E_ALL ^ E_NOTICE);

// working directory
$path = substr(__FILE__,0,(strlen(__FILE__) - strlen("CRON.php")));
chdir($path); unset($path);
$parts = explode("/",__FILE__);
$ThisFile = $parts[count($parts) - 1];
chdir(substr(__FILE__,0,(strlen(__FILE__) - strlen($ThisFile))));
require_once("load_enviorment.php"); unset($parts); unset($ThisFile);

// setup
require("ConnectToDatabase.php");
require("CONFIG.php");
require("DAVE.php");
require("CACHE.php");
require("CommonFunctions.php");
date_default_timezone_set($CONFIG['systemTimeZone']);
load_tasks();

$CRON_OUTPUT = "";
$CRON_OUTPUT = "STARTING CRON @ ".date("m-d-Y H:i:s")."\r\n\r\n";

$CRON_OUTPUT .= date("m-d-Y H:i:s")." \r\n";

/////////////////////////////////////////////////////////////////////////
// Check the CACHE DB table for old entries, and remove them
if($CONFIG['CacheType'] == "DB")
{
$SQL= 'DELETE FROM `'.$CONFIG['DB'].'`.`'.$CONFIG['CacheTable'].'` WHERE (`ExpireTime` < "'.(time() - $CONFIG['CacheTime']).'") ;';
$Status = $DBObj->GetStatus();
if ($Status === true)
{
$DBObj->Query($SQL);
$CRON_OUTPUT .= 'Deleted '.$DBObj->NumRowsEffected()." entries from the CACHE DB. \r\n";
}
}
/////////////////////////////////////////////////////////////////////////
// Check the CACHE Folder table for old entries, and remove them
if($CONFIG['CacheType'] == "FlatFile")
{
$files = scandir($CONFIG['CacheFolder']);
$counter = 0;
foreach ($files as $num => $fname)
{
$ThisFile = $CONFIG['CacheFolder'].$fname;
if (file_exists($ThisFile) && ((time() - filemtime($ThisFile)) > $CONFIG['CacheTime']) && $fname != "." && $fname != ".." && $fname != ".svn")
{
unlink($ThisFile);
$counter++;
}
}
$CRON_OUTPUT .= 'Deleted '.$counter." files from the CACHE direcotry. \r\n";
}

/////////////////////////////////////////////////////////////////////////
// Clear the LOG of old LOG entries, acording to $CONFIG['LogAge']
$Status = $DBObj->GetStatus();
if ($Status === true)
{
$SQL= 'DELETE FROM `'.$CONFIG['LogTable'].'` WHERE (`TimeStamp` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['LogAge'])).'") ;';
$DBObj->Query($SQL);
$CRON_OUTPUT .= 'Deleted '.$DBObj->NumRowsEffected()." entries from the LOG. \r\n";
}

/////////////////////////////////////////////////////////////////////////
// Clear the LOG of old LOG entries, acording to $CONFIG['SessionAge']
$Status = $DBObj->GetStatus();
if ($Status === true)
{
$SQL= 'DELETE FROM `SESSIONS` WHERE (`created_at` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['SessionAge'])).'") ;';
$DBObj->Query($SQL);
$CRON_OUTPUT .= 'Deleted '.$DBObj->NumRowsEffected()." expired Sessions. \r\n";
}

/////////////////////////////////////////////////////////////////////////
// Delete Big Log Files, list set in CONFIG
clearstatcache();
$i = 0;
while ($i < count($CONFIG['LogsToCheck']))
{
if (@filesize($CONFIG['LogsToCheck'][$i]) > $CONFIG['MaxLogFileSize'])
{
$CRON_OUTPUT .= 'Log: '.$CONFIG['LogsToCheck'][$i].'is too big, killing'."\r\n";
unlink($CONFIG['LogsToCheck'][$i]);
$fh = fopen($CONFIG['LogsToCheck'][$i], 'w');
fclose($fh);
chmod($Logs[$i], 0777);
}
$i++;
}

/////////////////////////////////////////////////////////////////////////
// Do something else.....
// Do Tasks

$CRON_OUTPUT .= run_task("CleanCache", $ARGS);
$CRON_OUTPUT .= run_task("CleanLog", $ARGS);
$CRON_OUTPUT .= run_task("CleanSessions", $ARGS);
$CRON_OUTPUT .= run_task("RemoveLargeLogs", $ARGS);

/////////////////////////////////////////////////////////////////////////
// End the log output
$CRON_OUTPUT .= "\r\n\r\n";
echo $CRON_OUTPUT;
$fh = fopen($CONFIG['App_dir'].$CONFIG['CronLogFile'], 'a');
$fh = fopen($CONFIG['CronLogFile'], 'a');
fwrite($fh, $CRON_OUTPUT);
fclose($fh);

$DBObj->close();

exit;
?>
21 changes: 19 additions & 2 deletions API/CommonFunctions.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

/***********************************************
DAVE PHP API
https://github.com/evantahler/PHP-DAVE-API
Evan Tahler | 2011
I am a collection of common API functions that will be re-used, such as special-case input validation.
/*************************************************************/

function humanize_actions()
Expand Down Expand Up @@ -40,6 +38,25 @@ function reload_tables()
}
}

function load_tasks()
{
global $CONFIG;
require_once($CONFIG['App_dir']."Tasks/_BASE.php");
$TaskFiles = glob($CONFIG['App_dir']."Tasks/*.php");
foreach($TaskFiles as $task_file){require_once($task_file); }

return $TaskFiles;
}

function run_task($TaskName, $PARAMS = array())
{
// assumes that tasks have been properly loaded in with load_tasks()
$TaskLog = "Running Task: ".$TaskName."\r\n\r\n";
$_TASK = new $TaskName(true, $PARAMS);
$TaskLog .= $_TASK->get_task_log();
return $TaskLog."\r\n";
}

function create_session()
{
$key = md5( uniqid() );
Expand Down
83 changes: 83 additions & 0 deletions API/TASKS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/***********************************************
DAVE PHP API
https://github.com/evantahler/PHP-DAVE-API
Evan Tahler | 2011
I am the task runner.
Tasks can be run with the following syntax: php API/TASK.php --task=name_of_task, or by instantitaiting the class directly
***********************************************/

$parts = explode("/",__FILE__);
$ThisFile = $parts[count($parts) - 1];
chdir(substr(__FILE__,0,(strlen(__FILE__) - strlen($ThisFile))));
require_once("load_enviorment.php"); unset($parts); unset($ThisFile);

require_once("helper_functions/parseArgs.php");

$TaskFiles = load_tasks();

$ARGS = __parseArgs();

$TaskNames = array();
foreach($TaskFiles as $class_name)
{
$parts = explode("/",$class_name);
$parts = explode(".",$parts[(count($parts) - 1)]);
$class_name = $parts[0];
if ($class_name != "task" && class_exists($class_name))
{
$TaskNames[] = $class_name;
}
}

// help / List
if ($ARGS["h"] == true || $ARGS["help"] == true || $ARGS["l"] == true || $ARGS["list"] == true)
{
echo "Task List:\r\n\r\n";

$max_name_length = 0;
foreach($TaskNames as $class_name)
{
if (strlen($class_name) > $max_name_length)
{
$max_name_length = strlen($class_name);
}
}

foreach($TaskNames as $class_name)
{
echo $class_name::class_name();
$i = strlen($class_name);
while ($i < ($max_name_length + 4)) { echo " "; $i++; }
echo $class_name::get_description();
echo "\r\n";
}
exit;
}

// which task?
$ThisTask = ($ARGS["t"]);
if (strlen($ThisTask) == 0){ $ThisTask = ($ARGS["T"]); }
if (strlen($ThisTask) == 0){ $ThisTask = ($ARGS["task"]); }
if (strlen($ThisTask) == 0)
{
echo "No task provided. Please provide one with -t or --task. Use --list to show available tasks.\r\n";
exit;
}
else
{
if (in_array(($ThisTask), $TaskNames))
{
echo run_task($ThisTask, $ARGS);
}
else
{
echo "That task cannot be found. Use --list to show available tasks.\r\n";
exit;
}
}


?>
48 changes: 48 additions & 0 deletions API/Tasks/CleanCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/***********************************************
DAVE PHP API
https://github.com/evantahler/PHP-DAVE-API
Evan Tahler | 2011
I am a task that will clean both the DB and file caches
***********************************************/

class CleanCache extends task
{
protected static $description = "Use me to clean both the DB and file-based cache";

public function run($PARAMS = array())
{
global $CONFIG, $DBObj;

if (self::check_DBObj())
{
/////////////////////////////////////////////////////////////////////////
// Check the CACHE DB table for old entries, and remove them
$SQL= 'DELETE FROM `'.$CONFIG['DB'].'`.`'.$CONFIG['CacheTable'].'` WHERE (`ExpireTime` < "'.(time() - $CONFIG['CacheTime']).'") ;';
$Status = $DBObj->GetStatus();
if ($Status === true)
{
$DBObj->Query($SQL);
$this->task_log('Deleted '.$DBObj->NumRowsEffected()." entries from the CACHE DB");
}

/////////////////////////////////////////////////////////////////////////
// Check the CACHE Folder table for old entries, and remove them
$files = scandir($CONFIG['CacheFolder']);
$counter = 0;
foreach ($files as $num => $fname)
{
$ThisFile = $CONFIG['CacheFolder'].$fname;
if (file_exists($ThisFile) && ((time() - filemtime($ThisFile)) > $CONFIG['CacheTime']) && $fname != "." && $fname != ".." && $fname != ".svn")
{
unlink($ThisFile);
$counter++;
}
}
$this->task_log('Deleted '.$counter." files from the CACHE directory");
}
}
}

?>
27 changes: 27 additions & 0 deletions API/Tasks/CleanLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/***********************************************
DAVE PHP API
https://github.com/evantahler/PHP-DAVE-API
Evan Tahler | 2011
I am a task that will clean the LOG db acording to the expire time
***********************************************/

class CleanLog extends task
{
protected static $description = "I will remove old entries from the LOG table in the DB";

public function run($PARAMS = array())
{
global $CONFIG, $DBObj;

if (self::check_DBObj())
{
$SQL= 'DELETE FROM `'.$CONFIG['LOG_DB'].'`.`'.$CONFIG['LogTable'].'` WHERE (`TimeStamp` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['LogAge'])).'") ;';
$DBObj->Query($SQL);
$this->task_log('Deleted '.$DBObj->NumRowsEffected()." entries from the LOG Table in the ".$CONFIG['LOG_DB']." DB");
}
}
}

?>
27 changes: 27 additions & 0 deletions API/Tasks/CleanSessions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/***********************************************
DAVE PHP API
https://github.com/evantahler/PHP-DAVE-API
Evan Tahler | 2011
TASK
***********************************************/

class CleanSessions extends task
{
protected static $description = "I will remove old entries from the SESSIONS table in the DB";

public function run($PARAMS = array())
{
global $CONFIG, $DBObj;

if (self::check_DBObj())
{
$SQL= 'DELETE FROM `SESSIONS` WHERE (`created_at` < "'.date('Y-m-d H:i:s',(time() - $CONFIG['SessionAge'])).'") ;';
$DBObj->Query($SQL);
$this->task_log('Deleted '.$DBObj->NumRowsEffected()." entries from the SESSIONS Table in the DB");
}
}
}

?>
Loading

0 comments on commit 0dff136

Please sign in to comment.