FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
admin-scheduler.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2011-2013 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***********************************************************/
18 
24 define("TITLE_ADMIN_SCHEDULER", _("Scheduler Administration"));
25 
31 {
32  var $error_info = "";
33  var $operation_array;
34 
35  function __construct()
36  {
37  $this->Name = "admin_scheduler";
38  $this->Title = TITLE_ADMIN_SCHEDULER;
39  $this->MenuList = "Admin::Scheduler";
40  $this->DBaccess = PLUGIN_DB_ADMIN;
41  parent::__construct();
42  }
43 
49  {
50  $V = "";
51  foreach ($this->operation_array as $key => $value) {
52  $V .= "<option value='$key'>$value[0]</option>";
53  }
54  return $V;
55  }
56 
61  function JobListOption()
62  {
63  $job_list_option = "<option value='0'>scheduler</option>";
64  $operation = GetParm('operation', PARM_TEXT);
65  if ("stop" === $operation) {
66  return $job_list_option;
67  }
68  $job_array = GetRunnableJobList(); /* get all job list */
69  for ($i = 0; $i < sizeof($job_array); $i ++) {
70  $job_id = $job_array[$i];
71  $job_list_option .= "<option value='$job_id'>$job_id</option>";
72  }
73  return $job_list_option;
74  }
75 
81  function GetOperationText($operation)
82  {
83  $operation_text = '';
84  $job_id = GetParm('job_list', PARM_TEXT);
85  if ('0' == $job_id) {
86  $text = _("scheduler");
87  $job_type = $text;
88  } else {
89  $text = _("job");
90  $job_type = "$text $job_id";
91  }
92  switch ($operation) {
93  case 'status':
94  $text = _("Status of the");
95  $operation_text = "$text $job_type";
96  break;
97  case 'database':
98  $text = "Scheduler checked the job queue";
99  $operation_text = $text;
100  break;
101  case 'reload':
102  $text = _("Configuration information for the agents and hosts reloaded");
103  $operation_text = $text;
104  break;
105  case 'agents':
106  $text = _("Get list of valid agents");
107  $operation_text = $text;
108  break;
109  case 'verbose':
110  $level_id = GetParm('level_list', PARM_TEXT);
111  $verbose_level = log($level_id + 1, 2);
112  $text1 = _("Change the verbosity level of the");
113  $text2 = _("as");
114  $operation_text = "$text1 $job_type $text2 $verbose_level";
115  break;
116  case 'stop':
117  $text = _("Shutdown Scheduler");
118  $operation_text = $text;
119  break;
120  case 'start':
121  $text = _("Start Scheduler");
122  $operation_text = $text;
123  break;
124  case 'restarts':
125  $text = _("Restart Scheduler");
126  $operation_text = $text;
127  break;
128  case 'restart':
129  $text = _("Restart the");
130  $operation_text = "$text $job_type";
131  break;
132  case 'pause':
133  $text = _("Pause the");
134  $operation_text = "$text $job_type";
135  break;
136  case 'priority':
137  $priority_id = GetParm('priority_list', PARM_TEXT);
138  $text1 = _("Change the priority of the");
139  $text2 = _("as");
140  $operation_text = "$text1 $job_type $text2 $priority_id";
141  break;
142  }
143  return $operation_text;
144  }
145 
154  function OperationSubmit($operation, $job_id, $priority_id, $level_id)
155  {
156  if ("start" === $operation) {
157  // start the scheduler
158  $commu_status = fo_communicate_with_scheduler('status',
159  $response_from_scheduler, $this->error_info);
160  if ($commu_status) {
161  // the scheduler is running
162  $response_from_scheduler = "Warning, the scheduler is running";
163  } else {
164  // start the stopped scheduler
165  $this->error_info = null;
166  $this->StartScheduler();
167  return $this->error_info;
168  }
169  } else if ("restarts" === $operation) { // restart the scheduler
170  $this->StartScheduler('restarts');
171  return $this->error_info;
172  }
173  $commands = $operation;
174  if (! empty($job_id) && 'scheduler' != $job_id) {
175  $commands .= " $job_id";
176  }
177  if (isset($priority_id)) {
178  $commands .= " $priority_id";
179  }
180  if (! empty($level_id)) {
181  $commands .= " $level_id";
182  }
183  $commands = trim($commands);
184  $commu_status = fo_communicate_with_scheduler($commands,
185  $response_from_scheduler, $this->error_info);
186  return $response_from_scheduler . $this->error_info;
187  } // OperationSubmit()
188 
193  function StartScheduler($operation = '')
194  {
195  if ($operation) {
196  $command = "/etc/init.d/fossology restart >/dev/null 2>&1";
197  } else {
198  $command = "/etc/init.d/fossology start >/dev/null 2>&1";
199  }
200  $lastline = system($command, $rc);
201  if ($rc) {
202  $this->error_info = " Failed to start the scheduler, return value is: $rc.";
203  }
204  }
205 
206  public function Output()
207  {
208  $V="";
209  $status_msg = "";
210 
211  $this->operation_array = array
212  (
213  "status" => array(_("Status"), _("Display job or scheduler status.")),
214  "database" => array(_("Check job queue"),_("Check for new jobs.")),
215  "reload" => array(_("Reload"), _("Reload fossology.conf.")),
216  "agents" => array(_("Agents"), _("Show a list of enabled agents.")),
217  "verbose" => array(_("Verbose"), _("Change the verbosity level of the scheduler or a job.")),
218  "stop" => array(_("Shutdown Scheduler"), _("Shutdown the scheduler gracefully and stop all background processing. This can take a while for all the agents to quit.")),
219  // "start" => array(_("Start Scheduler"), _("Start Scheduler.")),
220  // "restarts" => array(_("Restart Scheduler"), _("Restart Scheduler.")),
221  "restart" => array(_("Unpause a job"), _("Unpause a job.")),
222  "pause" => array(_("Pause a running job"), _("Pause a running job.")),
223  "priority" => array(_("Priority"), _("Change the priority of a job."))
224  );
225 
226  $operation = GetParm('operation', PARM_TEXT);
227  $job_id = GetParm('job_list', PARM_TEXT);
228  $priority_id = GetParm('priority_list', PARM_TEXT);
229  $level_id = GetParm('level_list', PARM_TEXT);
230  if (! empty($operation)) {
231  $report = "";
232  $response_from_scheduler = $this->OperationSubmit($operation, $job_id,
233  $priority_id, $level_id);
234  $operation_text = $this->GetOperationText($operation);
235  if (empty($this->error_info)) {
236  $text = _("successfully");
237  $status_msg .= "$operation_text $text.";
238  if (! empty($response_from_scheduler)) {
239  $report .= "<hr style='border-style:dashed'>"; // Add one dashed line
240  $report .= $response_from_scheduler;
241  }
242  } else {
243  $text = _("failed");
244  $status_msg .= "$operation_text $text.";
245  $report .= $this->error_info;
246  }
247  $this->vars['message'] = $status_msg . $report;
248  }
249 
250  $text = _("List of operations:");
251  $V.= $text;
252  $V.= "<ul>";
253  foreach ($this->operation_array as $value) {
254  $V .= "<li><b>$value[0]</b>: $value[1]</li>";
255  }
256  $V.= "</ul>";
257  $V.= "<hr>";
258 
259  $text = _("Select an operation");
260  $V.= "<form id='operation_form' method='post'>";
261  $V.= "<p><li>$text: ";
262  $V.= "<select name='operation' id='operation' onchange='OperationSwich_Get(\""
263  . Traceback_uri() . "?mod=ajax_admin_scheduler&operation=\"+this.value)'<br />\n";
264  $V.= $this->OperationListOption();
265  $V.= "</select>\n";
266  $V.= "<br><br>";
267  $V.= "<div id='div_operation'>";
268  $text = _("Select the scheduler or a job");
269  $V.= "$text: <select name='job_list' id='job_list'>";
270  $V.= $this->JobListOption('status');
271  $V.= "</select>";
272  $V.= "</div>";
273  $V .= "<hr>";
274  $text = _("Submit");
275  $V.= "<input type='submit' value='$text' />\n";
276  $V.= "</form>";
277 
278  $choice = ActiveHTTPscript("OperationSwich");
279  $choice .= "<script language='javascript'>\n
280  function OperationSwich_Reply()\n
281  {\n
282  if ((OperationSwich.readyState==4) && (OperationSwich.status==200)) \n
283  {\n
284  document.getElementById('div_operation').innerHTML = OperationSwich.responseText;\n
285  }\n
286  }\n
287  </script>\n";
288  $V.= $choice;
289 
290  return $V;
291  }
292 }
293 $NewPlugin = new admin_scheduler;
294 $NewPlugin->Initialize();
GetRunnableJobList()
Get runnable job list, the process is below:
Traceback_uri()
Get the URI without query to this location.
StartScheduler($operation= '')
start the scheduler
const PARM_TEXT
Definition: common-parm.php:31
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
GetOperationText($operation)
get the related operation text, e.g. the operation text of &#39;stop&#39; is &#39;Shutdown Schedule&#39; ...
This is a class for operations on the scheduler from GUI.
JobListOption()
get the job list for the operation &#39;status&#39;
OperationListOption()
get the operation list
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
OperationSubmit($operation, $job_id, $priority_id, $level_id)
submit the specified operation
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695