view recorder.php @ 153:4099ae0393eb

mod: do-record.shラッパを追加(まだ途中)
author epgrec@park.mda.or.jp <yoneda@recorder.localnet.mda.or.jp>
date Sun, 28 Mar 2010 22:56:00 +0900
parents
children 84220382fea8
line wrap: on
line source

#!/usr/bin/php
<?php
$script_path = dirname( __FILE__ );
chdir( $script_path );
include_once( $script_path . '/config.php');
include_once( INSTALL_PATH . "/DBRecord.class.php" );
include_once( INSTALL_PATH . "/Settings.class.php" );
include_once( INSTALL_PATH . "/recLog.inc.php" );
include_once( INSTALL_PATH . "/reclib.php" );

// 緇剛

if( ! defined( RECORDER_CMD ) ) {
	define( "RECORDER_CMD", INSTALL_PATH . "/recorder.php" );
}


// 潟潟違<祉若後篆

function epgrec_get_message() {
	global $msgh_r, $reserve_id;
	
	$r = msg_receive($msgh_r, (int)$reserve_id , $msgtype, 1024, $message, TRUE, MSG_IPC_NOWAIT | MSG_NOERROR);
	if( $r ) return $message;
	
	return null;
}

// <祉若檎篆

function epgrec_send_message( $msg ) {
	global $msgh_w, $reserve_id;
	
	msg_send( $msgh_w, (int)$reserve_id, $msg );
	sleep(1);	// 御篆<緇
}


function epgrec_exec( $cmd ) {
	$descspec = array(
                        0 => array( 'file','/dev/null','r' ),
                        1 => array( 'file','/dev/null','w' ),
                        2 => array( 'file','/dev/null','w' ),
	);
	$p = proc_open( $cmd, $descspec, $pipes );
	if( is_resource( $p ) ) return $p;
	
	return false;
}

// 絎祉鴻潟絖祉鴻pid鴻菴
// 鴻鐚
//
function epgrec_childproc( $p )
{
	$st = proc_get_status( $p );
	$cpids = epgrec_childproc( $st['pid'] );
	
	// ps 絎茵
	$d = array(
			0 => array( 'file','/dev/null','r' ),
			1 => array( 'pipe','w' ),
			2 => array( 'file','/dev/null','w' ),
	);
	
	$ps = proc_open( "/bin/ps -o pid,ppid -ax" , $d, $pipes );
	do {
		$st = proc_get_status( $ps );
	}while( $st['running'] );
	
	// 罔羣阪茯
	$cpids = array();
	while( ! feof( $pipes[1] ) ) {
		$line = fgets( $pipes[1] );
		$pids = preg_split( "/[\s]+/", $line );
		if( $pids[1] == $ppid ) {
			array_push( $cpids, $pids[0] );
		}
	}
	fclose( $pipes[1] );
	proc_close( $ps );
	
	return $cpids;
}

// 絎祉鴻潟絖祉鴻腟篋

function epgrec_termproc( $p )
{
	$cpids = epgrec_childproc( $p );
	
	@proc_terminate( $p );
	sleep(1);
	@proc_terminate( $p );	// 2綺
	
	foreach( $cpids as $cpid ) {
		@posix_kill( $cpid, SIGTERM );	// sigterm
		usleep(100);
		@posix_kill( $cpid, SIGKILL );	// sigkill
	}
	
	return true;	// 篆荐若с
}

////// 膩

$settings = Settings::factory();
$reserve_id = $argv[1];
$msgh_r = null;		// 篆∞<祉若吾潟
$msgh_w = null;		// 篆∞<祉若吾潟

// <祉若吾潟緇
$ipc_key = ftok( RECORDER_CMD, "R" );
$msgh_r = msg_get_queue( $ipc_key );

$ipc_key = ftok( RECORDER_CMD, "W" );
$msgh_w = msg_get_queue( $ipc_key );

try{
	$rrec = new DBRecord( RESERVE_TBL, "id" , $reserve_id );
	
	// 祉緇
	$starttime = toTimestamp($rrec->starttime);
	$endtime   = toTimestamp($rrec->endtime);
	
	if( time() > $starttime ) {
		// サ牙私膣
		$rrec->complete = 1;	// 腟c
		throw new RecException("recorder:: サ牙私膣絎茵", EPGREC_ERROR );
	}
	reclog("recorder:: 牙ID".$rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉吾с紮" );
	
	// 牙脂紮障у
	while( time() < $starttime ) {
		if( ($message = epgrec_get_message() ) != null ) {
			switch( $message ) {
				case "terminate":			// 腟篋腓
					epgrec_send_message("success");
					$rrec->complete = 1;	// 腟c
					throw new RecException("recorder:: 牙ID".$rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉筝" );
					break;
				
				case "stat":
					epgrec_send_message("alive");
					break;
					
				default:
					break;
			}
		}
		usleep( 50 * 1000 );				// 50腱緇
	}
	
	// 牙脂紮
	
	$proch = false;
	if( ( $proch = epgrec_exec(DO_RECORD) ) !== false ) {
		// 牙糸篋緇
		$rec_cont = true;
		while( $rec_cont ){
			$st = proc_get_status($proch);
			if(! $st['running'] ) $rec_cont = false;    // 牙糸篋
			
			if( ($message = epgrec_get_message() ) != null ) {
				switch( $message ) {
					case "terminate":	// 腟篋腓
						if( epgrec_termproc( $proch ) == false ) {
							epgrec_send_message("error");
							reclog( "牙祉潟潟罩≪с障", EPGREC_WARN );
						}
						else {
							epgrec_send_message("success");
							reclog("recorder:: 牙ID".$rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉筝" );
						}
						break;
					
					case "stat":
						epgrec_send_message("alive");
						break;
				
					default:
						break;
				}
			}
			sleep(1);
		}
		proc_close( $proch );
		$proch = false;
	}
	else {
		$rrec->complete = 1;	// 腟c
		throw new RecException("recorder:: 牙祉潟潟絎茵紊掩", EPGREC_ERROR );
	}
	
	// 篋絎腟篋憜吾
	
	if( time() < $endtime ) {
		$rrec->endtime = toDatetime( time() );
	}
	
	// 絎篋違腴
	$rrec->complete = '1';
	
	// <c緇c鴻腆阪c純
	sleep(15);
	@exec("sync");
	
	if( file_exists( INSTALL_PATH .$settings->spool . "/". $rrec->path ) ) {
		// 篋膣絎篋
		reclog( "recorder:: 篋膣ID". $rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙紫篋" );
	
		// 泣若篏
		if( $settings->use_thumbs == 1 ) {
			$gen_thumbnail = INSTALL_PATH."/gen-thumbnail.sh";
			if( defined("GEN_THUMBNAIL") ) 
				$gen_thumbnail = GEN_THUMBNAIL;
			@exec($gen_thumbnail);
		}
		
		if( $settings->mediatomb_update == 1 ) {
			$dbh = mysql_connect( $settings->db_host, $settings->db_user, $settings->db_pass );
			if( $dbh !== false ) {
				$sqlstr = "use ".$settings->db_name;
				@mysql_query( $sqlstr );
				// ャ
				$sqlstr = "set NAME utf8";
				@mysql_query( $sqlstr );
				$sqlstr = "update mt_cds_object set metadata='dc:description=".mysql_real_escape_string($rrec->description)."&epgrec:id=".$reserve_id."' where dc_title='".$rrec->path."'";
				@mysql_query( $sqlstr );
				$sqlstr = "update mt_cds_object set dc_title='".mysql_real_escape_string($rrec->title)."(".date("Y/m/d").")' where dc_title='".$rrec->path."'";
				@mysql_query( $sqlstr );
			}
		}
	}
	else {	// 篋膣紊掩
		reclog( "recomplete:: 篋膣ID". $rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉紊掩罔≧", EPGREC_ERROR );
	}
}
catch( Exception $e ) {
	reclog( "recorder:: ".$e->getMessage(), $e->getLevel() );
}

msg_remove_queue( $msgh_r );	// <祉若吾潟
msg_remove_queue( $msgh_w );	// <祉若吾潟
?>