| |
---|
| | * |
---|
| | */ |
---|
| | 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'); |
---|
| | |
---|
| |
---|
| | $g_bu_states = null; |
---|
| | $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){ |
---|
| | |
---|
| | if($g_shutdown){ |
---|
| |
---|
| | function check_bu_sched(){ |
---|
| | |
---|
| | 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 ); |
---|
| | $idfound = 0; |
---|
| |
---|
| | ensure_cached_table(); |
---|
| | |
---|
| | 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" ); |
---|
| | |
---|
| |
---|
| | |
---|
| | // 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" ); |
---|
| | |
---|
| | print( "kickoff_backup for $buid\n" ); |
---|
| | debug_print( "childpid = $pid\n" ); |
---|
| | |
---|
| | debug_print( "kickoff_backup for $buid\n" ); |
---|
| | include_once __DIR__."/backup.php"; |
---|
| | $bu_result = ''; |
---|
| | global $g_data_dir; |
---|
| | $g_data_dir = __DIR__."/../data"; |
---|
| | |
---|
| | $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" ); |
---|
| | exit(0); |
---|
| |
---|
| | */ |
---|
| | function check_rpc_clients(){ |
---|
| | |
---|
| | global $msgArr; |
---|
| | |
---|
| | global $g_fork_cnt; |
---|
| | |
---|
| | $caller_id = 0; |
---|
| | |
---|
| | $obj_in = rpc_listen( $caller_id, $msgArr, false ); |
---|
| | $obj_out = null; |
---|
| |
---|
| | try{ |
---|
| | $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 ); |
---|
| | } |
---|
| | |
---|
| | //send msg back |
---|
| |
---|
| | |
---|
| | 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"){ |
---|
| | backupComplete( $obj_in[3], $obj_in[4] ); |
---|
| |
---|
| | |