kaltura /package/app/app/alpha/apps/kaltura/lib/batch/batchStatus.class.php

Language PHP Lines 429
MD5 Hash b750b896858c8ff60e1eb72b6aef9e1f Estimated Cost $8,972 (why?)
Repository https://bitbucket.org/pandaos/kaltura.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
class batchStatus
{
	public $batch_name = null;
	public $in_path = null;
	public $pending = 0;
	public $in_proc = 0;
	public $errors = null;
	public $last_log_time = null;
	public $oldest = null;
	public $newest = null; 
	public $succeedded_in_period;
	public $failed_in_period;
	
	private $running_os = '';
	
	private $pending_data = array();
	private $in_proc_data = array();
	
	private $db_job_stats = null;
	
	private static $proc_res = null;
	
	private static $batch_names = array();
	
	function __construct()
	{
		$this->running_os = $this->getPlatform();
	}
	
	private static function getBatchNameFromCommandLine ( $batch_cmd_line )
	{
		$batch_name = $batch_cmd_line;
		// crack and find the name of the process from the command line
		$tokens = explode ( " " , $batch_cmd_line );
		foreach ( $tokens as $token )
		{
			// if found the word batch - use it
			if ( stripos ( $token , "batch" ) !== FALSE ) 
			{
				$batch_name = $token;
				break;
			}
		}		
		
		return pathinfo( $batch_name , PATHINFO_FILENAME );
	}
	
	public static function cleanup ( )
	{
		foreach ( self::$batch_names as $batch_name )
		{
			self::batchEnd ( $batch_cmd_line );	
		}
	}
	
	public static function batchStart( $batch_cmd_line ) 
	{
		$batch_name = self::getBatchNameFromCommandLine( $batch_cmd_line );
		$path = myContentStorage::getFSContentRootPath() . "/batchwatch/_$batch_name";
		touch ( $path );
		
		self::$batch_names[] = $batch_name;
		register_shutdown_function(array("batchStatus", "cleanup"));
		return $path;
	}

	
	public static function batchEnd( $batch_cmd_line ) 
	{
		$batch_name = self::getBatchNameFromCommandLine( $batch_cmd_line );
		$path = myContentStorage::getFSContentRootPath() . "/batchwatch/_$batch_name";
		@unlink ( $path );
		return $path;
	}
	
	public static function getPlatform()
	{
		if (isset($_SERVER['SERVER_SOFTWARE']))
		{
			$os = (substr_count($_SERVER['SERVER_SOFTWARE'], 'Win32'))? 'win': 'nix';
		}
		else
		{
			$os = (substr_count(strtolower(@$_SERVER['OS']), 'windows'))? 'win': 'nix';
		}
		return $os;
	}
	
	public function getProcessStatusByPid($pid)
	{
		if ($this->running_os == 'win')
		{
		    $command = 'tasklist /FI "PID eq '.$pid.'"';
		}
		if ($this->running_os == 'nix')
		{
		    $command = 'ps aux | grep '.$pid.' |grep -v grep';
		}
		exec($command, $output, $error);
		$output = trim(implode("\n",$output));
		if ($error || !$output)
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	
	public static function getIndicatorFilesForBatch($batch_name)
	{
		$files_in_dir = scandir(myBatchBase::getBatchwatchPath());
		$batch_indicators = array();
		foreach($files_in_dir as $ind)
		{
		    if (substr_count($ind, $batch_name.'.running'))
		    {
			$batch_indicators[] = $ind;
		    }
		}
		return $batch_indicators;
	}
	
	public function getStatus()
	{
		/* status check according to running.pid filename and platform relevant command */
		$batch_name = self::getBatchNameFromCommandLine( $this->batch_name );
		$running = 0;
		$batch_indicators = self::getIndicatorFilesForBatch($batch_name);
		foreach($batch_indicators as $ind)
		{
			$filename_parts = explode('.', $ind);
			if ($this->getProcessStatusByPid($filename_parts[2]))
			{
				$running++;
			}
			else
			{
				// indicator exists, process doesn't
				//echo "i should unlink now: ".$ind;
				unlink(myBatchBase::getBatchwatchPath().$ind);
			}
		}
		return $running;
	}
	
	public function getName( $name_only = true )
	{
		if ( $name_only )  
			return self::getBatchNameFromCommandLine( $this->batch_name );
		return $this->batch_name;
	}

	public static function getPhpPath()
	{
		$kaltura_env_path = myContentStorage::getFSContentRootPath() .  '/kaltura/alpha/batch/kaltura_env.sh';
		$kaltura_env = file_get_contents($kaltura_env_path);
		$kaltura_env_lines = explode("\n", $kaltura_env);
		foreach($kaltura_env_lines as $line)
		{
			// find PHP_PATH and return the path
			if(substr_count($line, 'PHP_PATH'))
			{
				$parts = explode('=', $line);
				return $parts[1];
			}
		}
		throw("could not find php path");
	}
	public static function executeRunBatch ( $action , $batch_name )
	{
		$output = array();
		$error = "";
		$batch_runner_log = myContentStorage::getFSContentRootPath().'/logs/'.php_uname('n').'-batchRunner.log';
		//$command = myContentStorage::getFSContentRootPath() .  '/kaltura/alpha/batch/runBatch.sh ' . $action . " " . $batch_name;
		$runbatch_path = myContentStorage::getFSContentRootPath() .'/kaltura/alpha/batch/runBatch.php';
		if (self::getPlatform() == 'win')
		{
			$args = '"'.$runbatch_path.'" "'. $action .'" "'. $batch_name .'"';
			pclose(popen("start \"kaltura_batchrunner\" \"" . self::getPhpPath() . "\" " . $args, "r"));
		}
		else
		{
			$args = $runbatch_path.' '. $action .' '. $batch_name.' >> '.$batch_runner_log.' &';
			pclose(popen(self::getPhpPath() . " " . $args, "r"));
		}
//		exec($command, $output, $error);
	}
	
	/*
	 * When creating a batchWatch command do 2 things:
	 * 	1. create the batchwatch/ file for the batchWatch to handle
	 *  2. execute the same command line the batchwatch would only in the scope of the web
	 * 
	 * the 2 together will make sure the processes will really stop/start/restart
	 */
	private function createBatchwatchCmd ( $cmd )
	{
		// use this with no prefix
		$batch_name = self::getBatchNameFromCommandLine( $this->batch_name );
		$path = myContentStorage::getFSContentRootPath() . "/batchwatch/$batch_name";

		self::executeRunBatch ( $cmd , $batch_name );

		if ( $cmd == "stop")
		{
			$path = batchStatus::batchEnd( $batch_name );
		}
						
		return $path;		
	}
	
	public function start()
	{
		if (!$this->getStatus())
		{
			return $this->createBatchwatchCmd( "start" );
		}
		return 1;
	}
	
	public function stop()
	{
		if($this->getStatus())
		{
			return $this->createBatchwatchCmd( "stop" );
		}
		return 1;
	}
	
	public function restart()
	{
		return $this->createBatchwatchCmd( "restart" );
	}
	
	public function addToPending ( $title , $count )
	{
		if ( ! $count ) $count = 0;
		$this->pending_data[$title] = $count ;
		$this->pending += $count ;
	}

	public function getPendingData()
	{
		if ( $this->pending_data )
			return $this->pending_data;
		return null;	
	}
	
	public function addToInProc ( $title , $count )
	{
		if ( ! $count ) $count = 0;
		$this->in_proc_data[$title] = $count ;
		$this->in_proc += $count ;
	}
	
	public function getInProcData()
	{
		if ( $this->in_proc_data ) 
			return $this->in_proc_data;
		return null;
	}
	
	public function getLogData ( $log_name )
	{
		$file_name = null;
		$log_timestamp = "";
		$log_size = "0";
	
		if ( $log_name )
		{
			$log_dir = myContentStorage::getFSContentRootPath() . "/logs/";
			$pat = $log_dir . "/*$log_name*";
			$possible_log_files = glob ( $pat );

			// iterate through all the files - the last (and most updated)  will be taken
			// TODO - the above didn't work - stopped at the first log - SHOUD FIX
			foreach ( $possible_log_files as $file )
			{
				$file_name = realpath ( $file );
				clearstatcache();//true , $file_name );				
				$log_timestamp = filemtime ( $file_name );
				$log_size = filesize( $file_name );
				break;
			}
		}	
		
		if ( ! $file_name ) $file_name =  $log_name;
		
		return array ( $file_name , $log_timestamp , $log_size );
	}	
	
	public function getDiskStatsCount ( $log_name , $dir , $pattern = null )
	{
//print ( "getDiskStatsCount ( $log_name , $dir , $pattern = null )<br>")	;	
		$oldest = null;
		$newest = 0;
		$path = realpath ( $dir  ) . ( $pattern ? "/$pattern" : "/*" );
		$files = glob($path);
		$count = count ( $files );
		return $count;
	}
	
	public function getDiskStats ( $log_name , $dir , $pattern = null )
	{
		$oldest = null;
		$newest = 0;
		$path = realpath ( $dir  ) . ( $pattern ? "/$pattern" : "/*" );
		$files = glob($path);
		$count = count ( $files );
		foreach ( $files as $file )
		{
			$timestamp =  filemtime ( $file );
			if ( $oldest == null || $timestamp < $oldest  ) $oldest = $timestamp;
			if ( $timestamp > $newest ) $newest = $timestamp;
		}

		if ( $oldest > $newest ) $oldest = $newest;
		return array ( 
			"service_name" => $log_name ,  // all log_names are called after their services
			"path" => $path , 
			"count" => $count , 
			"oldest" => $oldest , 
			"newest" => $newest , 
		 );
	}	

	private function getJobStats ( $hours_ago = 24 )
	{
		$connection = Propel::getConnection();
		$from_date = date("Y-m-d H:i:s", time()- $hours_ago * 3600 ); 
		// don't fetch status 7=aborted
		// need type 1=import, 3=flatten, 4=bulkupload and 6=download
		// select status = 5 as well , but don't include in problematic count
	    $query = "select job_type , status , UNIX_TIMESTAMP(min(created_at)) as oldest , UNIX_TIMESTAMP(max(created_at)) as newest , count(1) as cnt " .
	    	"from batch_job where status in (0,1,2,3,4,6 , 5) and job_type in (1,3,4,6) and created_at>\"$from_date\" " . 
	    	"group by job_type,status";
		
		
		$statement = $connection->prepareStatement($query);
		$resultset = $statement->executeQuery();	

		$db_job_stats = array();
		while ($resultset->next())
	    {
	    	$job_type = $resultset->getInt('job_type');
	    	$status = $resultset->getInt('status');
//	    	$oldest = $resultset->getTimestamp('oldest');
//	    	$newest = $resultset->getTimestamp('newest');
			$oldest = $resultset->getInt('oldest');
	    	$newest = $resultset->getInt('newest');
	    	$count = $resultset->getInt('cnt');

	    	if ( !isset ( $db_job_stats[$job_type] ) )
	    	{
	    		$job_type_data = array();
	    	}
	    	else
	    	{
	    		$job_type_data =  $db_job_stats[$job_type];
	    	}
	    	
	    	// foreach job_type - creaet an array per status
	    	$job_type_data [$status] = array ( "oldest" => $oldest , "newest" => $newest , "count" => $count ) ;
	    	$db_job_stats[$job_type] = $job_type_data;
	    }
	    
		return $db_job_stats;
				
	}
		
	public function getDbStats ( $log_name , $job_type , $successful_stats_enum = BatchJob::BATCHJOB_STATUS_FINISHED , 
		$failed_stats_enum = BatchJob::BATCHJOB_STATUS_FAILED )
	{
		if ( $this->db_job_stats == null )
		{
			  $this->db_job_stats = $this->getJobStats ( 2400 );
		}
		
		$oldest = null;
		$newest = 0;
		$count = 0;
		$stats_for_job = @$this->db_job_stats[$job_type];
		$successful_stats = null;
		$failed_stats = null;
		if ( $stats_for_job )
		{
			foreach ( $stats_for_job as $status => $stats_for_job_per_status )
			{
				if ( $status == $successful_stats_enum )
				{
					 $successful_stats = $stats_for_job_per_status;
				}
				elseif ( $status == $failed_stats_enum )
				{
					$failed_stats = $stats_for_job_per_status;
				}
				else
				{
					if ( $oldest == null || $stats_for_job_per_status["oldest"] < $oldest  ) 
						$oldest = $stats_for_job_per_status["oldest"];
					if ( $stats_for_job_per_status["newest"] > $newest ) $newest = $stats_for_job_per_status["newest"];
					$count += $stats_for_job_per_status["count"];
				}
			}
		}

		list ( $file_name , $log_timestamp , $log_size ) = $this->getLogData( $log_name );
		
		if ( $oldest > $newest ) $oldest = $newest;
		return array ( 
			"service_name" => $log_name ,  // all log_names are called after their services
			"path" => null , 
			"count" => $count , 
			"oldest" => $oldest , 
			"newest" => $newest , 
			"full_stats" => $stats_for_job , 
			"successful_stats" => $successful_stats ,
			"failed_stats" => $failed_stats , 
			"log_name" => $file_name  , 
			"log_timestamp" => $log_timestamp , 
			"log_size" => $log_size ); 		
	}
		
}
?>
Back to Top