FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-scheduler.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
7 
31 function fo_scheduler_connect($IPaddr='', $Port='', &$ErrorMsg="")
32 {
33  if (empty($IPaddr)) {
34  $IPaddr = '127.0.0.1';
35  }
36  if (empty($Port)) {
37  $Port = 5555;
38  }
39  if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
40  $ErrorMsg = "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>\n";
41  return false;
42  }
43 
44  $result = @socket_connect($sock, $IPaddr, $Port);
45  if ($result === false) {
46  $ErrorMsg = "Connection to the scheduler failed. Is the scheduler running?<br>";
47  $ErrorMsg .= "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($sock)) . "<br>\n";
48  return false;
49  }
50  return $sock;
51 } // fo_scheduler_connect()
52 
53 
62 function fo_scheduler_read($SchedObj, $MaxSize=2048)
63 {
64  return socket_read($SchedObj, $MaxSize, PHP_NORMAL_READ);
65 } // fo_scheduler_read()
66 
67 
78 function fo_scheduler_write($SchedObj, $msg)
79 {
80  return socket_write($SchedObj, $msg, strlen($msg));
81 } // fo_scheduler_write()
82 
83 
90 function fo_scheduler_close($SchedObj)
91 {
92  socket_close($SchedObj);
93 } // fo_scheduler_close()
94 
120 function fo_communicate_with_scheduler($input, &$output, &$error_msg)
121 {
122  global $SysConf;
123 
124  $address = $SysConf['FOSSOLOGY']['address'];
125  $port = $SysConf['FOSSOLOGY']['port'];
126  $sock = fo_scheduler_connect($address, $port, $error_msg);
127  if ($sock) {
128  $msg = trim($input);
129  $write_result = fo_scheduler_write($sock, $msg);
130  if ($write_result) {
131  while ($buf = fo_scheduler_read($sock)) {
132  /* when get all response from the scheduler for the command 'status' or 'status <job_id>', or 'agents'
133  will get a string 'end' */
134  if (substr($buf, 0, 3) == "end") {
135  break;
136  }
137  if (substr($buf, 0, 8) == "received") { /* get a string 'received'*/
138  /* 1. if the command is not 'status' or 'status <job_id>' or 'agents', when receiving
139  a string 'received', that mean this communication is over.
140  2. if the command is 'status' or 'status <job_id>' or 'agents', first receiving
141  a string 'received', then will receive related response.
142  then a string 'end' as ending.
143  */
144  if (substr($input, 0, 6) != "status" && substr($input, 0, 6) != "agents") {
145  break;
146  }
147  } else { /* do not save the symbol string 'received' as the output, they are just symbols */
148  $output .= "$buf<br>";
149  }
150  }
151  } else {
152  $error_msg = socket_strerror(socket_last_error($sock));
153  }
154  fo_scheduler_close($sock);
155  }
156 
157  return empty($error_msg);
158 }
159 
172 {
173  /* get the raw job list from scheduler
174  send command 'status' to the scheduler, get the all status of runnable jobs and scheduler
175  like:
176  scheduler:[#] daemon:[#] jobs:[#] log:[agentName] port:[#] verbose:[#]
177  job:[#] status:[agentName] type:[agentName] priority:[#] running:[#] finished[#] failed:[#]
178  job:[#] status:[agentName] type:[agentName] priority:[#] running:[#] finished[#] failed:[#]
179  */
180  $command = "status";
181  $command_status = fo_communicate_with_scheduler($command, $status_info, $error_msg);
182  /* can not get status info from the scheduler, so can not get runnable jobs, probably the scheduler is not running */
183  if (false === $command_status) {
184  return ;
185  }
186  $pattern = '/job:(\d+) /';
187  preg_match_all($pattern, $status_info, $matches);
188  /* the $matches[1] is like: Array(1, 2, 3, .., i) */
189  $job_array = $matches[1];
190  sort($job_array, SORT_NUMERIC);
191  return $job_array;
192 }
fo_scheduler_write($SchedObj, $msg)
Write to the scheduler socket.
fo_scheduler_close($SchedObj)
Close the scheduler connection (socket).
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
fo_scheduler_read($SchedObj, $MaxSize=2048)
Read the scheduler socket.
GetRunnableJobList()
Get runnable job list, the process is below:
fo_scheduler_connect($IPaddr='', $Port='', &$ErrorMsg="")
Connect to the scheduler.
int socket_connect(char *host, char *port)
Create a socket connection.
Definition: fo_cli.c:54
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690