kaltura /package/app/app/alpha/apps/kaltura/modules/system/actions/batchwatchAction.class.php

Language PHP Lines 358
MD5 Hash b0ca7add1015a21b7217eabbe9a1a743 Estimated Cost $6,090 (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
<?php
/**
 * @package    Core
 * @subpackage system
 * @deprecated
 */
require_once ( "model/genericObjectWrapper.class.php" );
require_once ( "kalturaSystemAction.class.php" );

/**
 * @package    Core
 * @subpackage system
 * @deprecated
 */
class batchwatchAction extends kalturaSystemAction
{
	private $log_info;
	
	/**
	 * Will give a good view of the batch processes in the system
	 */
	public function execute()
	{
		myDbHelper::$use_alternative_con = myDbHelper::DB_HELPER_CONN_PROPEL2;
		
		$this->forceSystemAuthentication();

		if ( $this->getP ( "mode" , null ) == "restart" )
		{
			$batch_name = $this->getP ( "batch" );
			$path = "/web/batchwatch/$batch_name";
			echo $path;
			@file_put_contents( $path , "restart" ); // sync - OK
			die();	
		}
		
		$this->setLogInfo();
		
		$this->include_old = $this->getP ( "old" , false );
		// import    - DB
		// old convert client (in) + client errors - DISK  
		// old convert server + server errors - DISK
		// old convert client (out) - DISK
		// handled- DISK
		// emails - DB
		// flatten - DB
		// new convert client (in) - DISK
		// new convert server - DISK
		// new convert client (out)- DISK
		// commercial encoder (in) - DISK
		// download video (in) - DB
		// download video (out) - DISK
		// notifications - DB

		// check logs - to see that jobs are running - make sure that was written not too long ago
		// green | yellow | red for each section depending on thresholds
		
		// TODO - read from paramter to understand the time frame of which to query the db 
		$hours_back  = $this->getP ( "h" , 24 );
		
		$this->hours_back = $hours_back;
		// search in DB for pending / errornouse jobs
		$db_job_stats = $this->getJobStats ( $hours_back );
		
		$content = myContentStorage::getFSContentRootPath();
		// saerch on disk in varios directories
		
		$this->import = $this->getDbStats ( "batchImportServer" , $db_job_stats , BatchJobType::IMPORT );
		$this->flatten = $this->getDbStats ( "batchFlattenServer" , $db_job_stats , BatchJobType::FLATTEN );
		$this->bulk = $this->getDbStats ( "batchBulkUpload" , $db_job_stats , BatchJobType::BULKUPLOAD );
		
		$db_convert_stats = $this->getConversionStats ( $hours_back );

		if ( $this->include_old )
		{
			$this->old_convert_client_in = $this->getDiskStats ( "batchConvertClient" , $content . "/content/preconvert/files" );
			$this->old_convert_client_out = $this->getDiskStats ( "batchConvertClient" , $content . "/conversions/postconvert/files" );
			$this->old_convert_client_out_db = $this->getDbStats ( "batchConvertClient" , $db_convert_stats , 
				BatchJobType::CONVERT  );
			
			$this->old_convert_client_out["full_stats"] = $this->old_convert_client_out_db["full_stats"];
			$this->old_convert_client_out["successful_stats"] = $this->old_convert_client_out_db["successful_stats"];
			$this->old_convert_client_out["count"] = $this->old_convert_client_out["count"] + 
			$this->old_convert_client_out_db["full_stats"][BatchJob::BATCHJOB_STATUS_PROCESSING]["count"];  
	//		$this->old_convert_client_out = $this->mergeStats ( )
			$this->old_convert_client_errors = $this->getDiskStats ( "__reconert"  ,$content . "/conversions/CLIENT_ERRORS/files" );
			$this->old_convert_server = $this->getDiskStats ( "batchConvertServer" ,$content . "/conversions/preconvert/files" );
			$this->old_convert_server_in_proc = $this->getDiskStats ( null ,$content . "/conversions/preconvert/inprocess_files" ); // see how many files are in the in_process dir - indicating process of the old server
			// merge the 2 results from the old server directory
			$this->old_convert_server  = $this->mergeStats ( 0 , $this->old_convert_server , 2 , $this->old_convert_server_in_proc );
			
			$this->old_convert_server_errors = $this->getDiskStats ( null , $content . "/conversions/SERVER_ERRORS/files" );
		}
		
		$this->new_convert_client_in = $this->getDiskStats ( "newBatchConvertClient" , $content . "/content/new_preconvert" , "*.in*" ) ; //dicator" );
		$this->new_convert_client_out = $this->getDiskStats ( "newBatchConvertClient" , $content . "/conversions/postconvert_res" , "*.in*" ) ; //dicator" );
		$this->new_convert_server = $this->getDiskStats ( "newBatchConvertServer" , $content . "/conversions/preconvert_cmd" , "*.in*" ) ; //dicator" );
		$this->new_commercial_convert_server = $this->getDiskStats ( "newBatchCommercialConvertServer" , $content . "/conversions/preconvert_commercial_cmd" , "*.in*" ) ; //dicator" );
		
		$this->download_video_in = $this->getDbStats ( null , $db_job_stats , BatchJobType::DOWNLOAD );
		$this->download_video_out = $this->getDiskStats ( "batchDownloadVideoServer" , $content . "/conversions/download_res" , "*.in*" ) ; //dicator" );
		
		
		
	}
	
	
	private function setLogInfo ()
	{
		$log_info_file = myContentStorage::getFSContentRootPath() . "/logs/logsize.log";
		
		// this is the new way to read the log data on production - due to logs being created on other mchines and other disks
		if ( file_exists( $log_info_file ) )
		{
			$content = file_get_contents( $log_info_file );
			$lines = explode ( "\n" , $content );
			
			$this->log_info =  $lines;
		}
	}
	/*
		will return :
		file count in dir
		oldest file timestamp
		newest file timestamp
		date & size of log file
	 * 
	 */
	private 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 )
		{
//echo "$file" . "<br>";			
			$timestamp =  filemtime ( $file );
			if ( $oldest == null || $timestamp < $oldest  ) $oldest = $timestamp;
			if ( $timestamp > $newest ) $newest = $timestamp;
		}

		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" => $path , 
			"count" => $count , 
			"oldest" => $oldest , 
			"newest" => $newest , 
			"log_name" => $file_name  , 
			"log_timestamp" => $log_timestamp , 
			"log_size" => $log_size );
	}
	
	private function getDbStats ( $log_name , $db_job_stats , $job_type , $successful_stats_enum = BatchJob::BATCHJOB_STATUS_FINISHED ,
		$failed_stats_enum = BatchJob::BATCHJOB_STATUS_FAILED )
	{
		$oldest = null;
		$newest = 0;
		$count = 0;
		$stats_for_job = @$db_job_stats[$job_type];
		$successful_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;
				}
				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"];
					if ( $status != $failed_stats_enum ) // don't count the failed enums in the total count  
						$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 ,
			"log_name" => $file_name  , 
			"log_timestamp" => $log_timestamp , 
			"log_size" => $log_size ); 		
	}
	
	private function mergeStats ( $job_status1 , $stats1 , $job_status2 , $stats2 )
	{
		return array ( 
			"service_name" => $stats1["service_name"] ,  // all log_names are called after their services
			"path" => $stats1["path"] , 
			"count" => $stats1["count"] + $stats2["count"] , 
			"oldest" => min ( $stats1["oldest"] , $stats2["oldest"] ), 
			"newest" => max ( $stats1["newest"] , $stats2["newest"] ), 
			"full_stats" => array ( $job_status1 => $stats1 , $job_status2 => $stats2 	) , 
			"log_name" => $stats1["log_name"]  , 
			"log_timestamp" => $stats1["log_timestamp"] , 
			"log_size" => $stats1["log_size"] );			
	}
	
	// TODO - gets all stats from db and organizes them in a matrix 
	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;
				
	}
	
	// TODO - gets all stats from db and organizes them in a matrix 
	private function getConversionStats ( $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 status , UNIX_TIMESTAMP(min(created_at)) as oldest , UNIX_TIMESTAMP(max(created_at)) as newest , count(1) as cnt " .
	    	"from conversion where status in (-1,1,2) and created_at>\"$from_date\" " . 
	    	"group by status";
		
		
		$statement = $connection->prepareStatement($query);
		$resultset = $statement->executeQuery();	

		$db_convert_stats = array();
		while ($resultset->next())
	    {
	    	$job_type = BatchJobType::CONVERT;
	    	$status = $resultset->getInt('status');
			$oldest = $resultset->getInt('oldest');
	    	$newest = $resultset->getInt('newest');
	    	$count = $resultset->getInt('cnt');

	    	if ( !isset ( $db_convert_stats[$job_type] ) )
	    	{
	    		$conversion_data = array();
	    	}
	    	else
	    	{
	    		$conversion_data =  $db_convert_stats[$job_type];
	    	}
	    	
	    	switch ($status)
	    	{
		    	case conversion::CONVERSION_STATUS_ERROR: 
		    		$status = BatchJob::BATCHJOB_STATUS_FAILED;
		    		break;
		    	case conversion::CONVERSION_STATUS_PRECONVERT: 
		    		$status = BatchJob::BATCHJOB_STATUS_PROCESSING;
		    		break;
		    	case conversion::CONVERSION_STATUS_COMPLETED: 
		    		$status = BatchJob::BATCHJOB_STATUS_FINISHED;
		    		break;
	    	}	
	    	
	    	// foreach job_type - creaet an array per status
	    	$conversion_data [$status] = array ( "oldest" => $oldest , "newest" => $newest , "count" => $count ) ;
	    	$db_convert_stats[$job_type] = $conversion_data;
	    }
		return $db_convert_stats;
	}
	
	
	
	private function getLogData ( $log_name )
	{
		if ( $this->log_info )
		{
			foreach ( $this->log_info as $line )
			{	
				if ( ! $line ) continue;
//				echo "[$line]<br>";
				list ( $log_size , $log_timestamp , $file_name ) = explode ( " " , $line );
				if ( strpos ( $file_name , $log_name ) > 0 )
					return array ( "*" . $file_name , (int)$log_timestamp , $log_size );
			}
		}
		
		
		$file_name = null;
		$log_timestamp = "";
		$log_size = "0";
	
		if ( $log_name )
		{
			$log_dir = myContentStorage::getFSContentRootPath() . "/logs/";
			$pat = $log_dir . "/*$log_name*";
//echo "[$pat]<br>";			
			$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 );
	}
}
?>
Back to Top