comparison 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
comparison
equal deleted inserted replaced
152:784e3c82926e 153:4099ae0393eb
1 #!/usr/bin/php
2 <?php
3 $script_path = dirname( __FILE__ );
4 chdir( $script_path );
5 include_once( $script_path . '/config.php');
6 include_once( INSTALL_PATH . "/DBRecord.class.php" );
7 include_once( INSTALL_PATH . "/Settings.class.php" );
8 include_once( INSTALL_PATH . "/recLog.inc.php" );
9 include_once( INSTALL_PATH . "/reclib.php" );
10
11 // 緇剛
12
13 if( ! defined( RECORDER_CMD ) ) {
14 define( "RECORDER_CMD", INSTALL_PATH . "/recorder.php" );
15 }
16
17
18 // 潟潟違<祉若後篆
19
20 function epgrec_get_message() {
21 global $msgh_r, $reserve_id;
22
23 $r = msg_receive($msgh_r, (int)$reserve_id , $msgtype, 1024, $message, TRUE, MSG_IPC_NOWAIT | MSG_NOERROR);
24 if( $r ) return $message;
25
26 return null;
27 }
28
29 // <祉若檎篆
30
31 function epgrec_send_message( $msg ) {
32 global $msgh_w, $reserve_id;
33
34 msg_send( $msgh_w, (int)$reserve_id, $msg );
35 sleep(1); // 御篆<緇
36 }
37
38
39 function epgrec_exec( $cmd ) {
40 $descspec = array(
41 0 => array( 'file','/dev/null','r' ),
42 1 => array( 'file','/dev/null','w' ),
43 2 => array( 'file','/dev/null','w' ),
44 );
45 $p = proc_open( $cmd, $descspec, $pipes );
46 if( is_resource( $p ) ) return $p;
47
48 return false;
49 }
50
51 // 絎祉鴻潟絖祉鴻pid鴻菴
52 // 鴻鐚
53 //
54 function epgrec_childproc( $p )
55 {
56 $st = proc_get_status( $p );
57 $cpids = epgrec_childproc( $st['pid'] );
58
59 // ps 絎茵
60 $d = array(
61 0 => array( 'file','/dev/null','r' ),
62 1 => array( 'pipe','w' ),
63 2 => array( 'file','/dev/null','w' ),
64 );
65
66 $ps = proc_open( "/bin/ps -o pid,ppid -ax" , $d, $pipes );
67 do {
68 $st = proc_get_status( $ps );
69 }while( $st['running'] );
70
71 // 罔羣阪茯
72 $cpids = array();
73 while( ! feof( $pipes[1] ) ) {
74 $line = fgets( $pipes[1] );
75 $pids = preg_split( "/[\s]+/", $line );
76 if( $pids[1] == $ppid ) {
77 array_push( $cpids, $pids[0] );
78 }
79 }
80 fclose( $pipes[1] );
81 proc_close( $ps );
82
83 return $cpids;
84 }
85
86 // 絎祉鴻潟絖祉鴻腟篋
87
88 function epgrec_termproc( $p )
89 {
90 $cpids = epgrec_childproc( $p );
91
92 @proc_terminate( $p );
93 sleep(1);
94 @proc_terminate( $p ); // 2綺
95
96 foreach( $cpids as $cpid ) {
97 @posix_kill( $cpid, SIGTERM ); // sigterm
98 usleep(100);
99 @posix_kill( $cpid, SIGKILL ); // sigkill
100 }
101
102 return true; // 篆荐若с
103 }
104
105 ////// 膩
106
107 $settings = Settings::factory();
108 $reserve_id = $argv[1];
109 $msgh_r = null; // 篆∞<祉若吾潟
110 $msgh_w = null; // 篆∞<祉若吾潟
111
112 // <祉若吾潟緇
113 $ipc_key = ftok( RECORDER_CMD, "R" );
114 $msgh_r = msg_get_queue( $ipc_key );
115
116 $ipc_key = ftok( RECORDER_CMD, "W" );
117 $msgh_w = msg_get_queue( $ipc_key );
118
119 try{
120 $rrec = new DBRecord( RESERVE_TBL, "id" , $reserve_id );
121
122 // 祉緇
123 $starttime = toTimestamp($rrec->starttime);
124 $endtime = toTimestamp($rrec->endtime);
125
126 if( time() > $starttime ) {
127 // サ牙私膣
128 $rrec->complete = 1; // 腟c
129 throw new RecException("recorder:: サ牙私膣絎茵", EPGREC_ERROR );
130 }
131 reclog("recorder:: 牙ID".$rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉吾с紮" );
132
133 // 牙脂紮障у
134 while( time() < $starttime ) {
135 if( ($message = epgrec_get_message() ) != null ) {
136 switch( $message ) {
137 case "terminate": // 腟篋腓
138 epgrec_send_message("success");
139 $rrec->complete = 1; // 腟c
140 throw new RecException("recorder:: 牙ID".$rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉筝" );
141 break;
142
143 case "stat":
144 epgrec_send_message("alive");
145 break;
146
147 default:
148 break;
149 }
150 }
151 usleep( 50 * 1000 ); // 50腱緇
152 }
153
154 // 牙脂紮
155
156 $proch = false;
157 if( ( $proch = epgrec_exec(DO_RECORD) ) !== false ) {
158 // 牙糸篋緇
159 $rec_cont = true;
160 while( $rec_cont ){
161 $st = proc_get_status($proch);
162 if(! $st['running'] ) $rec_cont = false; // 牙糸篋
163
164 if( ($message = epgrec_get_message() ) != null ) {
165 switch( $message ) {
166 case "terminate": // 腟篋腓
167 if( epgrec_termproc( $proch ) == false ) {
168 epgrec_send_message("error");
169 reclog( "牙祉潟潟罩≪с障", EPGREC_WARN );
170 }
171 else {
172 epgrec_send_message("success");
173 reclog("recorder:: 牙ID".$rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉筝" );
174 }
175 break;
176
177 case "stat":
178 epgrec_send_message("alive");
179 break;
180
181 default:
182 break;
183 }
184 }
185 sleep(1);
186 }
187 proc_close( $proch );
188 $proch = false;
189 }
190 else {
191 $rrec->complete = 1; // 腟c
192 throw new RecException("recorder:: 牙祉潟潟絎茵紊掩", EPGREC_ERROR );
193 }
194
195 // 篋絎腟篋憜吾
196
197 if( time() < $endtime ) {
198 $rrec->endtime = toDatetime( time() );
199 }
200
201 // 絎篋違腴
202 $rrec->complete = '1';
203
204 // <c緇c鴻腆阪c純
205 sleep(15);
206 @exec("sync");
207
208 if( file_exists( INSTALL_PATH .$settings->spool . "/". $rrec->path ) ) {
209 // 篋膣絎篋
210 reclog( "recorder:: 篋膣ID". $rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙紫篋" );
211
212 // 泣若篏
213 if( $settings->use_thumbs == 1 ) {
214 $gen_thumbnail = INSTALL_PATH."/gen-thumbnail.sh";
215 if( defined("GEN_THUMBNAIL") )
216 $gen_thumbnail = GEN_THUMBNAIL;
217 @exec($gen_thumbnail);
218 }
219
220 if( $settings->mediatomb_update == 1 ) {
221 $dbh = mysql_connect( $settings->db_host, $settings->db_user, $settings->db_pass );
222 if( $dbh !== false ) {
223 $sqlstr = "use ".$settings->db_name;
224 @mysql_query( $sqlstr );
225 // ャ
226 $sqlstr = "set NAME utf8";
227 @mysql_query( $sqlstr );
228 $sqlstr = "update mt_cds_object set metadata='dc:description=".mysql_real_escape_string($rrec->description)."&epgrec:id=".$reserve_id."' where dc_title='".$rrec->path."'";
229 @mysql_query( $sqlstr );
230 $sqlstr = "update mt_cds_object set dc_title='".mysql_real_escape_string($rrec->title)."(".date("Y/m/d").")' where dc_title='".$rrec->path."'";
231 @mysql_query( $sqlstr );
232 }
233 }
234 }
235 else { // 篋膣紊掩
236 reclog( "recomplete:: 篋膣ID". $rrec->id .":".$rrec->type.$rrec->channel.$rrec->title."牙祉紊掩罔≧", EPGREC_ERROR );
237 }
238 }
239 catch( Exception $e ) {
240 reclog( "recorder:: ".$e->getMessage(), $e->getLevel() );
241 }
242
243 msg_remove_queue( $msgh_r ); // <祉若吾潟
244 msg_remove_queue( $msgh_w ); // <祉若吾潟
245 ?>