PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/package/app/app/alpha/batch/batchWatch.php

https://bitbucket.org/pandaos/kaltura
PHP | 88 lines | 28 code | 4 blank | 56 comment | 1 complexity | f05dc64990d3207e9f82c9168c74ccb6 MD5 | raw file
 1#!/usr/bin/php
 2<?php
 3/*
 4 * Created on Nov 25, 2006
 5 *
 6 * To change the template for this generated file go to
 7 * Window - Preferences - PHPeclipse - PHP - Code Templates
 8 */
 9require_once(realpath(dirname(__FILE__)).'/../config/sfrootdir.php');
10define('SF_APP',         'kaltura');
11define('SF_ENVIRONMENT', 'batch');
12define('SF_DEBUG',       true);
13
14$PHP_CMD = "php" ;
15
16require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
17
18$batchwatch_job_list = array ( "batchConvertClient" ,
19	"batchConvertServer", 
20	"batchBulkUpload",
21	"batchDownloadVideoServer", 
22	"batchEmailServer",
23	"batchEntryDeleteServer", 
24	"batchMobileUploadServer",
25	"batchFlattenServer",
26	"batchImportServer",
27	"batchNotificationServer", 
28	"newBatchCommercialConvertServer", 
29	"newBatchConvertClient",
30	"newBatchConvertServer" , );
31
32$count = 0;
33$sleep_time = 1;
34$write_to_the_log_seconds = 120;
35while ( 1 )
36{
37	try
38	{
39		$files = glob ( myBatchBase::getBatchwatchPath() . "/*");
40		
41		SET_CONTEXT ("batchWatch" );
42		
43		// the files in this directory are supposed to be batch names to be started/stopped or restarted
44		foreach ( $files as $file )
45		{
46			$batch_name = pathinfo( $file , PATHINFO_FILENAME );
47			if ( $batch_name == myBatchBase::REGISTERED_BATCHS ) continue; // skip if the special file holding the registered batchs
48			if ( $batch_name[0] == "_" ) continue; //myBatchBase::IGNORE_PREFIX  ) continue;
49			if ( in_array ( $batch_name , $batchwatch_job_list )) 
50			{
51				$command = file_get_contents( $file );
52		// TODO - change to fit the way we start / stop / restart  
53//				$cmd_line = "service $batch_name $command";
54				$cmd_line = "$PHP_CMD runBatch.php $command $batch_name ";
55				TRACE ( "$cmd_line");
56				$output = array();
57				exec ( $cmd_line , $output , $return_var );
58				TRACE ( "Result: [$return_var]\n" . print_r ( $output , true ));
59				if ( $command == "stop")
60				{
61					$path = batchStatus::batchEnd( $batch_name );
62				}
63			}
64			else
65			{
66				TRACE ( "VERY BAD!!: tying to manipulate invalid batch [$batch_name]. Ignoring..." );
67			}
68			
69			// remove the file 
70			unlink ( $file );
71		}
72
73		if ( $count > $write_to_the_log_seconds )
74		{
75			$count = 0;
76		}
77		if ( $count == 0 )
78			TRACE ( "Sleeping for [$sleep_time] second. Will write to the log in ($write_to_the_log_seconds]" );
79		
80		sleep ( $sleep_time  );			
81		$count ++;
82	}
83	catch ( Exception $ex )
84	{
85		TRACE ( "Error:" . $ex->getTrace() );
86	}
87}
88?>