/Prohits/msManager/autoSearch/auto_search_table_shell.php
PHP | 376 lines | 286 code | 30 blank | 60 comment | 48 complexity | bc9b07ca27f27b64e2dbaab4a1b21dc6 MD5 | raw file
- <?
- /***********************************************************************
- Copyright 2010 Gingras and Tyers labs,
- Samuel Lunenfeld Research Institute, Mount Sinai Hospital.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- *************************************************************************/
- /************************************************************************
- date: 2006-05-08
- description:
- 1. a mass spec machine table name has to passed to this script
- 2. get the running task.
- 3. set spleep time, if start everyDay.
- 4. get new raw file to searchResults tasble, if is autoAddRawfiles
- 5. get all not searched raw files from searchResults table.
- 6. loop all files and send them to search engine if is not searched.
- 7. get searched resuts file.
- 8. shell > php auto_search_table_shell.php tableName taskID thisScriptURL sleepSec
- 9. check shell process
- shell > ps -o lstart PID
- 10.
- *************************************************************************/
- set_time_limit(0);
- ini_set("memory_limit","-1");
- //ini_set("default_socket_timeout", "1000");
- $tableName = '';
- $logfile = '../../logs/search.log';
- $theTask_arr = array();
- $searchEngine_arr = array();
- $searchEngines_parameter_arr = array();
- $theTaskID = 0;
- $raw_file_source_dir = '';
- $frm_theTaskID = '';
- $frm_theURL = '';
- $sleep_time = '';
- //change cwd to this directory
- chdir(dirname(__FILE__));
- include('../../config/conf.inc.php');
- include('./auto_search_gpm.inc.php');
- include('./auto_search_mascot.inc.php');
- include('../autoBackup/shell_functions.inc.php');
- include('../../common/mysqlDB_class2.php');
- require_once("../../common/HTTP/Request.php");
- require_once('../is_dir_file.inc.php');
- $php_command_location = PHP_PATH;
- //maybe it is remote connect prohits server---------
- $host = PROHITS_SERVER_IP;
- if(STORAGE_IP == PROHITS_SERVER_IP){
- $host = HOSTNAME;
- }
- $user = USERNAME;
- $pswd = DBPASSWORD;
- $msManager_link = mysql_connect("$host", $user, $pswd, true, 2 ) or fatalError("Unable to connect to mysql..." . mysql_error($msManager_link));
- mysql_select_db(MANAGER_DB, $msManager_link) or fatalError("Can not use '". MANAGER_DB . "' database". mysql_error($msManager_link));
- //if(defined('STORAGE_IP'))
- if(isset($_SERVER['argv']) and count($_SERVER['argv']) > 1){
- $tableName = $_SERVER['argv'][1];
- if(isset($_SERVER['argv'][2])){
- $frm_theTaskID = $_SERVER['argv'][2];
- $theURL = $_SERVER['argv'][3];
- $frm_theURL = str_replace("autoSearch/auto_search_table_shell.php","",$theURL) . "autoBackup/download_raw_file.php";
-
- if(isset($_SERVER['argv'][4])){
- $sleep_sec = $_SERVER['argv'][4];
- if(is_numeric($sleep_sec)){
- $msg = "It is ".@date("Y-m-d G:i:s").". Next $tableName (task $frm_theTaskID) process will be ". floor($sleep_sec/3600) . " hours later";
- writeLog($msg."\n");
- sleep($sleep_sec);
- }
- }
- }
- }else if(array_key_exists('REQUEST_METHOD', $_SERVER)){
- //processing search now sed by web ******************************************
- if( $_SERVER['REQUEST_METHOD'] == "POST"){
- $request_arr = $_POST;
- }else{
- $request_arr = $_GET;
- }
- foreach ($request_arr as $key => $value) {
- $$key=$value;
- }
- if(isset($SID) and isset($tableName) and check_permission($SID,$tableName)) {
- if(isset($kill)){
- system("kill $kill");
- writeLog("Table:$tableName task ID: $frm_theTaskID has been stopped.");
- exit;
- }
- //get running task
- $SQL = "SELECT ID, Schedule, ProcessID FROM ". $tableName ."SearchTasks where ";
- if($frm_theTaskID){
- $SQL .= "ID='$frm_theTaskID'";
- }else{
- $SQL .= "Status='Running' order by ID";
- }
- $result = mysql_query($SQL, $msManager_link);
- if($tmp_row = mysql_fetch_assoc($result) ){
- $tmp_taskID = $tmp_row['ID'];
- $tmp_schedule = $tmp_row['Schedule'];
- $tmp_ProcessID = $tmp_row['ProcessID'];
- if($tmp_ProcessID) system("kill $tmp_ProcessID");
- $theURL = "http://".$_SERVER["SERVER_ADDR"].":".$_SERVER["SERVER_PORT"].$_SERVER["PHP_SELF"];
- $com = "$php_command_location " . __FILE__ ." ".$tableName. " ".$tmp_taskID." ". $theURL;
- if(defined('DEBUG_SEARCH') and DEBUG_SEARCH){
- 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";
- echo "<font color=green>$com</font>";
- exit;
- }
- echo "Process ID:";
- $tmp_PID = system($com." > /dev/null & echo \$!");
- $SQL = "update ". $tableName ."SearchTasks set ProcessID='".$tmp_PID."', Status='Running' where ID='".$tmp_taskID."'";
- mysql_query($SQL, $msManager_link);
- }
- echo " auto-search is running in the background";
- }else{
- echo "no enough info. passed";
- }
- exit;
- //end web interface **********************************************
- }else{
- $msg = "running on the shell. but no table name passed. the process stopped";
- fatalError($msg, __LINE__);
- }
- //******************************************************************
- ///// shell script start from here /////////////////////////////////
- checkLogSize($logfile, 2000);
- $start_time = @date("Y-m-j G:i:s");
- $taskTable = $tableName . "SearchTasks";
- $resultTable = $tableName . "SearchResults";
- $tableTppTasks = $tableName . "tppTasks";
- $tableTppResults = $tableName . "tppResults";
- if(getPhpProcess_arr($tableName, $frm_theTaskID) > 1){
- echo "the previous request is running.";
- exit;
- }
- $raw_file_pattern = str_replace(" ", "", $RAW_FILES);
- $raw_file_pattern = str_replace(".", "\.", $raw_file_pattern);
- $raw_file_pattern = '/\.' . str_replace(",", '$|\.', $raw_file_pattern) . '$/i';
- check_manager_db_connection();
- $results = mysql_list_tables ( MANAGER_DB, $msManager_link );
- $tmp_num = 0;
- while($row = mysql_fetch_row($results)){
- if($row[0] == $tableName or $row[0] == $taskTable or $row[0] == $resultTable) $tmp_num++;
- }
- if($tmp_num != 3){
- $e_msg = error_handle('', $msManager_link);
- fatalError("$e_msg \r\n $tableName, $taskTable, $resultTable (3) tables should be in database ". MANAGER_DB, __LINE__);
- }
- $theTask_arr = get_running_task($frm_theTaskID);
- if($theTask_arr){
- $frm_theTaskID = $theTask_arr['ID'];
- $tmp_schedule = $theTask_arr['Schedule'];
- if($theTask_arr['AutoAddFile'] == 'Yes'){
- auto_add_files($theTask_arr);
- }
-
- $tmp_arr = explode(";", $theTask_arr['SearchEngines']);
- foreach($tmp_arr as $en_str){
- $the_a = explode("=", $en_str);
- array_push($searchEngine_arr, $the_a[0]);
- }
- $tmp_par_arr = explode("\n", $theTask_arr['Parameters']);
- foreach($tmp_par_arr as $par_str){
- $engine_par_arr = array();
- $the_p = explode("===", $par_str);
- $engine_par_arr = explode(";", $the_p[1]);
- $searchEngines_parameter_arr[$the_p[0]] = $engine_par_arr;
- }
-
- }else{
- writeLog("Table:$tableName no Running task. $start_time");
- exit;
- }
- //get all raw files they are not searched.
- $SQL = "SELECT S.WellID, S.SearchEngines, T.FileName, T.FolderID from ".$tableName." T, ".$resultTable." S
- where T.ID=S.WellID and TaskID='". $theTask_arr['ID'] . "' and (S.DataFiles is NULL or S.DataFiles='') order by S.WellID" ;
- $results = mysql_query($SQL, $msManager_link);
- //echo $SQL;exit;
- writeLog("\nTable:$tableName; Task ID: ". $theTask_arr['ID']. "; Start Time: $start_time; Raw data files: ". mysql_num_rows($results) );
- $SQL = "update $taskTable set StartTime=now() where ID='".$theTask_arr['ID']."'";
- mysql_query($SQL, $msManager_link);
- $folder_path_arr = array(); // array('dirID'=>path_str, ...);
- //$theTable_path = preg_replace("/\/$/", "", STORAGE_FOLDER) . "/" . $tableName. "/";
- while($row = mysql_fetch_row($results)){
- $tmp_WellID = $row[0];
- $tmp_Engine = $row['1'];
- $tmp_file = $row['2'];
- $tmp_FolderID = $row[3];
- $tmp_dir_path = getFileDirPath($tableName, $tmp_WellID, $tmp_FolderID);
- echo "$tmp_dir_path, $tmp_WellID, $tmp_file, $tmp_Engine";
- $tmp_converted_file = checkFileFormat($tmp_dir_path, $tmp_WellID, $tmp_file, $tmp_Engine);
-
-
- if($tmp_converted_file){
- $tmp_raw_file_path = $tmp_dir_path . $tmp_converted_file['fileName'];
- if(defined('DEBUG_SEARCH') and DEBUG_SEARCH){
- echo "file format checked: ". $tmp_raw_file_path .": $tmp_Engine \n";
- }
- if($tmp_Engine == 'Mascot'){
- searchMascot($tmp_raw_file_path, $tmp_WellID, $searchEngines_parameter_arr['Mascot'], $theTask_arr['LCQfilter'], $theTask_arr['ID'], $tmp_converted_file['type']);
- }else if($tmp_Engine == 'GPM'){
- searchGPM($tmp_raw_file_path, $tmp_WellID, $searchEngines_parameter_arr['GPM'], $theTask_arr['LCQfilter'], $theTask_arr['ID'], $tmp_converted_file['type']);
- }
- }
- }//end while loop
- //cehck if set to run TPP after the search task/////////////////////////////////////////////////
- if($theTask_arr['RunTPP']){
- $tpp_theURL = $theURL;
- //if(PROHITS_SERVER_IP != STORAGE_IP){
- // $tpp_theURL = str_replace(STORAGE_IP,PROHITS_SERVER_IP,$theURL);
- //}
- $tpp_theURL = str_replace("autoSearch/auto_search_table_shell.php","",$tpp_theURL) . "tppTask/tpp_task_shell.php";
- $tpp_theURL .='?tableName='.$tableName.'&tppTaskID='.$theTask_arr['RunTPP'];
-
- $handle = fopen($tpp_theURL, "r");
- $tppmsg = '';
- if($handle){
- while (!feof($handle)) {
- $tppmsg .= fgets($handle, 4096);
- }
- if(defined('DEBUG_SEARCH') and DEBUG_SEARCH){
- echo "response from: $tpp_theURL\n";
- echo $tppmsg;
- }
- fclose($handle);
- }else{
- $tppmsg = "Cannot open tpp URL $tpp_theURL";
- }
- writeLog($tppmsg);
- }
- ////////////////////////////////run next task ////////////////////////////////////////////////////////////
- if(isset($tmp_row['ProcessID'])) system("kill ".$tmp_row['ProcessID'] ." > /dev/null 2>&1 &");
- $tmp_row = array();
- $end_time = @date("Y-m-d G:i:s");
- writeLog("Table:$tableName; Task ID: ". $theTask_arr['ID']. "; End Time: $end_time");
- if( strpos($theTask_arr['Schedule'],"Every") === false){
- $SQL = "update $taskTable set Status='Finished' where ID='".$theTask_arr['ID']."'";
- mysql_query($SQL, $msManager_link);
-
- $tmp_row = get_running_task();
- }else{
- $tmp_row = get_running_task($frm_theTaskID);
- }
-
- if($tmp_row){
- $tmp_taskID = $tmp_row['ID'];
- $tmp_schedule = $tmp_row['Schedule'];
- $tmp_ProcessID = $tmp_row['ProcessID'];
- //Every;4
- $sch_arr = explode(';', $tmp_schedule);
- if($sch_arr[0] == 'Every' and isset($sch_arr[1])){
- $sleep_time = $sch_arr[1] * 3600;
- $sleep_time = " " . $sleep_time;
- }
- echo "Process ID:";
- $tmp_PID = system("$php_command_location " . __FILE__ ." ".$tableName. " ".$tmp_taskID." ". $theURL."$sleep_time > /dev/null & echo \$!");
- $SQL = "update ". $tableName ."SearchTasks set ProcessID='".$tmp_PID."', Status='Running' where ID='".$tmp_taskID."'";
- mysql_query($SQL, $msManager_link);
- }
- exit;
- //************************************************
- // return taske record array
- //************************************************
- function get_running_task($frm_theTaskID =''){
- global $msManager_link;
- global $taskTable;
- $SQL = "SELECT ID, PlateID, DataFileFormat, SearchEngines, Parameters, TaskName, LCQfilter, Schedule, StartTime,
- AutoAddFile, RunTPP, Status, ProcessID, UserID, ProjectID
- FROM ". $taskTable." where ";
- if($frm_theTaskID){
- $SQL .= "ID='$frm_theTaskID'";
- }else{
- $SQL .="Status='Running' or Status='Waiting' order by ID";
- }
- $result = mysql_query($SQL, $msManager_link);
- $theTask_arr = mysql_fetch_array($result);
- return $theTask_arr;
- }
- //
- //************************************************
- //
- //************************************************
- function check_manager_db_connection(){
- global $msManager_link;
- if(!mysql_ping($msManager_link)){
- mysql_close($msManager_link);
- $msManager_link = mysql_connect(PROHITS_SERVER_IP, USERNAME, DBPASSWORD, true, 2 ) or fatalError("Unable to connect to mysql..." . mysql_error($msManager_link));
- mysql_select_db(MANAGER_DB, $msManager_link) or fatalError("Can not use '". MANAGER_DB . "' database". mysql_error($msManager_link));
- }
- }
- function error_handle($msg = '', $db_link){
- if(mysql_errno($db_link) == "1062"){
- return '';
- }else{
- $msg .= "\nerror #" . mysql_errno($db_link);
- $msg .= "\nmysql error: " . mysql_error($db_link);
- $msg .= "\nScript Name: " . __FILE__;
- }
- return $msg;
- }
- //**********************************************
- // it will run the table backup first.
- //**********************************************
- function auto_add_files($theTask_arr){
- global $msManager_link;
- global $tableName ;
- global $resultTable;
- global $tableTppResults;
-
- writeLog("Get new created files from $tableName machine to Prohits.");
- $com_backup = PHP_PATH. " " .dirname(dirname(__FILE__))."/autoBackup/raw_backup_shell.php"." 0 ". $tableName;
- system($com_backup);
-
- $taskID = $theTask_arr['ID'];
- $fileFormat = $theTask_arr['DataFileFormat'];
- $dirID_str = $theTask_arr['PlateID'];
- $tppID = $theTask_arr['RunTPP'];
-
- $frm_tppID_str = '';
- $search_engine_arr = array();
- $tmp_en_arr = explode(";", $theTask_arr['SearchEngines']);
- foreach($tmp_en_arr as $tmp_en){
- $tmp_set = explode("=", $tmp_en);
- array_push($search_engine_arr, $tmp_set[0]);
- }
- $SQL = "select ID from ".$tableName." where FolderID in(".$dirID_str.") and FileType='".$fileFormat."'";
- $results = mysql_query($SQL, $msManager_link);
- while($row = mysql_fetch_row($results)){
- $SQL ="INSERT INTO ".$resultTable." SET
- WellID='".$row['0']."',
- TaskID='".$taskID."',
- SearchEngines=";
- foreach($search_engine_arr as $tmp_en){
- $tmp_SQL = $SQL . "'$tmp_en'";
- if(mysql_query($tmp_SQL, $msManager_link)){
- //add records to tpp results table.
- if($tppID){
- $tmp_SQL = "INSERT INTO `$tableTppResults` set
- `WellID`='".$row['0']."',
- `TppTaskID`='".$tppID."',
- `SearchEngine`='".$tmp_en."'";
- mysql_query($tmp_SQL, $msManager_link);
- }
- }
- }
- }
- }
- ?>