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

Language PHP Lines 220
MD5 Hash d7d2d0e7e1ad81ec7a37c0f425703b0c Estimated Cost $4,281 (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
<?php
require_once( 'myBatchBase.class.php');
require_once( 'myContentStorage.class.php');
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'lib/model/BatchJob.php');

class myBatchFlattenClient
{
	/**
	 * @param string $puser_id
	 * @param string $entry
	 * @param string $version
	 * @param string $file_format
	 * @return BatchJob
	 */
	public static function addJob($puser_id, $entry, $version, $file_format)
	{
		$entryId = $entry->getId();
		$entryIntId = $entry->getIntId();
		$entryVersion = $version ? $version : $entry->getVersion();

		if ( $entry )
		{
			$partner = $entry->getPartner ();
			$email = $partner->getAdminEmail();
		}
		
		$data = json_encode(array(
		'puserId' => $puser_id,
		'entryId' => $entryId,
		'entryIntId' => $entryIntId,
		'entryVersion' => $entryVersion,
		'fileFormat' => $file_format,
		'email' => $email,
		//'serverUrl' => "http://xp/final/$entryId_$entryVersion.avi",
		//'deleteUrl' => "http://xp:1234/DeleteMovie/$entryId_$entryVersion.avi"
		));
		
		$job = new BatchJob();
		$job->setJobType(BatchJobType::FLATTEN);
		$job->setData($data, true);
		$job->setStatus(BatchJob::BATCHJOB_STATUS_PENDING);
		$job->setCheckAgainTimeout(time() + 10);
		$job->setProgress(0);
		$job->setMessage('Queued');
		$job->setDescription('Queued, waiting to run');
		$job->setUpdatesCount(0);
		$job->setEntryId( $entryId );
		$job->setPartnerId( $entry->getPartnerId());
		$job->setSubpId ( $entry->getSubpId());
		
		$job->save();
		
		return $job;
	}
}

class myBatchFlattenServer extends myBatchBase
{
	const KALTURAS_FLATTEN_READY = 60;
	
	public static function getBatchStatus( $args )	
	{	
		$batch_status = new batchStatus();
		$batch_status->batch_name = $args[0];
		$stats = $batch_status->getDbStats( $batch_status->batch_name , BatchJobType::FLATTEN );
		$batch_status->addToPending( "DB:batch_job, type=" . BatchJobType::FLATTEN . " status=" . BatchJob::BATCHJOB_STATUS_PENDING , @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_PENDING]["count"]);
		$batch_status->addToInProc( "DB:batch_job, type=" . BatchJobType::FLATTEN . " status=" . BatchJob::BATCHJOB_STATUS_PROCESSING , @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_PROCESSING]["count"] );
		
		$batch_status->succeedded_in_period = @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_FINISHED]["count"];
		$batch_status->failed_in_period = @$stats["full_stats"][BatchJob::BATCHJOB_STATUS_FAILED]["count"];
		
		$batch_status->last_log_time  = @$stats["log_timestamp"];
		return $batch_status; 
	}
		
	public function myBatchFlattenServer( $script_name )
	{
		$this->script_name = $script_name;
		$this->register( $script_name );
		
		SET_CONTEXT ( "FS");
		
		$MAX_ITERATIONS_DUE_TO_PROPEL_MEMORY_LEAK = 10000000;
		
		self::initDb();

		list ( $sleep_between_cycles ,
		$number_of_times_to_skip_writing_sleeping ) = self::getSleepParams( 'app_flatten_' );

		$last_worker_count = 0;
		$iteration = 0;
		
		$c = new Criteria();
		$currentDc = kDataCenterMgr::getCurrentDc();
		$c->add(BatchJobPeer::DC, kDataCenterMgr::getCurrentDcId() );
		$c->add(BatchJobPeer::JOB_TYPE, BatchJobType::FLATTEN);
		$c->add(BatchJobPeer::STATUS, BatchJob::BATCHJOB_STATUS_PROCESSED);

		$temp_count = 0;
		while(1)
		{
			self::exitIfDone();
			try 
			{
				sleep($sleep_between_cycles);
				
				$jobs = BatchJobPeer::doSelect($c);
				
				foreach($jobs as $job)
				{
					$data = json_decode($job->getData(true), true);
					
					$entry_id = $data['entryId'];
					$entry_int_id = $data['entryIntId'];
					$entry_version = $data['entryVersion'];
					$file_format = $data['fileFormat'];

					$entry = entryPeer::retrieveByPK($entry_id);
					if(!$entry)
					{
						// entry is probably deleted if it is not returned from retrieveByPK
						// close job as failed
						$job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
						$job->setDescription("could not retrieve entry, probably deleted");
						TRACE("could not retrieve entry $entry_id , probably deleted");
						$job->save();
						continue;
					}
					
					$fileSyncKey = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DOWNLOAD, $file_format);
					$fullFinalPath = kFileSyncUtils::getLocalFilePathForKey($fileSyncKey);
					$finalPathNoExt = substr($fullFinalPath, 0 , strlen($fullFinalPath)-strlen($file_format));

					myContentStorage::fullMkdir($fullFinalPath);
					
					$wildcardFinalPath = $finalPathNoExt."*";
					$older_files = glob($wildcardFinalPath);
					foreach($older_files as $older_file)
					{
						TRACE("removing old file: [$older_file]");
						@unlink($older_file);
					}
					
					TRACE("Downloading: $fullFinalPath");
					kFile::downloadUrlToFile($data["serverUrl"], $fullFinalPath);
					if (!file_exists($fullFinalPath))
					{
						TRACE("file doesnt exist: ". $data["serverUrl"]);
						$job->setDescription("file doesnt exist: ". $data["serverUrl"]);
						$job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
					}
					else if (filesize($fullFinalPath) < 100000)
					{
						@unlink($fullFinalPath);
						TRACE("file too small: ". $data["serverUrl"]);
						$job->setDescription("file too small: ". $data["serverUrl"]);
						$job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
					}
					else
					{
						if ($data['email'])
						{
							$downloadLink = $entry->getDownloadUrl().'/format/'.$file_format;
							kJobsManager::addMailJob(
								null, 
								$entry_id,
								$entry->getPartnerId(),
								self::KALTURAS_FLATTEN_READY, 
								kMailJobData::MAIL_PRIORITY_NORMAL, 
								kConf::get ( "batch_flatten_video_sender_email" ), 
								kConf::get ( "batch_flatten_video_sender_name" ), 
								$data['email'], 
								array($data['email'] , $downloadLink));
						}
						
						TRACE("Deleting: ".$data["deleteUrl"]);
						kFile::downloadUrlToString($data["deleteUrl"]);
						
						myNotificationMgr::createNotification( kNotificationJobData::NOTIFICATION_TYPE_ENTRY_UPDATE, $entry );
						
						$job->setStatus(BatchJob::BATCHJOB_STATUS_FINISHED);
						
						$filePath = kFileSyncUtils::getLocalFilePathForKey($fileSyncKey);
						if (file_exists($filePath))
						{
							try {
								kFileSyncUtils::createSyncFileForKey($fileSyncKey);
							}
							catch(Exception $ex) // hack for the case where the file sync already exists and we re-flattened a mix
							{
								TRACE ( "ignore ERROR: " . $ex->getMessage() );
							}
						}							
						else
							TRACE("The file [$filePath] doesn't exists, not creating FileSync");
					}
					$job->save();
				}
			}	
			catch ( Exception $ex )
			{
				TRACE ( "ERROR: " . $ex->getMessage() );
				self::initDb( true );
				self::failed();
			}
			
			if ( $temp_count == 0 )
			{
				TRACE ( "Ended conversion. sleeping for a while (" . $sleep_between_cycles .
				" seconds). Will write to the log in (" . ( $sleep_between_cycles * $number_of_times_to_skip_writing_sleeping ) . ") seconds" );
			}

			$temp_count++;
			if ($temp_count >= $number_of_times_to_skip_writing_sleeping ) $temp_count = 0;
		
		}
	}
}

?>
Back to Top