From 34af2a3a8e1f7b9fa6a9797f6d501bc3f2c1fd84 Mon Sep 17 00:00:00 2001 From: Dries Buytaert <dries@buytaert.net> Date: Sat, 19 May 2001 13:41:52 +0000 Subject: [PATCH] CHANGES: - Rewrote the cron system. Removed cron.module and moved all cron related options to settings.module. Cron was a confusing thing: it has been made simpler both in terms of code and configuration. + You had to rehash your modules to make the cron show up in the list. This is no longer required. + You couldn't tell what cron "watchdog" or cron "story" were up to. Instead, we now display a clear description message for every cron involved. + The user interface of setting.module - and the admin section in general, looks a bit ackward but I couldn't care less and don't want to see this improve at the time being. - Improved setting.module: + Now uses variable_set(). + Added some help and documentaition on how to setup cron. - Improved ./export. - Updated CHANGELOG. TODO: - I'm now going to look into UnConeD's question with regard to check_output() and $theme->node(), as well as the filter and macro stuff. I'll probably be fine-tuning setting.module a bit more on my way. --- CHANGELOG | 2 +- cron.php | 9 +---- export | 7 +--- includes/module.inc | 16 -------- includes/variable.inc | 9 +++++ modules/cron.module | 65 -------------------------------- modules/cvs.module | 22 +++++++---- modules/headline.module | 42 +++++++++++++-------- modules/rating.module | 28 +++++++++----- modules/settings.module | 38 ++++++++++++------- modules/watchdog.module | 10 ++++- modules/watchdog/watchdog.module | 10 ++++- updates/2.00-to-x.xx.sql | 3 ++ 13 files changed, 117 insertions(+), 144 deletions(-) delete mode 100644 modules/cron.module diff --git a/CHANGELOG b/CHANGELOG index 8c82eb01dce0..c556edd4e720 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -24,6 +24,7 @@ drupal x.xx, xx/xx/xxxx (CVS, unstable) * allows to display and mail CVS log messages as daily digests. - added book.module: * allows collaborative handbook writing: primary used for drupal documentation. +- removed cron.module and integrated it into settings.module. - various updates: * introduced links/drupal tags: [[link]] * added preview functionality when submitting new content (such as a story) from the administration pages. @@ -69,7 +70,6 @@ drupal 2.00, 15/03/2001 + user accounts can be deleted. + added fine-grained permission support. * improved block module - * improved cron module * improved diary module: + diary entries can be deleted * improved headline module: diff --git a/cron.php b/cron.php index 148a3b492137..e59c90e98276 100644 --- a/cron.php +++ b/cron.php @@ -2,13 +2,6 @@ include_once "includes/common.inc"; -function cron_run() { - $time = time(); - $result = db_query("SELECT * FROM crons WHERE $time - timestamp > scheduled"); - while ($task = db_fetch_object($result)) module_invoke($task->module, "cron"); - db_query("UPDATE crons SET timestamp = $time WHERE $time - timestamp > scheduled"); -} - -cron_run(); +foreach (module_list() as $module) module_invoke($module, "cron"); ?> \ No newline at end of file diff --git a/export b/export index 50e402ea4eda..c11073879d09 100644 --- a/export +++ b/export @@ -2,11 +2,6 @@ include_once "includes/common.inc"; -function export($name) { - global $REQUEST_URI; - module_invoke($name, "export", explode("/", $REQUEST_URI)); -} - -module_iterate("export"); +foreach (module_list() as $module) module_invoke($module, "export", explode("/", $REQUEST_URI)); ?> \ No newline at end of file diff --git a/includes/module.inc b/includes/module.inc index 9d1d05dcf096..aa631faecf18 100644 --- a/includes/module.inc +++ b/includes/module.inc @@ -53,18 +53,6 @@ function module_hook($name, $hook) { return function_exists($name ."_". $hook); } -// rehash module-exported crons: -function module_rehash_crons($name) { - if (module_hook($name, "cron")) { - if (!db_fetch_object(db_query("SELECT * FROM crons WHERE module = '$name'"))) { - db_query("INSERT INTO crons (module, scheduled, timestamp) VALUES ('$name', '172800', '0')"); - } - } - else { - db_query("DELETE FROM crons WHERE module = '$name'"); - } -} - // rehash module-exported blocks: function module_rehash_blocks($name) { db_query("UPDATE blocks SET remove = '1' WHERE module = '$name'"); @@ -93,9 +81,6 @@ function module_rehash($name) { db_query("INSERT INTO modules (name) VALUES ('$name')"); } - // rehash module-exported crons (if necessary): - module_rehash_crons($name); - // rehash module-exported blocks (if necessary): module_rehash_blocks($name); } @@ -103,7 +88,6 @@ function module_rehash($name) { // remove all reference to module: db_query("DELETE FROM modules WHERE name = '$name'"); db_query("DELETE FROM blocks WHERE module = '$name'"); - db_query("DELETE FROM crons WHERE module = '$name'"); } } diff --git a/includes/variable.inc b/includes/variable.inc index fa389e51148b..5f47c03e658c 100644 --- a/includes/variable.inc +++ b/includes/variable.inc @@ -36,4 +36,13 @@ function variable_get($name, $default, $object = 0) { } } +function variable_set($name, $value) { + global $conf; + + db_query("DELETE FROM variable WHERE name = '". check_input($name) ."'"); + db_query("INSERT INTO variable (name, value) VALUES ('". check_input($name) ."', '". check_input($value) ."')"); + + $conf[$name] = $value; +} + ?> \ No newline at end of file diff --git a/modules/cron.module b/modules/cron.module deleted file mode 100644 index 97b59a10e69d..000000000000 --- a/modules/cron.module +++ /dev/null @@ -1,65 +0,0 @@ -<?php - -function cron_help() { - ?> - <P>Cron (which stands for chronograph) is a periodic command scheduler: it executes commands at intervals specified in seconds. It can be used to control the execution of daily, weekly and monthly jobs (or anything with a period of <i>n</i> seconds). Automating tasks is one of the best ways to keep a system running smoothly, and if most of your administration does not require your direct involvement, cron is an ideal solution.</P> - <P>Note that cron does not guarantee the commands will be executed at the specified interval. However, the engine will make sure that the commands are run as close to the specified intervals as possible.</P> - <P>Check the documentation page for more information about cron and how to setup it correctly.</P> - <?php -} - -function cron_save($edit) { - foreach ($edit as $key=>$value) { - db_query("UPDATE crons SET scheduled = '$value' WHERE module = '$key'"); - } -} - -function cron_execute($name) { - watchdog("message", "cron: executed '". $name ."_cron()'"); - module_invoke($name, "cron"); - db_query("UPDATE crons SET timestamp = ". time() ." WHERE module = '$name'"); -} - -function cron_display() { - $intervals = array(300, 900, 1800, 3600, 7200, 10800, 21600, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200); - - // Perform query: - $result = db_query("SELECT * FROM crons"); - - // Generate output: - $output .= "<FORM ACTION=\"admin.php?mod=cron\" METHOD=\"post\">\n"; - $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n"; - $output .= " <TR><TH>module</TH><TH>period</TH><TH>last run</TH><TH>next run</TH><TH>operations</TH></TR>\n"; - while ($cron = db_fetch_object($result)) { - foreach ($intervals as $value) $period .= "<OPTION VALUE=\"$value\"". (($cron->scheduled == $value) ? " SELECTED" : "") .">every ". format_interval($value) ."</OPTION>\n"; - $output .= " <TR><TD>". check_output($cron->module) ."</TD><TD><SELECT NAME=\"edit[$cron->module]\">$period</SELECT></TD><TD>". ($cron->timestamp ? format_interval(time() - $cron->timestamp) ." ago" : "never") ."</TD><TD>". ($cron->timestamp ? format_interval($cron->timestamp + $cron->scheduled - time()) ." left" : "never") ."</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=cron&op=execute&name=$cron->module\">execute</A></TD></TR>\n"; - unset($period); - } - $output .= "</TABLE>\n"; - $output .= "<INPUT NAME=\"op\" TYPE=\"submit\" VALUE=\"Save crons\">\n"; - $output .= "</FORM>\n"; - print $output; -} - -function cron_admin() { - global $op, $edit, $name; - - print "<SMALL><A HREF=\"admin.php?mod=cron\">overview</A> | <A HREF=\"admin.php?mod=cron&op=help\">help</A></SMALL><HR>\n"; - - switch($op) { - case "help": - cron_help(); - break; - case "execute": - cron_execute($name); - cron_display(); - break; - case "Save crons": - cron_save($edit); - // fall through - default: - cron_display(); - } -} - -?> diff --git a/modules/cvs.module b/modules/cvs.module index 8aaf65b0895e..f31dd2d26784 100644 --- a/modules/cvs.module +++ b/modules/cvs.module @@ -1,19 +1,27 @@ <?php function cvs_cron() { - $result = db_query("SELECT * FROM cvs WHERE status = '0' ORDER BY timestamp DESC LIMIT 50"); + if (time() - variable_get("cvs_cron_last", 0) > variable_get("cvs_cron_time", time())) { - while ($cvs = db_fetch_object($result)) { - $body .= "File: $cvs->files\nDate: ". format_date($cvs->timestamp) ."\nUser: $cvs->user\n\n$cvs->message\n----------------------------------------------------------------------\n"; - } + print "cvs cron ran"; - $result = db_query("UPDATE cvs SET status = '1'"); + variable_set("cvs_cron_last", time()); - if ($body) mail(variable_get(cvs_mail, "root@localhost"), "CVS log messages", $body, "From: no-reply"); + $result = db_query("SELECT * FROM cvs WHERE status = '0' ORDER BY timestamp DESC LIMIT 50"); + while ($cvs = db_fetch_object($result)) { + $body .= "File: $cvs->files\nDate: ". format_date($cvs->timestamp) ."\nUser: $cvs->user\n\n$cvs->message\n----------------------------------------------------------------------\n"; + } + $result = db_query("UPDATE cvs SET status = '1'"); + + if ($body) mail(variable_get(cvs_mail, "root@localhost"), "CVS log messages", $body, "From: no-reply"); + } } function cvs_conf() { - return form_textfield(t("CVS digest recepient"), "cvs_mail", variable_get(cvs_mail, "root@localhost"), 30, 55, t("The e-mail address to mail the CVS log messages to. Multiple recipients can be specified by putting a comma between each address.")); + $period = array(43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600)); + $output .= form_textfield(t("Digest recepients"), "cvs_mail", variable_get("cvs_mail", "root@localhost"), 30, 55, t("The e-mail address to mail the CVS log messages to. Multiple recipients can be specified by putting a comma between each address.")); + $output .= form_select(t("Digest interval"), "cvs_cron_time" , variable_get("cvs_cron_time", 86400), $period, t("The time interval at which batched CVS digests are dispatched. Requires crontab.")); + return $output; } function cvs_page() { diff --git a/modules/headline.module b/modules/headline.module index 8402e504a459..cfb347086ce5 100644 --- a/modules/headline.module +++ b/modules/headline.module @@ -2,6 +2,33 @@ include_once "modules/backend.class"; +function headline_help() { + ?> + <P>Drupal's headline module both imports and exports RDF/RSS headlines.</P> + <P>A lot of news-oriented websites are now publishing news (headlines) and making their content available through XML, RSS and RDF backend files. They syndicate free content and allow retrieval and further transmission, aggregation, and online publication. In its current state, drupal's headline module supports RDF and RSS backends.</P> + <P>RSS was originally developed by Netscape to allow adding news channels to "My Netscape" sites, but it has since become adopted as the <I>de facto</I> net standard for distributing headlines and brief dynamic texts.</P> + <P>The headline module goes out to a list of configured news sites once an hour or so (driven by cron), downloads new RSS/RDF data and makes it available to your visitors. In addition, your headlines are exported as well and can be retrieved by other sites from <CODE><?php echo path_uri(); ?>export/headlines.rdf</CODE>.</P> + <?php +} + +function headline_conf() { + $period = array(900 => format_interval(900), 1800 => format_interval(1800), 3600 => format_interval(3600), 7200 => format_interval(7200), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 64800 => format_interval(64800), 86400 => format_interval(86400)); + $output .= form_select(t("Update interval"), "headline_cron_time" , variable_get("headline_cron_time", 86400), $period, t("The update interval indicating how often you want to update your headline channels. Requires crontab.")); + return $output; +} + + +function headline_cron() { + if (time() - variable_get("headline_cron_last", 0) > variable_get("headline_cron_time", time())) { + variable_set("headline_cron_last", time()); + + $result = db_query("SELECT * FROM channel"); + while ($channel = db_fetch_object($result)) { + $backend = new Backend($channel->id); + } + } +} + function headline_blocks() { global $theme; @@ -52,21 +79,6 @@ function headline_page() { } } -function headline_cron() { - $result = db_query("SELECT * FROM channel"); - while ($channel = db_fetch_object($result)) { - $backend = new Backend($channel->id); - } -} - -function headline_help() { - ?> - <P>Drupal's headline module both imports and exports RDF/RSS headlines.</P> - <P>A lot of news-oriented websites are now publishing news (headlines) and making their content available through XML, RSS and RDF backend files. They syndicate free content and allow retrieval and further transmission, aggregation, and online publication. In its current state, drupal's headline module supports RDF and RSS backends.</P> - <P>RSS was originally developed by Netscape to allow adding news channels to "My Netscape" sites, but it has since become adopted as the <I>de facto</I> net standard for distributing headlines and brief dynamic texts.</P> - <P>The headline module goes out to a list of configured news sites once an hour or so (driven by cron), downloads new RSS/RDF data and makes it available to your visitors. In addition, your headlines are exported as well and can be retrieved by other sites from <CODE><?php echo path_uri(); ?>export/headlines.rdf</CODE>.</P> - <?php -} function headline_block() { $result = db_query("SELECT * FROM channel"); diff --git a/modules/rating.module b/modules/rating.module index d8382cf7ae6e..3c3e301b72a8 100644 --- a/modules/rating.module +++ b/modules/rating.module @@ -1,17 +1,27 @@ <?php +function rating_conf() { + $period = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600)); + $output .= form_select(t("Update interval"), "rating_cron_time" , variable_get("rating_cron_time", 86400), $period, t("The update interval for the user ratings. Requires crontab.")); + return $output; +} + function rating_cron() { - $r1 = db_query("SELECT id FROM users ORDER BY rating DESC"); - while ($account = db_fetch_object($r1)) { - db_query("UPDATE users SET rating = '". user_gravity($account->id) ."' WHERE id = '$account->id'"); - $rating[$account->id] = ++$i; - } + if (time() - variable_get("rating_cron_last", 0) > variable_get("rating_cron_time", time())) { + variable_set("rating_cron_last", time()); + + $r1 = db_query("SELECT id FROM users ORDER BY rating DESC"); + while ($account = db_fetch_object($r1)) { + db_query("UPDATE users SET rating = '". user_gravity($account->id) ."' WHERE id = '$account->id'"); + $rating[$account->id] = ++$i; + } - db_query("DELETE FROM rating"); + db_query("DELETE FROM rating"); - $r2 = db_query("SELECT id FROM users ORDER BY rating DESC"); - while ($account = db_fetch_object($r2)) { - db_query("INSERT INTO rating (user, new, old) VALUES ('$account->id', '". ++$j ."', '". $rating[$account->id] ."')"); + $r2 = db_query("SELECT id FROM users ORDER BY rating DESC"); + while ($account = db_fetch_object($r2)) { + db_query("INSERT INTO rating (user, new, old) VALUES ('$account->id', '". ++$j ."', '". $rating[$account->id] ."')"); + } } } diff --git a/modules/settings.module b/modules/settings.module index 2edda844a4ef..736ddbee220c 100644 --- a/modules/settings.module +++ b/modules/settings.module @@ -1,5 +1,17 @@ <?php +function settings_help() { + ?> + <P>Drupal comes with system-wide defaults but the setting-module provides control over many Drupal preferences, behaviors including visual and operational settings.</P> + <H3>Cron</H3> + <P>Some settings require a <I>cron</I> or <I>crontab</I>. Cron (which stands for chronograph) is a periodic command scheduler: it executes commands at intervals specified in seconds. It can be used to control the execution of daily, weekly and monthly jobs (or anything with a period of <i>n</i> seconds). Automating tasks is one of the best ways to keep a system running smoothly, and if most of your administration does not require your direct involvement, cron is an ideal solution.</P> + <P>Whenever <A HREF="<?php echo path_uri(); ?>cron.php"><?php echo path_uri(); ?>cron.php</A> is accessed, cron will run: it checks for the jobs cron controls, and their periods in seconds. If a certain task wasn't executed in the last n seconds, where n is the period of that job, it will be executed. When all the executed commands terminate, cron is done.</P> + <P>The recommended way to setup your cron system is to setup a Unix/Linux crontab that frequently visits <A HREF="<?php echo path_uri(); ?>cron.php"><?php echo path_uri(); ?>cron.php</A>. Note that cron does not guarantee the commands will be executed at the specified interval. However, Drupal will try his best and run the crons as close to the specified intervals as possible. The more you visit cron.php, the more accurate cron will be.</P> + <P>If your hosting company does not allow you to setup crontabs, you can always ask someone else to setup a crontab for you. After all, virtually any Unix/Linux machine with access to the internet can setup a crontab to frequently visit <A HREF="<?php echo path_uri(); ?>cron.php"><?php echo path_uri(); ?>cron.php</A>.</P> + <P>For the Unix/Linux crontab itself, use a browser like <I>lynx</I> or <I>wget</I> but make sure the process terminates: either use <CODE>/usr/bin/lynx -source <?php echo path_uri(); ?>cron.php</CODE> or <CODE>/usr/bin/wget -O /dev/null <?php echo path_uri(); ?>cron.php</CODE>. Take a look at the example scripts in the <CODE>scripts</CODE>-directory and make sure to adjust them to your needs. A good crontab-line to run the cron-script once every hour would be: <CODE>00 * * * * /home/www/drupal/scripts/cron-lynx</CODE>.</P> + <?php +} + function settings_conf() { global $conf, $cmodes, $corder, $themes; @@ -41,7 +53,7 @@ function settings_conf() { // development settings: $output .= "<H3>Development settings</H3>\n"; - $output .= form_select(t("Display timings"), "dev_timing", variable_get(dev_timing, 0), array("Disabled", "Enabled"), t("Display the time it took to generate a page: for drupal development only.")); + $output .= form_select(t("Display timings"), "dev_timing", variable_get(dev_timing, 0), array("Disabled", "Enabled"), t("Display the time it took to generate a page: for Drupal development only.")); return $output; } @@ -57,17 +69,9 @@ function setting_modules() { return $output; } -function settings_save($edit) { - global $conf; - - // save all variables: - if ($edit) { - db_query("DELETE FROM variable"); - foreach ($edit as $name=>$value) db_query("INSERT INTO variable (name, value) VALUES ('". check_input($name) ."', '". check_input($value) ."')"); - } - - // update context: - $conf = variable_init(); +function settings_save($edit = array()) { + // save variables: + foreach ($edit as $name=>$value) variable_set($name, $value); return "all settings have been saved."; } @@ -90,15 +94,23 @@ function settings_overview() { function settings_admin() { global $edit, $op; + print "<SMALL><A HREF=\"admin.php?mod=settings\">overview</A> | <A HREF=\"admin.php?mod=settings&op=help\">help</A></SMALL><HR>\n"; + switch ($op) { + case "help": + settings_help(); + break; case "Reset to defaults": print status(settings_default($edit)); + print settings_overview(); break; case "Save settings": print status(settings_save($edit)); + print settings_overview(); break; + default: + print settings_overview(); } - print settings_overview(); } ?> \ No newline at end of file diff --git a/modules/watchdog.module b/modules/watchdog.module index a3d04b36dc25..8397da6668b0 100644 --- a/modules/watchdog.module +++ b/modules/watchdog.module @@ -3,12 +3,18 @@ function watchdog_help() { ?> <P>The watchdog module monitors your website, captures system events in a log and records them to be reviewed by an authorized individual at a later time. The watchdog log is simply a list of events recorded during operation and contains usage data, performance data, errors, warnings and operational information. It is vital to check the watchdog report on a regular basis as it is often the only way to tell what is going on.</P> - <P>To ease administration, the watchdog will automatically remove old logs.</P> + <P>To ease administration, the watchdog will automatically discard old log entries.</P> <?php } +function watchdog_conf() { + $period = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200)); + $output .= form_select(t("Discard entries older than"), "watchdog_clear", variable_get("watchdog_clear", 604800), $period, t("The time watchdog entries should be kept. Older entries will be automatically discarded. Requires crontab.")); + return $output; +} + function watchdog_cron() { - db_query("DELETE FROM watchdog WHERE ". time() ." - timestamp > ". variable_get(watchdog_clear, "302400")); + db_query("DELETE FROM watchdog WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800)); } function watchdog_overview() { diff --git a/modules/watchdog/watchdog.module b/modules/watchdog/watchdog.module index a3d04b36dc25..8397da6668b0 100644 --- a/modules/watchdog/watchdog.module +++ b/modules/watchdog/watchdog.module @@ -3,12 +3,18 @@ function watchdog_help() { ?> <P>The watchdog module monitors your website, captures system events in a log and records them to be reviewed by an authorized individual at a later time. The watchdog log is simply a list of events recorded during operation and contains usage data, performance data, errors, warnings and operational information. It is vital to check the watchdog report on a regular basis as it is often the only way to tell what is going on.</P> - <P>To ease administration, the watchdog will automatically remove old logs.</P> + <P>To ease administration, the watchdog will automatically discard old log entries.</P> <?php } +function watchdog_conf() { + $period = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200)); + $output .= form_select(t("Discard entries older than"), "watchdog_clear", variable_get("watchdog_clear", 604800), $period, t("The time watchdog entries should be kept. Older entries will be automatically discarded. Requires crontab.")); + return $output; +} + function watchdog_cron() { - db_query("DELETE FROM watchdog WHERE ". time() ." - timestamp > ". variable_get(watchdog_clear, "302400")); + db_query("DELETE FROM watchdog WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800)); } function watchdog_overview() { diff --git a/updates/2.00-to-x.xx.sql b/updates/2.00-to-x.xx.sql index 50b9e851ac4e..2cf722f725f6 100644 --- a/updates/2.00-to-x.xx.sql +++ b/updates/2.00-to-x.xx.sql @@ -141,3 +141,6 @@ ALTER TABLE topic ADD moderate TEXT NOT NULL; ALTER TABLE node ADD users TEXT NOT NULL; ALTER TABLE comments ADD users TEXT NOT NULL; ALTER TABLE users DROP history; + +# 19/05/2001 +DROP TABLE crons; -- GitLab