summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2016-08-22 12:11:27 +0200
committerSimon Rettberg2016-08-22 12:11:27 +0200
commitf2133e98aa5457f99e4ab7e8f8ac48b9720fb777 (patch)
tree734c3b47b99bdb711f5abce76e130c34d84f3bbd
parentMerge branch 'modularization' of git.openslx.org:openslx-ng/slx-admin into mo... (diff)
downloadslx-admin-f2133e98aa5457f99e4ab7e8f8ac48b9720fb777.tar.gz
slx-admin-f2133e98aa5457f99e4ab7e8f8ac48b9720fb777.tar.xz
slx-admin-f2133e98aa5457f99e4ab7e8f8ac48b9720fb777.zip
Modularize cron api
Modules now can have a ./hooks/cron.inc.php to run every X minutes, according to how you set up the cron job for the global /inc/cron.inc.php
-rw-r--r--apis/cron.inc.php42
-rw-r--r--modules-available/eventlog/hooks/cron.inc.php5
-rw-r--r--modules-available/statistics/hooks/cron.inc.php18
-rw-r--r--modules-available/syslog/hooks/cron.inc.php5
4 files changed, 43 insertions, 27 deletions
diff --git a/apis/cron.inc.php b/apis/cron.inc.php
index 7171fb4e..8068eb2e 100644
--- a/apis/cron.inc.php
+++ b/apis/cron.inc.php
@@ -10,41 +10,29 @@
if (!isLocalExecution())
exit(0);
+// Hooks by other modules
+function handleModule($file)
+{
+ include_once $file;
+}
+
+foreach (glob('modules/*/hooks/cron.inc.php') as $file) {
+ preg_match('#^modules/([^/]+)/#', $file, $out);
+ $mod = Module::get($out[1]);
+ if ($mod === false)
+ continue;
+ $mod->activate();
+ handleModule($file);
+}
+
switch (mt_rand(1, 10)) {
-case 1:
- Database::exec("DELETE FROM clientlog WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 90");
- break;
-case 2:
- Database::exec("DELETE FROM eventlog WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 90");
- break;
case 3:
Database::exec("DELETE FROM property WHERE dateline <> 0 AND dateline < UNIX_TIMESTAMP()");
break;
case 4:
Database::exec("DELETE FROM callback WHERE (UNIX_TIMESTAMP() - dateline) > 86400");
break;
-case 5:
- Database::exec("DELETE FROM statistic WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 90");
- break;
-case 6:
- Database::exec("DELETE FROM machine WHERE (UNIX_TIMESTAMP() - lastseen) > 86400 * 365");
- break;
-}
-
-// TODO: Move to some module
-function logstats() {
- $NOW = time();
- $cutoff = $NOW - 86400 * 30;
- $online = $NOW - 610;
- $known = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $cutoff");
- $on = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $online");
- $used = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $online AND logintime <> 0");
- Database::exec("INSERT INTO statistic (dateline, typeid, clientip, username, data) VALUES (:now, '~stats', '', '', :vals)", array(
- 'now' => $NOW,
- 'vals' => $known['val'] . '#' . $on['val'] . '#' . $used['val'],
- ));
}
-logstats();
Trigger::checkCallbacks();
Trigger::ldadp();
diff --git a/modules-available/eventlog/hooks/cron.inc.php b/modules-available/eventlog/hooks/cron.inc.php
new file mode 100644
index 00000000..027acf87
--- /dev/null
+++ b/modules-available/eventlog/hooks/cron.inc.php
@@ -0,0 +1,5 @@
+<?php
+
+if (mt_rand(1, 10) === 1) {
+ Database::exec("DELETE FROM eventlog WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 190");
+} \ No newline at end of file
diff --git a/modules-available/statistics/hooks/cron.inc.php b/modules-available/statistics/hooks/cron.inc.php
new file mode 100644
index 00000000..94c65248
--- /dev/null
+++ b/modules-available/statistics/hooks/cron.inc.php
@@ -0,0 +1,18 @@
+<?php
+
+Database::exec("DELETE FROM statistic WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 190");
+Database::exec("DELETE FROM machine WHERE (UNIX_TIMESTAMP() - lastseen) > 86400 * 365");
+
+function logstats() {
+ $NOW = time();
+ $cutoff = $NOW - 86400 * 30;
+ $online = $NOW - 610;
+ $known = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $cutoff");
+ $on = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $online");
+ $used = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $online AND logintime <> 0");
+ Database::exec("INSERT INTO statistic (dateline, typeid, clientip, username, data) VALUES (:now, '~stats', '', '', :vals)", array(
+ 'now' => $NOW,
+ 'vals' => $known['val'] . '#' . $on['val'] . '#' . $used['val'],
+ ));
+}
+logstats();
diff --git a/modules-available/syslog/hooks/cron.inc.php b/modules-available/syslog/hooks/cron.inc.php
new file mode 100644
index 00000000..c796675f
--- /dev/null
+++ b/modules-available/syslog/hooks/cron.inc.php
@@ -0,0 +1,5 @@
+<?php
+
+if (mt_rand(1, 10) === 1) {
+ Database::exec("DELETE FROM clientlog WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 190");
+} \ No newline at end of file