FOSSology  4.4.0
Open Source License Compliance by Open Source Software
admin-scheduler.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 define("TITLE_ADMIN_SCHEDULER", _("Scheduler Administration"));
14 
20 {
21  var $error_info = "";
22  public $operation_array;
23 
24  function __construct()
25  {
26  $this->Name = "admin_scheduler";
27  $this->Title = TITLE_ADMIN_SCHEDULER;
28  $this->MenuList = "Admin::Scheduler";
29  $this->DBaccess = PLUGIN_DB_ADMIN;
30  $this->operation_array = array
31  (
32  "status" => array(_("Status"), _("Display job or scheduler status.")),
33  "database" => array(_("Check job queue"),_("Check for new jobs.")),
34  "reload" => array(_("Reload"), _("Reload fossology.conf.")),
35  "agents" => array(_("Agents"), _("Show a list of enabled agents.")),
36  "verbose" => array(_("Verbose"), _("Change the verbosity level of the scheduler or a job.")),
37  "stop" => array(_("Shutdown Scheduler"), _("Shutdown the scheduler gracefully and stop all background processing. This can take a while for all the agents to quit.")),
38  // "start" => array(_("Start Scheduler"), _("Start Scheduler.")),
39  // "restarts" => array(_("Restart Scheduler"), _("Restart Scheduler.")),
40  "restart" => array(_("Unpause a job"), _("Unpause a job.")),
41  "pause" => array(_("Pause a running job"), _("Pause a running job.")),
42  "priority" => array(_("Priority"), _("Change the priority of a job."))
43  );
44  parent::__construct();
45  }
46 
52  {
53  $V = "";
54  foreach ($this->operation_array as $key => $value) {
55  $V .= "<option value='$key'>$value[0]</option>";
56  }
57  return $V;
58  }
59 
64  function JobListOption()
65  {
66  $job_list_option = "<option value='0'>scheduler</option>";
67  $operation = GetParm('operation', PARM_TEXT);
68  if ("stop" === $operation) {
69  return $job_list_option;
70  }
71  $job_array = GetRunnableJobList(); /* get all job list */
72  if (!empty($job_array)) {
73  foreach ($job_array as $job_id) {
74  $job_list_option .= "<option value='$job_id'>$job_id</option>";
75  }
76  }
77  return $job_list_option;
78  }
79 
85  public function GetOperationText($operation)
86  {
87  $operation_text = '';
88  $job_id = GetParm('job_list', PARM_TEXT);
89  if ('0' == $job_id) {
90  $text = _("scheduler");
91  $job_type = $text;
92  } else {
93  $text = _("job");
94  $job_type = "$text $job_id";
95  }
96  switch ($operation) {
97  case 'status':
98  $text = _("Status of the");
99  $operation_text = "$text $job_type";
100  break;
101  case 'database':
102  $text = "Scheduler checked the job queue";
103  $operation_text = $text;
104  break;
105  case 'reload':
106  $text = _("Configuration information for the agents and hosts reloaded");
107  $operation_text = $text;
108  break;
109  case 'agents':
110  $text = _("Get list of valid agents");
111  $operation_text = $text;
112  break;
113  case 'verbose':
114  $level_id = GetParm('level_list', PARM_TEXT);
115  $verbose_level = log($level_id + 1, 2);
116  $text1 = _("Change the verbosity level of the");
117  $text2 = _("as");
118  $operation_text = "$text1 $job_type $text2 $verbose_level";
119  break;
120  case 'stop':
121  $text = _("Shutdown Scheduler");
122  $operation_text = $text;
123  break;
124  case 'start':
125  $text = _("Start Scheduler");
126  $operation_text = $text;
127  break;
128  case 'restarts':
129  $text = _("Restart Scheduler");
130  $operation_text = $text;
131  break;
132  case 'restart':
133  $text = _("Restart the");
134  $operation_text = "$text $job_type";
135  break;
136  case 'pause':
137  $text = _("Pause the");
138  $operation_text = "$text $job_type";
139  break;
140  case 'priority':
141  $priority_id = GetParm('priority_list', PARM_TEXT);
142  $text1 = _("Change the priority of the");
143  $text2 = _("as");
144  $operation_text = "$text1 $job_type $text2 $priority_id";
145  break;
146  }
147  return $operation_text;
148  }
149 
158  public function OperationSubmit($operation, $job_id, $priority_id, $level_id)
159  {
160  if ("start" === $operation) {
161  // start the scheduler
162  $commu_status = fo_communicate_with_scheduler('status',
163  $response_from_scheduler, $this->error_info);
164  if ($commu_status) {
165  // the scheduler is running
166  $response_from_scheduler = "Warning, the scheduler is running";
167  } else {
168  // start the stopped scheduler
169  $this->error_info = null;
170  $this->StartScheduler();
171  return $this->error_info;
172  }
173  } else if ("restarts" === $operation) { // restart the scheduler
174  $this->StartScheduler('restarts');
175  return $this->error_info;
176  }
177  $commands = $operation;
178  if (! empty($job_id) && 'scheduler' != $job_id) {
179  $commands .= " $job_id";
180  }
181  if (isset($priority_id)) {
182  $commands .= " $priority_id";
183  }
184  if (! empty($level_id)) {
185  $commands .= " $level_id";
186  }
187  $commands = trim($commands);
188  $commu_status = fo_communicate_with_scheduler($commands,
189  $response_from_scheduler, $this->error_info);
190  return $response_from_scheduler . $this->error_info;
191  } // OperationSubmit()
192 
197  function StartScheduler($operation = '')
198  {
199  if ($operation) {
200  $command = "/etc/init.d/fossology restart >/dev/null 2>&1";
201  } else {
202  $command = "/etc/init.d/fossology start >/dev/null 2>&1";
203  }
204  $lastline = system($command, $rc);
205  if ($rc) {
206  $this->error_info = " Failed to start the scheduler, return value is: $rc.";
207  }
208  }
209 
210  public function Output()
211  {
212  $V="";
213  $status_msg = "";
214 
215  $operation = GetParm('operation', PARM_TEXT);
216  $job_id = GetParm('job_list', PARM_TEXT);
217  $priority_id = GetParm('priority_list', PARM_TEXT);
218  $level_id = GetParm('level_list', PARM_TEXT);
219  if (! empty($operation)) {
220  $report = "";
221  $response_from_scheduler = $this->OperationSubmit($operation, $job_id,
222  $priority_id, $level_id);
223  $operation_text = $this->GetOperationText($operation);
224  if (empty($this->error_info)) {
225  $text = _("successfully");
226  $status_msg .= "$operation_text $text.";
227  if (! empty($response_from_scheduler)) {
228  $report .= "<hr style='border-style:dashed'>"; // Add one dashed line
229  $report .= $response_from_scheduler;
230  }
231  } else {
232  $text = _("failed");
233  $status_msg .= "$operation_text $text.";
234  $report .= $this->error_info;
235  }
236  $this->vars['message'] = $status_msg . $report;
237  }
238 
239  $text = _("List of operations:");
240  $V.= $text;
241  $V.= "<ul>";
242  foreach ($this->operation_array as $value) {
243  $V .= "<li><b>$value[0]</b>: $value[1]</li>";
244  }
245  $V.= "</ul>";
246  $V.= "<hr>";
247 
248  $text = _("Select an operation");
249  $V.= "<form id='operation_form' method='post'>";
250  $V.= "<p><li>$text: ";
251  $V.= "<select name='operation' id='operation' onchange='OperationSwich_Get(\""
252  . Traceback_uri() . "?mod=ajax_admin_scheduler&operation=\"+this.value)'<br />\n";
253  $V.= $this->OperationListOption();
254  $V.= "</select>\n";
255  $V.= "<br><br>";
256  $V.= "<div id='div_operation'>";
257  $text = _("Select the scheduler or a job");
258  $V.= "$text: <select name='job_list' id='job_list'>";
259  $V.= $this->JobListOption('status');
260  $V.= "</select>";
261  $V.= "</div>";
262  $V .= "<hr>";
263  $text = _("Submit");
264  $V.= "<input type='submit' value='$text' />\n";
265  $V.= "</form>";
266 
267  $choice = ActiveHTTPscript("OperationSwich");
268  $choice .= "<script language='javascript'>\n
269  function OperationSwich_Reply()\n
270  {\n
271  if ((OperationSwich.readyState==4) && (OperationSwich.status==200)) \n
272  {\n
273  document.getElementById('div_operation').innerHTML = OperationSwich.responseText;\n
274  }\n
275  }\n
276  </script>\n";
277  $V.= $choice;
278 
279  return $V;
280  }
281 }
282 $NewPlugin = new admin_scheduler;
283 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
This is a class for operations on the scheduler from GUI.
OperationListOption()
get the operation list
Output()
This function is called when user output is requested. This function is responsible for content....
__construct()
base constructor. Most plugins will just use this
StartScheduler($operation='')
start the scheduler
OperationSubmit($operation, $job_id, $priority_id, $level_id)
submit the specified operation
JobListOption()
get the job list for the operation 'status'
GetOperationText($operation)
get the related operation text, e.g. the operation text of 'stop' is 'Shutdown Schedule'
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_TEXT
Definition: common-parm.php:20
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
GetRunnableJobList()
Get runnable job list, the process is below:
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39