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

@@ -179,7 +179,7 @@ class Settings {
`value` = :value
");
$ins_data = array(
'settinggroup' => $sstr[0],
'group' => $sstr[0],
'varname' => $sstr[1],
'value' => $value
);

View File

@@ -183,3 +183,6 @@ $idna_convert = new idna_convert_wrapper();
// Initialize logging
$cronlog = FroxlorLogger::getInstanceOf(array('loginname' => 'cronjob'));
fwrite($debugHandler, 'Logger has been included' . "\n");
// check for cron.d-generation task and create it if necessary
checkCrondConfigurationFile();

View File

@@ -15,98 +15,6 @@
*
*/
/**
* Function getNextCronjobs
*
* checks which cronjobs have to be executed
*
* @return array array of cron-files which are to be executed
*/
function getNextCronjobs() {
$query = "SELECT `id`, `cronfile` FROM `".TABLE_PANEL_CRONRUNS."` WHERE `interval` <> '0' AND `isactive` = '1' AND (";
$intervals = getIntervalOptions();
$x = 0;
foreach($intervals as $name => $ival) {
if($name == '0') continue;
if($x == 0) {
$query.= "(UNIX_TIMESTAMP(DATE_ADD(FROM_UNIXTIME(`lastrun`), INTERVAL ".$ival.")) <= UNIX_TIMESTAMP() AND `interval` = '".$ival."')";
} else {
$query.= " OR (UNIX_TIMESTAMP(DATE_ADD(FROM_UNIXTIME(`lastrun`), INTERVAL ".$ival.")) <= UNIX_TIMESTAMP() AND `interval` = '".$ival."')";
}
$x++;
}
$query.= ');';
$result = Database::query($query);
$cron_files = array();
// Update lastrun-timestamp
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$cron_files[] = $row['cronfile'];
$upd_stmt = Database::prepare("
UPDATE `".TABLE_PANEL_CRONRUNS."` SET `lastrun` = UNIX_TIMESTAMP() WHERE `id` = :id;"
);
Database::pexecute($upd_stmt, array('id' => $row['id']));
}
return $cron_files;
}
function includeCronjobs($debugHandler) {
global $cronlog;
$cronjobs = getNextCronjobs();
$jobs_to_run = array();
$cron_path = makeCorrectDir(FROXLOR_INSTALL_DIR.'/scripts/jobs/');
if ($cronjobs !== false
&& is_array($cronjobs)
&& isset($cronjobs[0])
) {
foreach ($cronjobs as $cronjob) {
$cron_file = makeCorrectFile($cron_path.$cronjob);
if (!file_exists($cron_file)) {
$cronlog->logAction(CRON_ACTION, LOG_ERROR, 'Wanted to include cronfile "'.$cron_file.'" but this file does not exist!!!');
} else {
$jobs_to_run[] = $cron_file;
}
}
}
return $jobs_to_run;
}
function getIntervalOptions() {
global $lng, $cronlog;
$query = "SELECT DISTINCT `interval` FROM `" . TABLE_PANEL_CRONRUNS . "` ORDER BY `interval` ASC;";
$result = Database::query($query);
$cron_intervals = array();
$cron_intervals['0'] = $lng['panel']['off'];
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
if (validateSqlInterval($row['interval'])) {
$cron_intervals[$row['interval']] = $row['interval'];
} else {
$cronlog->logAction(CRON_ACTION, LOG_ERROR, "Invalid SQL-Interval ".$row['interval']." detected. Please fix this in the database.");
}
}
return $cron_intervals;
}
function getCronjobsLastRun() {
global $lng;

View File

@@ -0,0 +1,81 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2014 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Functions
*
*/
/**
* 1st: check for task of generation
* 2nd: if task found, generate cron.d-file
* 3rd: maybe restart cron?
*/
function checkCrondConfigurationFile() {
// check for task
$result_tasks_stmt = Database::query("
SELECT * FROM `" . TABLE_PANEL_TASKS . "` WHERE `type` = '99'
");
$num_results = Database::num_rows();
// is there a task for re-generating the cron.d-file?
if ($num_results > 0) {
// get all crons and their intervals
$cronfile = "# automatically generated cron-configuration by froxlor\ņ";
$cronfile.= "# do not manually edit this file as it will be re-generated periodically.\ņ";
$cronfile.= "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n#\n";
// get all the crons
$result_stmt = Database::query("
SELECT * FROM `" . TABLE_PANEL_CRONRUNS . "` WHERE `isactive` = '1'
");
while ($row_cronentry = $result_stmt->fetch(PDO::FETCH_ASSOC)) {
// create cron.d-entry
if (preg_match("/(\d+) (MINUTE|HOUR|DAY|WEEK|MONTH)/", $row_cronentry['interval'], $matches)) {
switch($matches[2]) {
case "MINUTE":
$cronfile .= "*/" . $matches[1] . " * * * * ";
break;
case "HOUR":
$cronfile .= "* */" . $matches[1] . " * * * ";
break;
case "DAY":
$cronfile .= "* * */" . $matches[1] . " * * ";
break;
case "WEEK":
$cronfile .= "* * * */" . $matches[1] . " * ";
break;
case "MONTH":
$cronfile .= "* * * * */" . $matches[1] . " ";
break;
}
// create entry-line
$binpath = "/usr/bin/nice -n 5 /usr/bin/php5 -q";
$cronfile .= "root " . $binpath." " . FROXLOR_INSTALL_DIR . "/scripts/froxlor_master_cronjob.php --" . $row_cronentry['cronfile'] . " 1 > /dev/null\n";
}
}
// write the file
if (file_put_contents(Settings::Get("system.cronconfig"), $cronfile) === false) {
// oh snap cannot create new crond-file
die("Oh snap, we cannot create the cron.d file. This should not happen.\nPlease check the path and permissions, the cron will keep trying if you don't stop the cron-service.\n\n");
}
// remove all re-generation tasks
Database::query("DELETE FROM `" . TABLE_PANEL_TASKS . "` WHERE `type` = '99'");
}
return true;
}

View File

@@ -51,6 +51,6 @@ define('TABLE_PANEL_DOMAIN_SSL_SETTINGS', 'domain_ssl_settings');
define('TABLE_DOMAINTOIP', 'panel_domaintoip');
// VERSION INFO
$version = '0.9.32-dev4';
$version = '0.9.32-dev5';
$dbversion = '2';
$branding = '';