introducing new way of controling the cronjobs by creating a cron.d-file

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2014-01-13 08:55:39 +01:00
parent c5efe9fd7f
commit 4be52f76eb
14 changed files with 232 additions and 141 deletions

View File

@@ -19,7 +19,8 @@ define('MASTER_CRONJOB', 1);
include_once dirname(dirname(__FILE__)) . '/lib/cron_init.php';
$jobs_to_run = includeCronjobs($debugHandler);
$jobs_to_run = array();
$lastrun_update = array();
/**
* check for --help
@@ -27,41 +28,51 @@ $jobs_to_run = includeCronjobs($debugHandler);
if (isset($argv[1]) && strtolower($argv[1]) == '--help') {
echo "\n*** Froxlor Master Cronjob ***\n\n";
echo "Below are possible parameters for this file\n\n";
echo "--force\t\t\tforces re-generating of config-files (webserver, etc.)\n";
echo "--force-[cronname]\tforces the given cron to run, e.g. --force-mailboxsize, --force-traffic\n\n";
echo "--[cronname]\t\t\tincludes the given cron-file\n";
echo "--force\t\t\tforces re-generating of config-files (webserver, nameserver, etc.)\n\n";
}
/**
* check for --force to include cron_tasks
* even if it's not its turn
* check for parameters
*
* --[cronname] include [cronname]
* --force to include cron_tasks even if it's not its turn
*/
for ($x = 1; $x < count($argv); $x++) {
if (isset($argv[$x]) && strtolower($argv[$x]) == '--force') {
$crontasks = makeCorrectFile(FROXLOR_INSTALL_DIR.'/scripts/jobs/cron_tasks.php');
// really force re-generating of config-files by
// inserting task 1
inserttask('1');
if (!in_array($crontasks, $jobs_to_run)) {
array_unshift($jobs_to_run, $crontasks);
// check argument
if (isset($argv[$x])) {
// --force
if (strtolower($argv[$x]) == '--force') {
$crontasks = makeCorrectFile(FROXLOR_INSTALL_DIR.'/scripts/jobs/cron_tasks.php');
// really force re-generating of config-files by
// inserting task 1
inserttask('1');
addToQueue($jobs_to_run, $crontasks);
$lastrun_update['tasks'] = crontasks;
}
}
elseif (isset($argv[$x]) && substr(strtolower($argv[$x]), 0, 8) == '--force-') {
$crontasks = makeCorrectFile(FROXLOR_INSTALL_DIR.'/scripts/jobs/cron_'.substr(strtolower($argv[$x]), 8).'.php');
if (file_exists($crontasks)) {
if (!in_array($crontasks, $jobs_to_run)) {
array_unshift($jobs_to_run, $crontasks);
// --[cronname]
elseif (substr(strtolower($argv[$x]), 0, 2) == '--') {
if (strlen($argv[$x]) > 3) {
$cronfile = makeCorrectFile(FROXLOR_INSTALL_DIR.'/scripts/jobs/cron_'.substr(strtolower($argv[$x]), 3).'.php');
addToQueue($jobs_to_run, $cronfile);
$lastrun_update[substr(strtolower($argv[$x]), 3)] = cronfile;
}
}
}
}
foreach ($jobs_to_run as $cron) {
require_once $cron;
// do we have anything to include?
if (count($jobs_to_run) > 0) {
// include all jobs we want to execute
foreach ($jobs_to_run as $cron) {
updateLastRunOfCron($lastrun_update, $cron);
require_once $cron;
}
}
fwrite($debugHandler, 'Cronfiles have been included' . "\n");
/*
/**
* we have to check the system's last guid with every cron run
* in case the admin installed new software which added a new user
* so users in the database don't conflict with system users
@@ -71,3 +82,23 @@ checkLastGuid();
// shutdown cron
include_once FROXLOR_INSTALL_DIR . '/lib/cron_shutdown.php';
// -- helper function
function addToQueue(&$jobs_to_run, $cronfile = null, $checkExists = true) {
if ($checkExists == false || ($checkExists && file_exists($crontasks))) {
if (!in_array($cronfile, $jobs_to_run)) {
array_unshift($jobs_to_run, $cronfile);
}
}
}
function updateLastRunOfCron($update_arr, $cronfile) {
foreach ($update_arr as $cron => $cronf) {
if ($cronf == $cronfile) {
$upd_stmt = Database::prepare("
UPDATE `".TABLE_PANEL_CRONRUNS."` SET `lastrun` = UNIX_TIMESTAMP() WHERE `cronfile` = :cron;
");
Database::pexecute($upd_stmt, array('cron' => $cron));
}
}
}