PageRenderTime 104ms CodeModel.GetById 60ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 0ms

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

https://bitbucket.org/pandaos/kaltura
PHP | 220 lines | 179 code | 30 blank | 11 comment | 7 complexity | d7d2d0e7e1ad81ec7a37c0f425703b0c MD5 | raw file
  1<?php
  2require_once( 'myBatchBase.class.php');
  3require_once( 'myContentStorage.class.php');
  4require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'lib/model/BatchJob.php');
  5
  6class myBatchFlattenClient
  7{
  8	/**
  9	 * @param string $puser_id
 10	 * @param string $entry
 11	 * @param string $version
 12	 * @param string $file_format
 13	 * @return BatchJob
 14	 */
 15	public static function addJob($puser_id, $entry, $version, $file_format)
 16	{
 17		$entryId = $entry->getId();
 18		$entryIntId = $entry->getIntId();
 19		$entryVersion = $version ? $version : $entry->getVersion();
 20
 21		if ( $entry )
 22		{
 23			$partner = $entry->getPartner ();
 24			$email = $partner->getAdminEmail();
 25		}
 26		
 27		$data = json_encode(array(
 28		'puserId' => $puser_id,
 29		'entryId' => $entryId,
 30		'entryIntId' => $entryIntId,
 31		'entryVersion' => $entryVersion,
 32		'fileFormat' => $file_format,
 33		'email' => $email,
 34		//'serverUrl' => "http://xp/final/$entryId_$entryVersion.avi",
 35		//'deleteUrl' => "http://xp:1234/DeleteMovie/$entryId_$entryVersion.avi"
 36		));
 37		
 38		$job = new BatchJob();
 39		$job->setJobType(BatchJobType::FLATTEN);
 40		$job->setData($data, true);
 41		$job->setStatus(BatchJob::BATCHJOB_STATUS_PENDING);
 42		$job->setCheckAgainTimeout(time() + 10);
 43		$job->setProgress(0);
 44		$job->setMessage('Queued');
 45		$job->setDescription('Queued, waiting to run');
 46		$job->setUpdatesCount(0);
 47		$job->setEntryId( $entryId );
 48		$job->setPartnerId( $entry->getPartnerId());
 49		$job->setSubpId ( $entry->getSubpId());
 50		
 51		$job->save();
 52		
 53		return $job;
 54	}
 55}
 56
 57class myBatchFlattenServer extends myBatchBase
 58{
 59	const KALTURAS_FLATTEN_READY = 60;
 60	
 61	public static function getBatchStatus( $args )	
 62	{	
 63		$batch_status = new batchStatus();
 64		$batch_status->batch_name = $args[0];
 65		$stats = $batch_status->getDbStats( $batch_status->batch_name , BatchJobType::FLATTEN );
 66		$batch_status->addToPending( "DB:batch_job, type=" . BatchJobType::FLATTEN . " status=" . BatchJob::BATCHJOB_STATUS_PENDING , @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_PENDING]["count"]);
 67		$batch_status->addToInProc( "DB:batch_job, type=" . BatchJobType::FLATTEN . " status=" . BatchJob::BATCHJOB_STATUS_PROCESSING , @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_PROCESSING]["count"] );
 68		
 69		$batch_status->succeedded_in_period = @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_FINISHED]["count"];
 70		$batch_status->failed_in_period = @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_FAILED]["count"];
 71		
 72		$batch_status->last_log_time  = @$stats["log_timestamp"];
 73		return $batch_status; 
 74	}
 75		
 76	public function myBatchFlattenServer( $script_name )
 77	{
 78		$this->script_name = $script_name;
 79		$this->register( $script_name );
 80		
 81		SET_CONTEXT ( "FS");
 82		
 83		$MAX_ITERATIONS_DUE_TO_PROPEL_MEMORY_LEAK = 10000000;
 84		
 85		self::initDb();
 86
 87		list ( $sleep_between_cycles ,
 88		$number_of_times_to_skip_writing_sleeping ) = self::getSleepParams( 'app_flatten_' );
 89
 90		$last_worker_count = 0;
 91		$iteration = 0;
 92		
 93		$c = new Criteria();
 94		$currentDc = kDataCenterMgr::getCurrentDc();
 95		$c->add(BatchJobPeer::DC, kDataCenterMgr::getCurrentDcId() );
 96		$c->add(BatchJobPeer::JOB_TYPE, BatchJobType::FLATTEN);
 97		$c->add(BatchJobPeer::STATUS, BatchJob::BATCHJOB_STATUS_PROCESSED);
 98
 99		$temp_count = 0;
100		while(1)
101		{
102			self::exitIfDone();
103			try 
104			{
105				sleep($sleep_between_cycles);
106				
107				$jobs = BatchJobPeer::doSelect($c);
108				
109				foreach($jobs as $job)
110				{
111					$data = json_decode($job->getData(true), true);
112					
113					$entry_id = $data['entryId'];
114					$entry_int_id = $data['entryIntId'];
115					$entry_version = $data['entryVersion'];
116					$file_format = $data['fileFormat'];
117
118					$entry = entryPeer::retrieveByPK($entry_id);
119					if(!$entry)
120					{
121						// entry is probably deleted if it is not returned from retrieveByPK
122						// close job as failed
123						$job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
124						$job->setDescription("could not retrieve entry, probably deleted");
125						TRACE("could not retrieve entry $entry_id , probably deleted");
126						$job->save();
127						continue;
128					}
129					
130					$fileSyncKey = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DOWNLOAD, $file_format);
131					$fullFinalPath = kFileSyncUtils::getLocalFilePathForKey($fileSyncKey);
132					$finalPathNoExt = substr($fullFinalPath, 0 , strlen($fullFinalPath)-strlen($file_format));
133
134					myContentStorage::fullMkdir($fullFinalPath);
135					
136					$wildcardFinalPath = $finalPathNoExt."*";
137					$older_files = glob($wildcardFinalPath);
138					foreach($older_files as $older_file)
139					{
140						TRACE("removing old file: [$older_file]");
141						@unlink($older_file);
142					}
143					
144					TRACE("Downloading: $fullFinalPath");
145					kFile::downloadUrlToFile($data["serverUrl"], $fullFinalPath);
146					if (!file_exists($fullFinalPath))
147					{
148						TRACE("file doesnt exist: ". $data["serverUrl"]);
149						$job->setDescription("file doesnt exist: ". $data["serverUrl"]);
150						$job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
151					}
152					else if (filesize($fullFinalPath) < 100000)
153					{
154						@unlink($fullFinalPath);
155						TRACE("file too small: ". $data["serverUrl"]);
156						$job->setDescription("file too small: ". $data["serverUrl"]);
157						$job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
158					}
159					else
160					{
161						if ($data['email'])
162						{
163							$downloadLink = $entry->getDownloadUrl().'/format/'.$file_format;
164							kJobsManager::addMailJob(
165								null, 
166								$entry_id,
167								$entry->getPartnerId(),
168								self::KALTURAS_FLATTEN_READY, 
169								kMailJobData::MAIL_PRIORITY_NORMAL, 
170								kConf::get ( "batch_flatten_video_sender_email" ), 
171								kConf::get ( "batch_flatten_video_sender_name" ), 
172								$data['email'], 
173								array($data['email'] , $downloadLink));
174						}
175						
176						TRACE("Deleting: ".$data["deleteUrl"]);
177						kFile::downloadUrlToString($data["deleteUrl"]);
178						
179						myNotificationMgr::createNotification( kNotificationJobData::NOTIFICATION_TYPE_ENTRY_UPDATE, $entry );
180						
181						$job->setStatus(BatchJob::BATCHJOB_STATUS_FINISHED);
182						
183						$filePath = kFileSyncUtils::getLocalFilePathForKey($fileSyncKey);
184						if (file_exists($filePath))
185						{
186							try {
187								kFileSyncUtils::createSyncFileForKey($fileSyncKey);
188							}
189							catch(Exception $ex) // hack for the case where the file sync already exists and we re-flattened a mix
190							{
191								TRACE ( "ignore ERROR: " . $ex->getMessage() );
192							}
193						}							
194						else
195							TRACE("The file [$filePath] doesn't exists, not creating FileSync");
196					}
197					$job->save();
198				}
199			}	
200			catch ( Exception $ex )
201			{
202				TRACE ( "ERROR: " . $ex->getMessage() );
203				self::initDb( true );
204				self::failed();
205			}
206			
207			if ( $temp_count == 0 )
208			{
209				TRACE ( "Ended conversion. sleeping for a while (" . $sleep_between_cycles .
210				" seconds). Will write to the log in (" . ( $sleep_between_cycles * $number_of_times_to_skip_writing_sleeping ) . ") seconds" );
211			}
212
213			$temp_count++;
214			if ($temp_count >= $number_of_times_to_skip_writing_sleeping ) $temp_count = 0;
215		
216		}
217	}
218}
219
220?>