diff --git a/html/svc/main.php b/html/svc/main.php index 8d7bf0f..2979813 100644 --- a/html/svc/main.php +++ b/html/svc/main.php @@ -40,10 +40,13 @@ */ include __DIR__."/../lib/sysVcom.php"; + function debug_print( $msg ){ - print( $msg ); + file_put_contents( __DIR__."/../data/rpc_op.log", $msg, FILE_APPEND); } +debug_print( date_format( date_create("now", new DateTimeZone("UTC")), 'Y-m-d H:i:s') . " started\n"); + //create the comms file touch('BU-commander'); @@ -71,6 +74,7 @@ $g_bu_table = array(); $g_bu_colIdxs = array(); getbu_list_all( $g_bu_table, $g_bu_colIdxs, 'order by BU_DATE desc, BU_TIME desc' ); +$g_fork_cnt=0; while(true){ @@ -119,6 +123,12 @@ global $g_bu_table; global $g_bu_colIdxs; + global $g_fork_cnt; + + if($g_fork_cnt>0){ + //we limit to a single fork for now + return; + } //get the first non null entry which is not running $keys = array_keys( $g_bu_table ); @@ -162,6 +172,7 @@ global $g_bu_table; global $g_bu_colIdxs; + global $g_fork_cnt; $isRunning = $g_bu_table[$buid][ $g_bu_colIdxs['BuRunning'] ]; if( !$bu_test && $isRunning==1) return array("e:Error", "Backup already running" ); @@ -177,25 +188,26 @@ // we should be able to use pcntl_fork() $pid = pcntl_fork(); if ( $pid == -1 ) { - print("fork failed"); + debug_print("fork failed"); return array("e:Error", "fork failed" ); } if ( $pid ) { // parent process - print( "parentpid = ". getmypid() . "\n" ); + debug_print( "parentpid = ". getmypid() . "\n" ); $g_bu_table[$buid][ $g_bu_colIdxs['BuRunning'] ] = 1; return array("ok:", "running" ); } else { // child process - + $g_fork_cnt++; + $g_bu_table = array(); //data here is unique to the parent $pid = getmypid(); - print( "childpid = $pid\n" ); + debug_print( "childpid = $pid\n" ); - print( "kickoff_backup for $buid\n" ); + debug_print( "kickoff_backup for $buid\n" ); include_once __DIR__."/backup.php"; $bu_result = ''; global $g_data_dir; @@ -203,7 +215,7 @@ $bu_result = run_backup_block( $buid, $bu_row, $bu_test ); - print( "backup ended for $buid, result = $bu_result\n" ); + debug_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 //print_r( $obj ); //print( "\n" ); @@ -222,7 +234,8 @@ function check_rpc_clients(){ global $msgArr; - + global $g_fork_cnt; + $caller_id = 0; $obj_in = rpc_listen( $caller_id, $msgArr, false ); @@ -234,7 +247,7 @@ $obj_out = handle_rpc_req( $obj_in ); }catch( Exception $e ){ - print( "Exception: " . $e->getMessage() ."\n" ); + debug_print( "Exception: " . $e->getMessage() ."\n" ); $obj_out = array( 'e:Exception', $e ); } @@ -244,11 +257,12 @@ if( $obj_in[0] === 'm:fork-complete' ){ //clean up child process - print("clean up fork child pid = {$obj_in[1]} \n"); + debug_print("clean up fork child pid = {$obj_in[1]} \n"); $status; pcntl_waitpid( $obj_in[1], $status ); - print("clean up done pid = {$obj_in[1]} \n"); + $g_fork_cnt--; + debug_print("clean up done pid = {$obj_in[1]} \n"); //finish any requirements for specific child processes (db updates etc) if( count($obj_in)>2 && $obj_in[2]="backup-complete"){