PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Prohits/msManager/autoSearch/auto_search_table_shell.php

http://prohits.googlecode.com/
PHP | 376 lines | 286 code | 30 blank | 60 comment | 48 complexity | bc9b07ca27f27b64e2dbaab4a1b21dc6 MD5 | raw file
  1. <?
  2. /***********************************************************************
  3. Copyright 2010 Gingras and Tyers labs,
  4. Samuel Lunenfeld Research Institute, Mount Sinai Hospital.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. *************************************************************************/
  15. /************************************************************************
  16. date: 2006-05-08
  17. description:
  18. 1. a mass spec machine table name has to passed to this script
  19. 2. get the running task.
  20. 3. set spleep time, if start everyDay.
  21. 4. get new raw file to searchResults tasble, if is autoAddRawfiles
  22. 5. get all not searched raw files from searchResults table.
  23. 6. loop all files and send them to search engine if is not searched.
  24. 7. get searched resuts file.
  25. 8. shell > php auto_search_table_shell.php tableName taskID thisScriptURL sleepSec
  26. 9. check shell process
  27. shell > ps -o lstart PID
  28. 10.
  29. *************************************************************************/
  30. set_time_limit(0);
  31. ini_set("memory_limit","-1");
  32. //ini_set("default_socket_timeout", "1000");
  33. $tableName = '';
  34. $logfile = '../../logs/search.log';
  35. $theTask_arr = array();
  36. $searchEngine_arr = array();
  37. $searchEngines_parameter_arr = array();
  38. $theTaskID = 0;
  39. $raw_file_source_dir = '';
  40. $frm_theTaskID = '';
  41. $frm_theURL = '';
  42. $sleep_time = '';
  43. //change cwd to this directory
  44. chdir(dirname(__FILE__));
  45. include('../../config/conf.inc.php');
  46. include('./auto_search_gpm.inc.php');
  47. include('./auto_search_mascot.inc.php');
  48. include('../autoBackup/shell_functions.inc.php');
  49. include('../../common/mysqlDB_class2.php');
  50. require_once("../../common/HTTP/Request.php");
  51. require_once('../is_dir_file.inc.php');
  52. $php_command_location = PHP_PATH;
  53. //maybe it is remote connect prohits server---------
  54. $host = PROHITS_SERVER_IP;
  55. if(STORAGE_IP == PROHITS_SERVER_IP){
  56. $host = HOSTNAME;
  57. }
  58. $user = USERNAME;
  59. $pswd = DBPASSWORD;
  60. $msManager_link = mysql_connect("$host", $user, $pswd, true, 2 ) or fatalError("Unable to connect to mysql..." . mysql_error($msManager_link));
  61. mysql_select_db(MANAGER_DB, $msManager_link) or fatalError("Can not use '". MANAGER_DB . "' database". mysql_error($msManager_link));
  62. //if(defined('STORAGE_IP'))
  63. if(isset($_SERVER['argv']) and count($_SERVER['argv']) > 1){
  64. $tableName = $_SERVER['argv'][1];
  65. if(isset($_SERVER['argv'][2])){
  66. $frm_theTaskID = $_SERVER['argv'][2];
  67. $theURL = $_SERVER['argv'][3];
  68. $frm_theURL = str_replace("autoSearch/auto_search_table_shell.php","",$theURL) . "autoBackup/download_raw_file.php";
  69. if(isset($_SERVER['argv'][4])){
  70. $sleep_sec = $_SERVER['argv'][4];
  71. if(is_numeric($sleep_sec)){
  72. $msg = "It is ".@date("Y-m-d G:i:s").". Next $tableName (task $frm_theTaskID) process will be ". floor($sleep_sec/3600) . " hours later";
  73. writeLog($msg."\n");
  74. sleep($sleep_sec);
  75. }
  76. }
  77. }
  78. }else if(array_key_exists('REQUEST_METHOD', $_SERVER)){
  79. //processing search now sed by web ******************************************
  80. if( $_SERVER['REQUEST_METHOD'] == "POST"){
  81. $request_arr = $_POST;
  82. }else{
  83. $request_arr = $_GET;
  84. }
  85. foreach ($request_arr as $key => $value) {
  86. $$key=$value;
  87. }
  88. if(isset($SID) and isset($tableName) and check_permission($SID,$tableName)) {
  89. if(isset($kill)){
  90. system("kill $kill");
  91. writeLog("Table:$tableName task ID: $frm_theTaskID has been stopped.");
  92. exit;
  93. }
  94. //get running task
  95. $SQL = "SELECT ID, Schedule, ProcessID FROM ". $tableName ."SearchTasks where ";
  96. if($frm_theTaskID){
  97. $SQL .= "ID='$frm_theTaskID'";
  98. }else{
  99. $SQL .= "Status='Running' order by ID";
  100. }
  101. $result = mysql_query($SQL, $msManager_link);
  102. if($tmp_row = mysql_fetch_assoc($result) ){
  103. $tmp_taskID = $tmp_row['ID'];
  104. $tmp_schedule = $tmp_row['Schedule'];
  105. $tmp_ProcessID = $tmp_row['ProcessID'];
  106. if($tmp_ProcessID) system("kill $tmp_ProcessID");
  107. $theURL = "http://".$_SERVER["SERVER_ADDR"].":".$_SERVER["SERVER_PORT"].$_SERVER["PHP_SELF"];
  108. $com = "$php_command_location " . __FILE__ ." ".$tableName. " ".$tmp_taskID." ". $theURL;
  109. if(defined('DEBUG_SEARCH') and DEBUG_SEARCH){
  110. echo "Prohits stopped by administrator:<br> if youa are Prohits admin copy following line and run it on the server shell for debug.<br>\n";
  111. echo "<font color=green>$com</font>";
  112. exit;
  113. }
  114. echo "Process ID:";
  115. $tmp_PID = system($com." > /dev/null & echo \$!");
  116. $SQL = "update ". $tableName ."SearchTasks set ProcessID='".$tmp_PID."', Status='Running' where ID='".$tmp_taskID."'";
  117. mysql_query($SQL, $msManager_link);
  118. }
  119. echo " auto-search is running in the background";
  120. }else{
  121. echo "no enough info. passed";
  122. }
  123. exit;
  124. //end web interface **********************************************
  125. }else{
  126. $msg = "running on the shell. but no table name passed. the process stopped";
  127. fatalError($msg, __LINE__);
  128. }
  129. //******************************************************************
  130. ///// shell script start from here /////////////////////////////////
  131. checkLogSize($logfile, 2000);
  132. $start_time = @date("Y-m-j G:i:s");
  133. $taskTable = $tableName . "SearchTasks";
  134. $resultTable = $tableName . "SearchResults";
  135. $tableTppTasks = $tableName . "tppTasks";
  136. $tableTppResults = $tableName . "tppResults";
  137. if(getPhpProcess_arr($tableName, $frm_theTaskID) > 1){
  138. echo "the previous request is running.";
  139. exit;
  140. }
  141. $raw_file_pattern = str_replace(" ", "", $RAW_FILES);
  142. $raw_file_pattern = str_replace(".", "\.", $raw_file_pattern);
  143. $raw_file_pattern = '/\.' . str_replace(",", '$|\.', $raw_file_pattern) . '$/i';
  144. check_manager_db_connection();
  145. $results = mysql_list_tables ( MANAGER_DB, $msManager_link );
  146. $tmp_num = 0;
  147. while($row = mysql_fetch_row($results)){
  148. if($row[0] == $tableName or $row[0] == $taskTable or $row[0] == $resultTable) $tmp_num++;
  149. }
  150. if($tmp_num != 3){
  151. $e_msg = error_handle('', $msManager_link);
  152. fatalError("$e_msg \r\n $tableName, $taskTable, $resultTable (3) tables should be in database ". MANAGER_DB, __LINE__);
  153. }
  154. $theTask_arr = get_running_task($frm_theTaskID);
  155. if($theTask_arr){
  156. $frm_theTaskID = $theTask_arr['ID'];
  157. $tmp_schedule = $theTask_arr['Schedule'];
  158. if($theTask_arr['AutoAddFile'] == 'Yes'){
  159. auto_add_files($theTask_arr);
  160. }
  161. $tmp_arr = explode(";", $theTask_arr['SearchEngines']);
  162. foreach($tmp_arr as $en_str){
  163. $the_a = explode("=", $en_str);
  164. array_push($searchEngine_arr, $the_a[0]);
  165. }
  166. $tmp_par_arr = explode("\n", $theTask_arr['Parameters']);
  167. foreach($tmp_par_arr as $par_str){
  168. $engine_par_arr = array();
  169. $the_p = explode("===", $par_str);
  170. $engine_par_arr = explode(";", $the_p[1]);
  171. $searchEngines_parameter_arr[$the_p[0]] = $engine_par_arr;
  172. }
  173. }else{
  174. writeLog("Table:$tableName no Running task. $start_time");
  175. exit;
  176. }
  177. //get all raw files they are not searched.
  178. $SQL = "SELECT S.WellID, S.SearchEngines, T.FileName, T.FolderID from ".$tableName." T, ".$resultTable." S
  179. where T.ID=S.WellID and TaskID='". $theTask_arr['ID'] . "' and (S.DataFiles is NULL or S.DataFiles='') order by S.WellID" ;
  180. $results = mysql_query($SQL, $msManager_link);
  181. //echo $SQL;exit;
  182. writeLog("\nTable:$tableName; Task ID: ". $theTask_arr['ID']. "; Start Time: $start_time; Raw data files: ". mysql_num_rows($results) );
  183. $SQL = "update $taskTable set StartTime=now() where ID='".$theTask_arr['ID']."'";
  184. mysql_query($SQL, $msManager_link);
  185. $folder_path_arr = array(); // array('dirID'=>path_str, ...);
  186. //$theTable_path = preg_replace("/\/$/", "", STORAGE_FOLDER) . "/" . $tableName. "/";
  187. while($row = mysql_fetch_row($results)){
  188. $tmp_WellID = $row[0];
  189. $tmp_Engine = $row['1'];
  190. $tmp_file = $row['2'];
  191. $tmp_FolderID = $row[3];
  192. $tmp_dir_path = getFileDirPath($tableName, $tmp_WellID, $tmp_FolderID);
  193. echo "$tmp_dir_path, $tmp_WellID, $tmp_file, $tmp_Engine";
  194. $tmp_converted_file = checkFileFormat($tmp_dir_path, $tmp_WellID, $tmp_file, $tmp_Engine);
  195. if($tmp_converted_file){
  196. $tmp_raw_file_path = $tmp_dir_path . $tmp_converted_file['fileName'];
  197. if(defined('DEBUG_SEARCH') and DEBUG_SEARCH){
  198. echo "file format checked: ". $tmp_raw_file_path .": $tmp_Engine \n";
  199. }
  200. if($tmp_Engine == 'Mascot'){
  201. searchMascot($tmp_raw_file_path, $tmp_WellID, $searchEngines_parameter_arr['Mascot'], $theTask_arr['LCQfilter'], $theTask_arr['ID'], $tmp_converted_file['type']);
  202. }else if($tmp_Engine == 'GPM'){
  203. searchGPM($tmp_raw_file_path, $tmp_WellID, $searchEngines_parameter_arr['GPM'], $theTask_arr['LCQfilter'], $theTask_arr['ID'], $tmp_converted_file['type']);
  204. }
  205. }
  206. }//end while loop
  207. //cehck if set to run TPP after the search task/////////////////////////////////////////////////
  208. if($theTask_arr['RunTPP']){
  209. $tpp_theURL = $theURL;
  210. //if(PROHITS_SERVER_IP != STORAGE_IP){
  211. // $tpp_theURL = str_replace(STORAGE_IP,PROHITS_SERVER_IP,$theURL);
  212. //}
  213. $tpp_theURL = str_replace("autoSearch/auto_search_table_shell.php","",$tpp_theURL) . "tppTask/tpp_task_shell.php";
  214. $tpp_theURL .='?tableName='.$tableName.'&tppTaskID='.$theTask_arr['RunTPP'];
  215. $handle = fopen($tpp_theURL, "r");
  216. $tppmsg = '';
  217. if($handle){
  218. while (!feof($handle)) {
  219. $tppmsg .= fgets($handle, 4096);
  220. }
  221. if(defined('DEBUG_SEARCH') and DEBUG_SEARCH){
  222. echo "response from: $tpp_theURL\n";
  223. echo $tppmsg;
  224. }
  225. fclose($handle);
  226. }else{
  227. $tppmsg = "Cannot open tpp URL $tpp_theURL";
  228. }
  229. writeLog($tppmsg);
  230. }
  231. ////////////////////////////////run next task ////////////////////////////////////////////////////////////
  232. if(isset($tmp_row['ProcessID'])) system("kill ".$tmp_row['ProcessID'] ." > /dev/null 2>&1 &");
  233. $tmp_row = array();
  234. $end_time = @date("Y-m-d G:i:s");
  235. writeLog("Table:$tableName; Task ID: ". $theTask_arr['ID']. "; End Time: $end_time");
  236. if( strpos($theTask_arr['Schedule'],"Every") === false){
  237. $SQL = "update $taskTable set Status='Finished' where ID='".$theTask_arr['ID']."'";
  238. mysql_query($SQL, $msManager_link);
  239. $tmp_row = get_running_task();
  240. }else{
  241. $tmp_row = get_running_task($frm_theTaskID);
  242. }
  243. if($tmp_row){
  244. $tmp_taskID = $tmp_row['ID'];
  245. $tmp_schedule = $tmp_row['Schedule'];
  246. $tmp_ProcessID = $tmp_row['ProcessID'];
  247. //Every;4
  248. $sch_arr = explode(';', $tmp_schedule);
  249. if($sch_arr[0] == 'Every' and isset($sch_arr[1])){
  250. $sleep_time = $sch_arr[1] * 3600;
  251. $sleep_time = " " . $sleep_time;
  252. }
  253. echo "Process ID:";
  254. $tmp_PID = system("$php_command_location " . __FILE__ ." ".$tableName. " ".$tmp_taskID." ". $theURL."$sleep_time > /dev/null & echo \$!");
  255. $SQL = "update ". $tableName ."SearchTasks set ProcessID='".$tmp_PID."', Status='Running' where ID='".$tmp_taskID."'";
  256. mysql_query($SQL, $msManager_link);
  257. }
  258. exit;
  259. //************************************************
  260. // return taske record array
  261. //************************************************
  262. function get_running_task($frm_theTaskID =''){
  263. global $msManager_link;
  264. global $taskTable;
  265. $SQL = "SELECT ID, PlateID, DataFileFormat, SearchEngines, Parameters, TaskName, LCQfilter, Schedule, StartTime,
  266. AutoAddFile, RunTPP, Status, ProcessID, UserID, ProjectID
  267. FROM ". $taskTable." where ";
  268. if($frm_theTaskID){
  269. $SQL .= "ID='$frm_theTaskID'";
  270. }else{
  271. $SQL .="Status='Running' or Status='Waiting' order by ID";
  272. }
  273. $result = mysql_query($SQL, $msManager_link);
  274. $theTask_arr = mysql_fetch_array($result);
  275. return $theTask_arr;
  276. }
  277. //
  278. //************************************************
  279. //
  280. //************************************************
  281. function check_manager_db_connection(){
  282. global $msManager_link;
  283. if(!mysql_ping($msManager_link)){
  284. mysql_close($msManager_link);
  285. $msManager_link = mysql_connect(PROHITS_SERVER_IP, USERNAME, DBPASSWORD, true, 2 ) or fatalError("Unable to connect to mysql..." . mysql_error($msManager_link));
  286. mysql_select_db(MANAGER_DB, $msManager_link) or fatalError("Can not use '". MANAGER_DB . "' database". mysql_error($msManager_link));
  287. }
  288. }
  289. function error_handle($msg = '', $db_link){
  290. if(mysql_errno($db_link) == "1062"){
  291. return '';
  292. }else{
  293. $msg .= "\nerror #" . mysql_errno($db_link);
  294. $msg .= "\nmysql error: " . mysql_error($db_link);
  295. $msg .= "\nScript Name: " . __FILE__;
  296. }
  297. return $msg;
  298. }
  299. //**********************************************
  300. // it will run the table backup first.
  301. //**********************************************
  302. function auto_add_files($theTask_arr){
  303. global $msManager_link;
  304. global $tableName ;
  305. global $resultTable;
  306. global $tableTppResults;
  307. writeLog("Get new created files from $tableName machine to Prohits.");
  308. $com_backup = PHP_PATH. " " .dirname(dirname(__FILE__))."/autoBackup/raw_backup_shell.php"." 0 ". $tableName;
  309. system($com_backup);
  310. $taskID = $theTask_arr['ID'];
  311. $fileFormat = $theTask_arr['DataFileFormat'];
  312. $dirID_str = $theTask_arr['PlateID'];
  313. $tppID = $theTask_arr['RunTPP'];
  314. $frm_tppID_str = '';
  315. $search_engine_arr = array();
  316. $tmp_en_arr = explode(";", $theTask_arr['SearchEngines']);
  317. foreach($tmp_en_arr as $tmp_en){
  318. $tmp_set = explode("=", $tmp_en);
  319. array_push($search_engine_arr, $tmp_set[0]);
  320. }
  321. $SQL = "select ID from ".$tableName." where FolderID in(".$dirID_str.") and FileType='".$fileFormat."'";
  322. $results = mysql_query($SQL, $msManager_link);
  323. while($row = mysql_fetch_row($results)){
  324. $SQL ="INSERT INTO ".$resultTable." SET
  325. WellID='".$row['0']."',
  326. TaskID='".$taskID."',
  327. SearchEngines=";
  328. foreach($search_engine_arr as $tmp_en){
  329. $tmp_SQL = $SQL . "'$tmp_en'";
  330. if(mysql_query($tmp_SQL, $msManager_link)){
  331. //add records to tpp results table.
  332. if($tppID){
  333. $tmp_SQL = "INSERT INTO `$tableTppResults` set
  334. `WellID`='".$row['0']."',
  335. `TppTaskID`='".$tppID."',
  336. `SearchEngine`='".$tmp_en."'";
  337. mysql_query($tmp_SQL, $msManager_link);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. ?>