Mercurial > epgrec.yaz
changeset 153:4099ae0393eb
mod: do-record.sh¥é¥Ã¥Ñ¤òÄɲáʤޤÀÅÓÃæ¡Ë
author | [email protected] <[email protected]> |
---|---|
date | Sun, 28 Mar 2010 22:56:00 +0900 |
parents | 784e3c82926e |
children | 64b60603d7df |
files | recorder.php |
diffstat | 1 files changed, 245 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/recorder.php Sun Mar 28 22:56:00 2010 +0900 @@ -0,0 +1,245 @@ +#!/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; // çµãã£ããã¨ã«ãã + 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; // çµãã£ããã¨ã«ãã + 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; // çµãã£ããã¨ã«ãã + throw new RecException("recorder:: é²ç»ã³ãã³ãã®å®è¡ã«å¤±æãã", EPGREC_ERROR ); + } + + // äºå®ããçããããªãçµäºæéãç¾å¨ã«æ¸ãæãã + + if( time() < $endtime ) { + $rrec->endtime = toDatetime( time() ); + } + + // å®äºãã©ã°ãç«ã¦ã¦ãã + $rrec->complete = '1'; + + // ã¡ãã£ã¨å¾ ã£ãæ¹ã確å®ã£ã½ã + 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 ); // ã¡ãã»ã¼ã¸ãã³ãã©éæ¾ +?>