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