diff --git a/html/bin/run-bu-by-id.php b/html/bin/run-bu-by-id.php deleted file mode 100644 index 7d93bf6..0000000 --- a/html/bin/run-bu-by-id.php +++ /dev/null @@ -1,26 +0,0 @@ - the name/path of this script -$id = $argv[1]; -$isTest = $argv[2]; - -$str_err = run_backup_block( $id, $isTest=='1' ); - -/* - * Since this script is used from within the shell, we loose return values. Therefore, any error - * recieved from the previous call will be echoed to the console. It shall have the string - * "Error:" prepended. - * - * The shell exec will pickup the echoed output and test for the "Error" string. - * - * The output is silent if all went well. - */ -if($str_err!='') echo "Error: $str_err"; - -?> diff --git a/html/inc/btn_run_bu.php b/html/inc/btn_run_bu.php deleted file mode 100644 index beec13f..0000000 --- a/html/inc/btn_run_bu.php +++ /dev/null @@ -1,59 +0,0 @@ -&1"; - - //error_log("btn_run_bu to execute cmd: $command"); - - if( exec( $command, $output, $result_code )===FALSE){ - error_log( "exec to php-cli failed - command: $command"); - return array('error', "exec to php-cli failed - command: $command" ); - } - - //error_log( print_r($output,true) ); - - //even though the script ran, it may have echoed errors to the console. We test here. - if( count($output)>0 ){ - if( substr( $output[0], 0, 6) == 'Error:' ){ - error_log( $output[0] ); - return array('error', $output[0] ); - } - } - - if($testrun!='false'){ - //a test run waits for the command to finish, so the output can be collected and sent back now - for($i=0;$i<2;$i++){ - $fn = __DIR__."/../data/".getFilename_out( $buid, $bIsTest, $i==0 ); - $output[] = file_get_contents($fn); - } - - return array_merge( array('done'), $output ); - } - - //error_log( "Backup kicked off for ID($buid): isTest=$isTest"); - - return array_merge( array('running'), $output ); -} - - -?> diff --git a/html/inc/bu-common.php b/html/inc/bu-common.php index de264ca..18dc25b 100644 --- a/html/inc/bu-common.php +++ b/html/inc/bu-common.php @@ -12,17 +12,32 @@ */ function get_bu_item( $id ){ - global $g_database_path; - $db = new SQLite3( __DIR__."/../$g_database_path", SQLITE3_OPEN_READONLY ); + if(!is_numeric($id) ) return null; + $id=(int)$id; + + [$table, $colIdxs] = rpc( 'f:getbu_list_all', "where BUID=$id" ); + + if( !array_key_exists( $id, $table ) ) return null; + $row = $table[$id]; + + return array( + $row[ $colIdxs['BUName']], + $row[ $colIdxs['Dir_Src']], + $row[ $colIdxs['Dir_Dest']], + $row[ $colIdxs['Files_Ex']], + $row[ $colIdxs['BuType']], + $row[ $colIdxs['BuRunning']], + $row[ $colIdxs['BuError']], + $row[ $colIdxs['LastRunDt']] + ); +} - $html_rows=''; - $results = $db->query("SELECT * FROM BULIST where BUID=$id"); - $row = $results->fetchArray(); - - if($row){ - return array( $row['BUName'], $row['Dir_Src'], $row['Dir_Dest'], $row['Files_Ex'], $row['BuType'], $row['BuRunning'], $row['BuError'], $row['LastRunDt'] ); - } - +/** + * Set the error string in the database. + */ +function set_bu_error( $buid, $str_errs ){ + [$err] = rpc( 'f:set_bu_error', $buid, $str_errs ); + return $err; } /** @@ -33,74 +48,13 @@ if($bu_test) return true; - global $g_database_path; - - print( __DIR__."/$g_database_path" ); - - $db = new SQLite3( __DIR__."/$g_database_path" ); - - $setit_int = $setit?1:0; - return $db->exec("update BULIST set BuRunning=$setit_int where BUID=$id"); + [$err] = rpc( 'f:set_bu_running', $id, (bool)$setit ); + return $err; } -/** - * Set the error string in the database. - */ -function set_bu_error( $buid, $str_errs ){ - global $g_database_path; - $db = new SQLite3( __DIR__."/$g_database_path" ); - if($str_errs==''){ - return $db->exec("update BULIST set BuError = NULL where BUID=$buid"); - } - $p = $db->prepare("update BULIST set BuError=:str_errs where BUID=:buid"); - $p->bindValue(':str_errs', $str_errs ); - $p->bindValue(':buid', $buid ); - $p->execute(); - - if( $db->lastErrorCode()!==0){ - return $db->lastErrorCode(); - } -} - - -function set_last_run_date( $buid, $str_dt ){ - global $g_database_path; - $db = new SQLite3( __DIR__."/$g_database_path" ); - $p = $db->prepare("update BULIST set LastRunDt=:str_dt where BUID=:buid"); - $p->bindValue(':str_dt', $str_dt ); - $p->bindValue(':buid', $buid ); - $p->execute(); - - if( $db->lastErrorCode()!==0){ - return $db->lastErrorCode(); - } -} - -function set_sched_date( $buid, $dt_new ){ +function run_backup_block( $buid, $row, $bu_test ){ - global $g_database_path; - - $db = new SQLite3( __DIR__."/$g_database_path" ); - - $p = $db->prepare("update BULIST set BU_DATE=:str_dt where BUID=:buid"); - $p->bindValue(':str_dt', $dt_new ); - $p->bindValue(':buid', $buid ); - $p->execute(); - - if( $db->lastErrorCode()!==0){ - return $db->lastErrorCode(); - } -} - - -/** - * Run the backup process for the given backup ID. - * - * Returns an error string on failure. - */ -function run_backup_block( $buid, $bu_test=true ){ - - $row = get_bu_item( $buid ); + //$row = get_bu_item( $buid ); if( !$bu_test && $row[5]==1){ return "Backup for item($buid) is already running\n"; @@ -123,10 +77,6 @@ } } - // NOTE: - // using pcntl_fork() was a waste of time because the parent always waited for the child to finish. - // so we rely on bash & - if($bu_type==1 ){ return run_backup_DB( $bu_test, $buid, $bu_src, $bu_dest ); } @@ -148,10 +98,10 @@ if($bu_test){ $flags = '-anv'; // n is the dry run indicator - }else{ + }else{ $flags = '-av'; set_bu_error( $buid, '' ); //clear any previous error - //set_last_run_date( $buid, gmdate("Y-m-d H:i:s") ); + //set_last_run_date( $buid, gmdate("Y-m-d H:i:s") ); } $fn_o = getFilename_out( $buid, $bu_test, false ); @@ -276,4 +226,33 @@ return "bu-$fn_type-$fn_stream-$buid.txt"; } +function set_last_run_date( $buid, $str_dt ){ + global $g_database_path; + $db = new SQLite3( __DIR__."/$g_database_path" ); + $p = $db->prepare("update BULIST set LastRunDt=:str_dt where BUID=:buid"); + $p->bindValue(':str_dt', $str_dt ); + $p->bindValue(':buid', $buid ); + $p->execute(); + + if( $db->lastErrorCode()!==0){ + return $db->lastErrorCode(); + } +} + +function set_sched_date( $buid, $dt_new ){ + + global $g_database_path; + + $db = new SQLite3( __DIR__."/$g_database_path" ); + + $p = $db->prepare("update BULIST set BU_DATE=:str_dt where BUID=:buid"); + $p->bindValue(':str_dt', $dt_new ); + $p->bindValue(':buid', $buid ); + $p->execute(); + + if( $db->lastErrorCode()!==0){ + return $db->lastErrorCode(); + } +} + ?> diff --git a/html/inc/bu_list_content.php b/html/inc/bu_list_content.php index d48f5e9..f3c23c1 100644 --- a/html/inc/bu_list_content.php +++ b/html/inc/bu_list_content.php @@ -5,114 +5,51 @@ * Provides a simple json array of the BU state info for the UI to poll changes. */ function getbu_list_state(){ - - global $g_database_path; - - $db = new SQLite3($g_database_path, SQLITE3_OPEN_READONLY ); - - error_log( '$g_database_path = ' . $g_database_path ); - - $bu_state = array(); - $results = $db->query('SELECT BUID, BuRunning FROM BULIST'); - while ($row = $results->fetchArray()) { - array_push( $bu_state, array( $row['BUID'], $row['BuRunning'] ) ); - } - return $bu_state; + return rpc( 'f:getbu_list_state' ); } /** - * Retrieve all database rows ordered by the next run time in ascending order. Note that the null date/times will - * be listed first. - */ -function getbu_list_all( &$allRows, &$colIdxs=null ){ - - global $g_database_path; - - $db = new SQLite3($g_database_path, SQLITE3_OPEN_READONLY ); - - if($colIdxs!==null){ - $i=0; - $colIdxs = array(); - $colIdxs['BUID'] = $i++; - $colIdxs['BUName'] = $i++; - $colIdxs['Dir_Src'] = $i++; - $colIdxs['Dir_Dest'] = $i++; - $colIdxs['Files_Ex'] = $i++; - $colIdxs['BuType'] = $i++; - $colIdxs['BuRunning'] = $i++; - $colIdxs['BU_DATE'] = $i++; - $colIdxs['BU_TIME'] = $i++; - $colIdxs['BU_REPEAT'] = $i++; - $colIdxs['BuError'] = $i++; - $colIdxs['LastRunDt'] = $i++; - } - - $html_rows=''; - $results = $db->query('SELECT BUID, BUName, Dir_Src, Dir_Dest, Files_Ex, BuType, BuRunning, BU_DATE, BU_TIME, BU_REPEAT, BuError, LastRunDt FROM BULIST order by BU_DATE desc, BU_TIME desc'); - while ($row = $results->fetchArray()) { - $allRows[ $row['BUID'] ] = array( - $row['BUID'], - $row['BUName'], - $row['Dir_Src'], - $row['Dir_Dest'], - $row['Files_Ex'], - $row['BuType'], - $row['BuRunning'], - $row['BU_DATE'], - $row['BU_TIME'], - $row['BU_REPEAT'], - $row['BuError'], - $row['LastRunDt'] - ); - } - -} - -/** * Returns the main backup list page. */ function getbu_list_content(){ - global $g_database_path; - - $db = new SQLite3($g_database_path, SQLITE3_OPEN_READONLY ); + //this is called destructuring + [$table, $colIdxs] = rpc( 'f:getbu_list_all', "order by BUID" ); - $html_rows=''; - $results = $db->query('SELECT * FROM BULIST'); - while ($row = $results->fetchArray()) { - //var_dump($row); - + $html_rows=''; + foreach( $table as $row ) { + $view_type = 'edit-file-bu'; - if($row['BuType']==1){ + if($row[ $colIdxs['BuType'] ]==1){ $view_type = 'edit-db-bu'; } $html_row = ''; - $html_row .= ''.$row['BUID'].''; - $html_row .= ""; // edit icon - $html_row .= ""; // trash icon - $html_row .= "{$row['BUName']}"; - $but = $row['BuType']==1?'DB':'F'; + $html_row .= ''.$row[ $colIdxs['BUID'] ].''; + $html_row .= ""; // edit icon + $html_row .= ""; // trash icon + $html_row .= "{$row[ $colIdxs['BUName']]}"; + $but = $row[$colIdxs['BuType']]==1?'DB':'F'; $html_row .= "$but"; - if($row['BuError']!=''){ - $html_row .= "!! {$row['LastRunDt']}"; + if($row[ $colIdxs['BuError'] ]!=''){ + $html_row .= "!! {$row[ $colIdxs['LastRunDt']]}"; }else{ - $html_row .= "{$row['LastRunDt']}"; + $html_row .= "{$row[ $colIdxs['LastRunDt']]}"; } - if( $row['BU_REPEAT']=='N' ){ - $html_row .= ""; + if( $row[ $colIdxs['BU_REPEAT'] ]=='N' ){ + $html_row .= ""; }else{ - $html_row .= " {$row['BU_REPEAT']}"; + $html_row .= " {$row[ $colIdxs['BU_REPEAT']]}"; } - if($row['BuRunning']){ + if($row[ $colIdxs['BuRunning']]){ $html_row .= ''; }else{ - $html_row .= ""; + $html_row .= ""; } $html_row .= ''; @@ -155,8 +92,7 @@ - -
+ EOD; diff --git a/html/inc/edit-sh-bu.php b/html/inc/edit-sh-bu.php index 89147ff..f737959 100644 --- a/html/inc/edit-sh-bu.php +++ b/html/inc/edit-sh-bu.php @@ -1,31 +1,33 @@ Not an integer $db_id"; + $db_id = (int)$db_id; - $db = new SQLite3( $g_database_path, SQLITE3_OPEN_READONLY ); - - $db_st = $db->prepare('SELECT * FROM BULIST where BUID=:buid'); - $db_st->bindValue(':buid', $db_id); - $results = $db_st->execute(); - $row = $results->fetchArray(); - if ( $row===FALSE ) { - return "Item $db_id not found"; - } + [$table, $colIdxs] = rpc( 'f:getbu_list_all', "where BUID=$db_id" ); - $bun = $row['BUName']; - $bus = $row['Dir_Src']; - $bu_dt = $row['BU_DATE']; + if(! array_key_exists( $db_id, $table) ) return "
Item not found $db_id
"; + + $row = $table[$db_id]; + + $bun = $row[ $colIdxs['BUName']]; + $bus = $row[ $colIdxs['Dir_Src']]; + $bu_dt = $row[ $colIdxs['BU_DATE']]; if($bu_dt==''){ $bu_dt = date("Y-m-d"); } - $bu_t = $row['BU_TIME']; + $bu_t = $row[ $colIdxs['BU_TIME']]; if($bu_t==''){ $bu_t = '00:00'; } - $bu_r = $row['BU_REPEAT']; + $bu_r = $row[ $colIdxs['BU_REPEAT']]; switch($bu_r){ case 'D': @@ -92,24 +94,5 @@ return str_replace('$db_id', $db_id, $content); } -function save_bu_schedule( $postVars ){ - - //error_log( $postVars['id'] ); - global $g_database_path; - - $db = new SQLite3( $g_database_path ); - - $db_st = $db->prepare('UPDATE BULIST SET BU_DATE=:bud, BU_TIME=:but, BU_REPEAT=:bur where BUID=:buid'); - $db_st->bindValue(':buid', $postVars['id'], SQLITE3_INTEGER ); - $db_st->bindValue(':bud', $postVars['sched_date'] ); - $db_st->bindValue(':but', $postVars['sched_time'] ); - $db_st->bindValue(':bur', $postVars['sched_period'] ); - $db_st->execute(); - - if( $db->lastErrorCode()!==0){ - return $db->lastErrorCode(); - } - -} ?> diff --git a/html/inc/new-file-bu.php b/html/inc/new-file-bu.php index fb58acb..5989ca8 100644 --- a/html/inc/new-file-bu.php +++ b/html/inc/new-file-bu.php @@ -130,43 +130,9 @@ * Both inserts as well as edits a single backup item. */ function save_bu_item( $postVars, $bu_id=0 ){ - - //error_log( "save_bu_item id = $bu_id, new_bu_dest = {$postVars['new_bu_dest']}" ); - global $g_database_path; - $db = new SQLite3( $g_database_path ); - $db_st = null; - if( $postVars['new_bu_is_dbType'] ){ - if($bu_id==0){ - $db_st = $db->prepare('INSERT INTO BULIST ( BUName, Dir_Src, Dir_Dest, BuType ) values( :bun, :bus, :bud, 1 )'); - }else{ - $db_st = $db->prepare('update BULIST set BUName=:bun, Dir_Src=:bus, Dir_Dest=:bud where BUID=:buid'); - } - }else{ - if($bu_id==0){ - $db_st = $db->prepare('INSERT INTO BULIST ( BUName, Dir_Src, Dir_Dest, Files_Ex, BuType ) values( :bun, :bus, :bud, :buex, 0 )'); - }else{ - $db_st = $db->prepare('update BULIST set BUName=:bun, Dir_Src=:bus, Dir_Dest=:bud, Files_Ex=:buex where BUID=:buid'); - } - } - - if($bu_id>0){ - $db_st->bindValue(':buid', $bu_id ); - } - - $db_st->bindValue(':bun', $postVars['new_bu_name'] ); - $db_st->bindValue(':bus', $postVars['new_bu_source'] ); - $db_st->bindValue(':bud', $postVars['new_bu_dest'] ); - if( !$postVars['new_bu_is_dbType'] ){ - $db_st->bindValue(':buex', $postVars['new_bu_exf'] ); - } - - $db_st->execute(); - - if( $db->lastErrorCode()!==0){ - return $db->lastErrorCode(); - } - + [$err] = rpc( 'f:save_bu_item', $postVars, $bu_id ); + return $err; } diff --git a/html/inc/pagemap.php b/html/inc/pagemap.php index 3ea3132..e77ab29 100644 --- a/html/inc/pagemap.php +++ b/html/inc/pagemap.php @@ -11,12 +11,7 @@ * The commands should all return true to indicate that they are valid calls from the client. */ function invokeCommand( $cmd, $postvars ){ - - if( $cmd=='btn_test_1' ){ - include_once __DIR__ . "/service_calls.php"; - sendHtmlOk_WithData( rpc_test( "Hello" ) ); - return true; - } + if( $cmd=='backup-list-state' ){ include_once __DIR__ . "/bu_list_content.php"; @@ -62,7 +57,16 @@ if( $cmd=='btn_run_bu' ){ - include_once __DIR__ . "/btn_run_bu.php"; + $res = rpc( 'f:kickoff-backup', $postvars['id'], $postvars['isTest'] ); + + //originally when this was handled by a bash script, the results would have been complex. The return + //values could have been error, done or running. + + //for the moment, we return running or error and handle the blocking case next + sendHtmlOk_WithData( $res ); + return true; + + /*include_once __DIR__ . "/btn_run_bu.php"; $res = btn_run_bu( $postvars['id'], $postvars['isTest'] ); if($res[0]=='running'){ @@ -81,9 +85,11 @@ sendHtmlError( $res ); return true; } - sendHtmlError( 'Unknown return value from btn_run_bu' ); return true; + */ + + } if( $cmd=='edit_shed_bu' ){ diff --git a/html/inc/service_calls.php b/html/inc/service_calls.php index 7b0d4e3..792f726 100644 --- a/html/inc/service_calls.php +++ b/html/inc/service_calls.php @@ -7,7 +7,7 @@ rpc_setGlobals( 'procName', __DIR__.'/../svc/BU-commander' ); rpc_setGlobals( 'max_chan', 8182 ); -function rpc_test( $msg ){ +function run_backup_block( $buid, $isTest ){ //error_log( "rpc_test in service_calls.php" ); return rpc( 'service_calls.php', 'From the web!', $msg ); diff --git a/html/post_handler.php b/html/post_handler.php index 3e06b43..22d0bf2 100644 --- a/html/post_handler.php +++ b/html/post_handler.php @@ -7,6 +7,11 @@ //error_log("POST: " . print_r($_POST, true) ); include_once __DIR__ . "/inc/paths.php"; + +include __DIR__."/lib/sysVcom.php"; +rpc_setGlobals( 'procName', 'svc/BU-commander' ); +rpc_setGlobals( 'max_chan', 8182 ); + include_once __DIR__ . "/inc/secure.php"; include_once __DIR__ . "/inc/common.php"; diff --git a/html/svc/db-reads.php b/html/svc/db-reads.php new file mode 100644 index 0000000..357f744 --- /dev/null +++ b/html/svc/db-reads.php @@ -0,0 +1,73 @@ +0) return; + + $html_rows=''; + $results = $db->query('SELECT BUID, BUName, Dir_Src, Dir_Dest, Files_Ex, BuType, BuRunning, BU_DATE, BU_TIME, BU_REPEAT, BuError, LastRunDt FROM BULIST '.$orderby); + while ($row = $results->fetchArray()) { + $allRows[ $row['BUID'] ] = array( + $row['BUID'], + $row['BUName'], + $row['Dir_Src'], + $row['Dir_Dest'], + $row['Files_Ex'], + $row['BuType'], + $row['BuRunning'], + $row['BU_DATE'], + $row['BU_TIME'], + $row['BU_REPEAT'], + $row['BuError'], + $row['LastRunDt'] + ); + } + +} + +/** + * Provides a simple json array of the BU state info for the UI to poll changes. + */ +function getbu_list_state(){ + + global $g_database_path; + + $db = new SQLite3($g_database_path, SQLITE3_OPEN_READONLY ); + + $bu_state = array(); + $results = $db->query('SELECT BUID, BuRunning FROM BULIST'); + while ($row = $results->fetchArray()) { + array_push( $bu_state, array( $row['BUID'], $row['BuRunning'] ) ); + } + return $bu_state; +} + + +?> diff --git a/html/svc/db-writes.php b/html/svc/db-writes.php new file mode 100644 index 0000000..45af47d --- /dev/null +++ b/html/svc/db-writes.php @@ -0,0 +1,101 @@ +prepare('UPDATE BULIST SET BU_DATE=:bud, BU_TIME=:but, BU_REPEAT=:bur where BUID=:buid'); + $db_st->bindValue(':buid', $postVars['id'], SQLITE3_INTEGER ); + $db_st->bindValue(':bud', $postVars['sched_date'] ); + $db_st->bindValue(':but', $postVars['sched_time'] ); + $db_st->bindValue(':bur', $postVars['sched_period'] ); + $db_st->execute(); + + if( $db->lastErrorCode()!==0){ + return $db->lastErrorCode(); + } + return 0; +} + +/** + * Both inserts as well as edits a single backup item. + */ +function save_bu_item( $postVars, $bu_id=0 ){ + + global $g_database_path; + + $db = new SQLite3( $g_database_path ); + $db_st = null; + if( $postVars['new_bu_is_dbType'] ){ + if($bu_id==0){ + $db_st = $db->prepare('INSERT INTO BULIST ( BUName, Dir_Src, Dir_Dest, BuType ) values( :bun, :bus, :bud, 1 )'); + }else{ + $db_st = $db->prepare('update BULIST set BUName=:bun, Dir_Src=:bus, Dir_Dest=:bud where BUID=:buid'); + } + }else{ + if($bu_id==0){ + $db_st = $db->prepare('INSERT INTO BULIST ( BUName, Dir_Src, Dir_Dest, Files_Ex, BuType ) values( :bun, :bus, :bud, :buex, 0 )'); + }else{ + $db_st = $db->prepare('update BULIST set BUName=:bun, Dir_Src=:bus, Dir_Dest=:bud, Files_Ex=:buex where BUID=:buid'); + } + } + + if($bu_id>0){ + $db_st->bindValue(':buid', $bu_id ); + } + + $db_st->bindValue(':bun', $postVars['new_bu_name'] ); + $db_st->bindValue(':bus', $postVars['new_bu_source'] ); + $db_st->bindValue(':bud', $postVars['new_bu_dest'] ); + if( !$postVars['new_bu_is_dbType'] ){ + $db_st->bindValue(':buex', $postVars['new_bu_exf'] ); + } + + $db_st->execute(); + + if( $db->lastErrorCode()!==0){ + return $db->lastErrorCode(); + } + +} + +/** + * Set the error string in the database. + */ +function set_bu_error_db( $buid, $str_errs ){ + + global $g_database_path; + + $db = new SQLite3( $g_database_path ); + if($str_errs==''){ + return $db->exec("update BULIST set BuError = NULL where BUID=$buid"); + } + $p = $db->prepare("update BULIST set BuError=:str_errs where BUID=:buid"); + $p->bindValue(':str_errs', $str_errs ); + $p->bindValue(':buid', $buid ); + $p->execute(); + + if( $db->lastErrorCode()!==0){ + return $db->lastErrorCode(); + } + return 0; +} + +/** + * Set (unset) the running datbase flag. Return true on success. + */ +function set_bu_running_db( $id, bool $setit ){ + + global $g_database_path; + + $db = new SQLite3( $g_database_path ); + + $setit_int = $setit?1:0; + return $db->exec("update BULIST set BuRunning=$setit_int where BUID=$id"); +} + + +?> diff --git a/html/svc/main.php b/html/svc/main.php index dce3873..063b0e4 100644 --- a/html/svc/main.php +++ b/html/svc/main.php @@ -26,12 +26,14 @@ include __DIR__."/../inc/paths.php"; $g_database_path = '../' . $g_database_path; -include_once "../inc/bu_list_content.php"; +include_once "db-reads.php"; +include_once "db-writes.php"; // get the latest data and cache it +$g_bu_states = null; $g_bu_table = array(); $g_bu_colIdxs = array(); -getbu_list_all( $g_bu_table, $g_bu_colIdxs ); +getbu_list_all( $g_bu_table, $g_bu_colIdxs, 'order by BU_DATE desc, BU_TIME desc' ); //print_r($g_bu_table); //print_r( $g_bu_table[18][ $g_bu_colIdxs['Dir_Dest'] ]); @@ -51,6 +53,18 @@ } /** + * Clears all cached data. This is called after any DB updates. It can be called at any time since loading is lazy. + */ +function clear_cached_data(){ + + global $g_bu_states; + global $g_bu_table; + + $g_bu_states = null; + $g_bu_table = array(); +} + +/** * Return true if the backup was kicked off */ function check_bu_sched(){ @@ -94,36 +108,40 @@ * true if it is run according to the $schedule info in the db * false if it has been manually initiated */ -function kickoff_backup( $buid, $scheduled = true ){ +function kickoff_backup( $buid, $scheduled = true, $bu_test = false ){ global $g_bu_table; global $g_bu_colIdxs; - + $isRunning = $g_bu_table[$buid][ $g_bu_colIdxs['BuRunning'] ]; + if( !$bu_test && $isRunning==1) return array("e:Error", "Backup already running" ); + + $i=0; + $bu_row[$i++] = $g_bu_table[$buid][ $g_bu_colIdxs['BUName'] ]; + $bu_row[$i++] = $g_bu_table[$buid][ $g_bu_colIdxs['Dir_Src'] ]; + $bu_row[$i++] = $g_bu_table[$buid][ $g_bu_colIdxs['Dir_Dest'] ]; + $bu_row[$i++] = $g_bu_table[$buid][ $g_bu_colIdxs['Files_Ex'] ]; + $bu_row[$i++] = $g_bu_table[$buid][ $g_bu_colIdxs['BuType'] ]; + $bu_row[$i++] = $isRunning; + // we should be able to use pcntl_fork() $pid = pcntl_fork(); if ( $pid == -1 ) { print("fork failed"); - return; + return array("e:Error", "fork failed" ); } - $bu_test = false; - $bu_type = $g_bu_table[$buid][ $g_bu_colIdxs['BuType'] ]; - $bu_src = $g_bu_table[$buid][ $g_bu_colIdxs['Dir_Src'] ]; - $bu_dest = $g_bu_table[$buid][ $g_bu_colIdxs['Dir_Dest'] ]; - $exFiles = $g_bu_table[$buid][ $g_bu_colIdxs['Files_Ex'] ]; - - - if ( $pid ) { // parent process print( "parentpid = ". getmypid() . "\n" ); $g_bu_table[$buid][ $g_bu_colIdxs['BuRunning'] ] = 1; - return; + return array("ok:", "running" ); } else { // child process + + $g_bu_table = array(); //data here is unique to the parent $pid = getmypid(); print( "childpid = $pid\n" ); @@ -133,11 +151,7 @@ global $g_data_dir; $g_data_dir = __DIR__."/../data"; - if($bu_type==1 ){ - $bu_result = run_backup_DB( $bu_test, $buid, $bu_src, $bu_dest ); - }else{ - $bu_result = run_backup_F( $bu_test, $buid, $bu_src, $bu_dest, $exFiles ); - } + $bu_result = run_backup_block( $buid, $bu_row, $bu_test ); print( "backup ended for $buid, result = $bu_result\n" ); $obj = rpc( 'm:fork-complete', $pid, "backup-complete", $buid, $scheduled ); //we use our rpc mechanism to notify parent @@ -159,28 +173,41 @@ $caller_id = 0; - $obj = rpc_listen( $caller_id, $msgArr, false ); + $obj_in = rpc_listen( $caller_id, $msgArr, false ); + $obj_out = null; if($caller_id==0){ //nothing to do return false; } - $obj = handle_rpc_req( $obj ); + try{ + $obj_out = handle_rpc_req( $obj_in ); + + }catch( Exception $e ){ + print( "Exception: " . $e->getMessage() ."\n" ); + $obj_out = array( 'e:Exception', $e ); + } //send msg back - rpc_reply( $caller_id, $obj ); + rpc_reply( $caller_id, $obj_out ); //done with the message unset( $msgArr[$caller_id] ); - if( $obj[0] == 'm:fork-complete' ){ + if( $obj_in[0] == 'm:fork-complete' ){ + if( count($obj_in)<5){ + print("m:fork-complete only has". count($obj_in) . " params, should be 5, skipping client cleanup\n"); + print_r( $obj_in ); + return true; + } + print("now wait for child pid = {$obj_in[1]} \n"); // this is required to clean up the child process $status; - pcntl_waitpid( $obj[1], $status ); + pcntl_waitpid( $obj_in[1], $status ); - if( $obj[2]="backup-complete"){ - backupComplete( $obj[3], $obj[4] ); + if( $obj_in[2]="backup-complete"){ + backupComplete( $obj_in[3], $obj_in[4] ); } } @@ -193,12 +220,13 @@ */ function backupComplete( $buid, $scheduled ){ - //reload the table data - client updates the error field global $g_bu_table; global $g_bu_colIdxs; - - $g_bu_table = array(); - getbu_list_all( $g_bu_table ); + + //reload the table data - client updates the error field (might move this) + clear_cached_data(); + $nothanks = null; + getbu_list_all( $g_bu_table, $nothanks, 'order by BU_DATE desc, BU_TIME desc' ); include_once __DIR__."/../inc/bu-common.php"; @@ -245,18 +273,70 @@ } + +/** + * Carries out the task requested from any RPC client. + * That client may be from the webserver or from a forked child or any other process. + */ function handle_rpc_req( $obj ){ + // The idea will be that the indicator before the colon will be handled generically in cases + // where it is a simple function call, i.e. 'f:' (provided it is a trusted source and/or has been previously checked) + + print_r( $obj ); switch( $obj[0] ){ case 'm:fork-complete': //send the same message back to the caller return $obj; + + case 'f:getbu_list_all': + //($table, $colIdxs) = rpc( 'f:getbu_list_all', "order by BUID" ); + $table = array(); + $colIdxs = array(); + getbu_list_all( $table, $colIdxs, $obj[1] ); + return array( $table, $colIdxs ); + + case 'f:getbu_list_state': + global $g_bu_states; + if($g_bu_states==null){ + $g_bu_states = getbu_list_state(); + } + return $g_bu_states; + + case 'f:save_bu_schedule': + $err = save_bu_schedule( $obj[1] ); + clear_cached_data(); + return array($err); + + case 'f:save_bu_item': + //[$err] = rpc( 'f:save_bu_item', $postVars, $bu_id ); + $err = save_bu_item( $obj[1], $obj[2] ); + clear_cached_data(); + return array($err); + + case 'f:set_bu_error': + print("setting backup error\n"); + $err = set_bu_error_db( $obj[1], $obj[2] ); + clear_cached_data(); + return array($err); + + case 'f:set_bu_running': + $err = set_bu_running_db( $obj[1], $obj[2] ); + clear_cached_data(); + return array($err); + + case 'f:kickoff-backup': + $buid = $obj[1]; + $isTest = $obj[2]==1; + //caller expects param1 to be one of error, done or running + //we've changed this slightly e:Error, ok:, done is not working yet + return kickoff_backup( $buid, false, $isTest ); + } - - //do something with the data + + array_unshift( $obj, "Unknown Command" ); print_r( $obj ); - array_unshift( $obj, "Greetings" ); return $obj; } diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..c65a851 --- /dev/null +++ b/notes.txt @@ -0,0 +1,42 @@ +3 Dec 2024 +---------- + + Reworking the project to use middleware as a services. (main.php) + + Clients will now use rpc to main.php to update the DB. + + I will work through all files to ensure that everything has been correctly updated. + + + In progress + ----------- + + + + bu-common.php + set_last_run_date, set_sched_date + + + Files left to update + -------------------- + + delete-file-bu.php + common.php + paths.php + + post_handler.php + service_calls.php - do we need this? + login.php + pagemap.php + secure.php + + db-site-insert-all.php + + + Files done + ---------- + + bu_list_content.php + edit-sh-bu.php + new-file-bu.php + service_calls.php - remove ?