FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-scheduler.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2011-2012 Hewlett-Packard Development Company, L.P.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this library; if not, write to the Free Software Foundation, Inc.
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 ***********************************************************/
18 
42 function fo_scheduler_connect($IPaddr='', $Port='', &$ErrorMsg="")
43 {
44  if (empty($IPaddr)) {
45  $IPaddr = '127.0.0.1';
46  }
47  if (empty($Port)) {
48  $Port = 5555;
49  }
50  if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
51  $ErrorMsg = "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>\n";
52  return false;
53  }
54 
55  $result = @socket_connect($sock, $IPaddr, $Port);
56  if ($result === false) {
57  $ErrorMsg = "Connection to the scheduler failed. Is the scheduler running?<br>";
58  $ErrorMsg .= "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($sock)) . "<br>\n";
59  return false;
60  }
61  return $sock;
62 } // fo_scheduler_connect()
63 
64 
73 function fo_scheduler_read($SchedObj, $MaxSize=2048)
74 {
75  return socket_read($SchedObj, $MaxSize, PHP_NORMAL_READ);
76 } // fo_scheduler_read()
77 
78 
89 function fo_scheduler_write($SchedObj, $msg)
90 {
91  return socket_write($SchedObj, $msg, strlen($msg));
92 } // fo_scheduler_write()
93 
94 
101 function fo_scheduler_close($SchedObj)
102 {
103  socket_close($SchedObj);
104 } // fo_scheduler_close()
105 
131 function fo_communicate_with_scheduler($input, &$output, &$error_msg)
132 {
133  global $SysConf;
134 
135  $address = $SysConf['FOSSOLOGY']['address'];
136  $port = $SysConf['FOSSOLOGY']['port'];
137  $sock = fo_scheduler_connect($address, $port, $error_msg);
138  if ($sock) {
139  $msg = trim($input);
140  $write_result = fo_scheduler_write($sock, $msg);
141  if ($write_result) {
142  while ($buf = fo_scheduler_read($sock)) {
143  /* when get all response from the scheduler for the command 'status' or 'status <job_id>', or 'agents'
144  will get a string 'end' */
145  if (substr($buf, 0, 3) == "end") {
146  break;
147  }
148  if (substr($buf, 0, 8) == "received") { /* get a string 'received'*/
149  /* 1. if the command is not 'status' or 'status <job_id>' or 'agents', when receiving
150  a string 'received', that mean this communication is over.
151  2. if the command is 'status' or 'status <job_id>' or 'agents', first receiving
152  a string 'received', then will receive related response.
153  then a string 'end' as ending.
154  */
155  if (substr($input, 0, 6) != "status" && substr($input, 0, 6) != "agents") {
156  break;
157  }
158  } else { /* do not save the symbol string 'received' as the output, they are just symbols */
159  $output .= "$buf<br>";
160  }
161  }
162  } else {
163  $error_msg = socket_strerror(socket_last_error($sock));
164  }
165  fo_scheduler_close($sock);
166  }
167 
168  return empty($error_msg);
169 }
170 
183 {
184  /* get the raw job list from scheduler
185  send command 'status' to the scheduler, get the all status of runnable jobs and scheduler
186  like:
187  scheduler:[#] daemon:[#] jobs:[#] log:[agentName] port:[#] verbose:[#]
188  job:[#] status:[agentName] type:[agentName] priority:[#] running:[#] finished[#] failed:[#]
189  job:[#] status:[agentName] type:[agentName] priority:[#] running:[#] finished[#] failed:[#]
190  */
191  $command = "status";
192  $command_status = fo_communicate_with_scheduler($command, $status_info, $error_msg);
193  /* can not get status info from the scheduler, so can not get runnable jobs, probably the scheduler is not running */
194  if (false === $command_status) {
195  return ;
196  }
197  $pattern = '/job:(\d+) /';
198  preg_match_all($pattern, $status_info, $matches);
199  /* the $matches[1] is like: Array(1, 2, 3, .., i) */
200  $job_array = $matches[1];
201  sort($job_array, SORT_NUMERIC);
202  return $job_array;
203 }
GetRunnableJobList()
Get runnable job list, the process is below:
fo_scheduler_write($SchedObj, $msg)
Write to the scheduler socket.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
fo_scheduler_close($SchedObj)
Close the scheduler connection (socket).
int socket_connect(char *host, char *port)
Create a socket connection.
Definition: fo_cli.c:65
fo_scheduler_read($SchedObj, $MaxSize=2048)
Read the scheduler socket.
fo_scheduler_connect($IPaddr='', $Port='', &$ErrorMsg="")
Connect to the scheduler.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695