4 * Small and fast system tasks trigger
6 * Copyright (C) 2014 Inria (Sylvain Beucler)
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 //putenv('FUSIONFORGE_NO_PLUGINS=true');
25 //putenv('FUSIONFORGE_NO_DB=true');
27 require (dirname(__FILE__).'/../common/include/env.inc.php');
28 require_once $gfcommon.'include/pre.php';
29 require_once $gfcommon.'include/cron_utils.php';
31 // Invalidate users/groups cache e.g. when a user is added to a group
32 // Special-case in 'publish-subscribe' mode
33 function usergroups_sync() {
34 global $usergroups_lastsync;
35 $res = db_query_params("SELECT MAX(last_modified_date) AS lastmodified FROM nss_usergroups");
36 $row = db_fetch_array($res);
37 if ($row['lastmodified'] > $usergroups_lastsync) {
40 $hook_params = array();
41 plugin_hook("usergroups_sync", $hook_params);
42 $usergroups_lastsync = time();
46 function systask_get_script($plugin_id, $systask_type_id) {
48 if ($plugin_id == null) {
49 if (isset($cron_arr[$systask_type_id]))
50 return forge_get_config('source_path')
51 .'/cronjobs/'.$cron_arr[$systask_type_id];
54 $plugins = $pm->GetPlugins(); // reload in case a new plugin was installed
55 if (isset($plugins[$plugin_id])) {
56 $plugin = $pm->GetPluginObject($plugins[$plugin_id]);
57 if ($plugin == null) {
58 $pm->LoadPlugin($plugins[$plugin_id]);
59 $plugin = $pm->GetPluginObject($plugins[$plugin_id]);
61 if (isset($plugin->systask_types[$systask_type_id]))
62 return forge_get_config('plugins_path')."/".$plugin->GetName()
63 ."/cronjobs/".$plugin->systask_types[$systask_type_id];
71 $shortopts = 'v'; // enable verbose mode
72 $longopts = array('verbose');
73 $options = getopt($shortopts, $longopts);
74 if (count($options) != (count($argv)-1)) { // PHP just strips invalid options
75 print "Usage: {$argv[0]} [-v|--verbose]\n";
79 if (isset($options['v']) or isset($options['verbose'])) {
80 print "verbose mode ON\n";
89 // Hack to reopen stdin/stdout/stderr, order is important
90 // https://andytson.com/blog/2010/05/daemonising-a-php-cli-script-on-a-posix-system/
91 // (prevents PHP from exiting when printing anything)
95 $ff_stdin = fopen('/dev/null', 'r');
96 $ff_stdout = fopen('/dev/null', 'w');
97 $ff_stderr = fopen('php://stdout', 'w');
99 // We could fork & continue in the background too, but then we'd have
100 // to manage the PID file as well - best leave this to the init script
103 $pm = plugin_manager_get_object();
107 // Deal with pending requests
108 $res = db_query_params("SELECT * FROM systasks WHERE status=$1"
109 . " ORDER BY systask_id", array('TODO'));
110 if (!$res && !db_connection_status())
112 while ($arr = db_fetch_array($res)) {
114 $script = systask_get_script($arr['plugin_id'], $arr['systask_type_id']);
115 if (!file_exists($script))
116 // Not installed on this node, skipping
118 if (!is_executable($script)) {
119 db_query_params("UPDATE systasks SET status=$1, error_message=$2"
120 . " WHERE systask_id=$3",
122 "Cron job {$arr['plugin_id']}/{$arr['systask_type_id']}"
123 . " '$script' not executable.\n",
124 $arr['systask_id']));
127 db_query_params("UPDATE systasks SET status=$1 WHERE systask_id=$2",
128 array('WIP', $arr['systask_id']));
129 cron_acquire_lock($script);
131 if ($verbose) print "Running: $script... ";
132 system("$script\n", $ret);
134 if ($verbose) print "DONE\n";
135 db_query_params("UPDATE systasks SET status=$1 WHERE systask_id=$2",
136 array('DONE', $arr['systask_id']));
138 if ($verbose) print "ERROR\n";
139 db_query_params("UPDATE systasks SET status=$1 WHERE systask_id=$2",
140 array('ERROR', $arr['systask_id']));
151 // c-file-style: "bsd"