/package/app/app/batch/batches/PostConvert/KAsyncPostConvert.class.php
PHP | 275 lines | 170 code | 49 blank | 56 comment | 21 complexity | ac517e7b1913f86c2a9a832898aae4f4 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
- <?php
- /**
- * @package Scheduler
- * @subpackage Post-Convert
- */
- require_once ("bootstrap.php");
-
-
- /**
- * Will convert a single flavor and store it in the file system.
- * The state machine of the job is as follows:
- * get the flavor
- * convert using the right method
- * save recovery file in case of crash
- * move the file to the archive
- * set the entry's new status and file details
- *
- *
- * @package Scheduler
- * @subpackage Post-Convert
- */
- class KAsyncPostConvert extends KBatchBase
- {
- /* (non-PHPdoc)
- * @see KBatchBase::getType()
- */
- public static function getType()
- {
- return KalturaBatchJobType::POSTCONVERT;
- }
-
- /* (non-PHPdoc)
- * @see KBatchBase::getJobType()
- */
- public function getJobType()
- {
- return self::getType();
- }
-
- /* (non-PHPdoc)
- * @see KBatchBase::exec()
- */
- protected function exec(KalturaBatchJob $job)
- {
- return $this->postConvert($job, $job->data);
- }
-
- // TODO remove run, updateExclusiveJob and freeExclusiveJob
-
- public function run($jobs = null)
- {
- KalturaLog::info("Post convert batch is running");
-
- if($this->taskConfig->isInitOnly())
- return $this->init();
-
- if(is_null($jobs))
- $jobs = $this->kClient->batch->getExclusivePostConvertJobs($this->getExclusiveLockKey(), $this->taskConfig->maximumExecutionTime, 1, $this->getFilter());
-
- KalturaLog::info(count($jobs) . " post convert jobs to perform");
-
- if(!count($jobs))
- {
- KalturaLog::info("Queue size: 0 sent to scheduler");
- $this->saveSchedulerQueue(self::getType());
- return null;
- }
-
- foreach($jobs as &$job) {
- $job = $this->postConvert($job, $job->data);
- }
- return $jobs;
- }
-
- /**
- * extractMediaInfo extract the file info using mediainfo and parse the returned data
- *
- * @param string $mediaFile file full path
- * @return KalturaMediaInfo or null for failure
- */
- private function extractMediaInfo($mediaFile)
- {
- KalturaLog::debug("extractMediaInfo($mediaFile)");
-
- $mediaParser = new KMediaInfoMediaParser($mediaFile, $this->taskConfig->params->mediaInfoCmd);
- return $mediaParser->getMediaInfo();
- }
-
- /**
- * extractFfmpegInfo extract the file info using FFmpeg and parse the returned data
- *
- * @param string $mediaFile file full path
- * @return KalturaMediaInfo or null for failure
- */
- private function extractFfmpegInfo($mediaFile)
- {
- KalturaLog::debug("extractFfmpegInfo($mediaFile)");
-
- $mediaParser = new KFFMpegMediaParser($mediaFile, $this->taskConfig->params->FFMpegCmd);
- return $mediaParser->getMediaInfo();
- }
-
- private function postConvert(KalturaBatchJob $job, KalturaPostConvertJobData $data)
- {
- KalturaLog::debug("postConvert($job->id)");
-
- try
- {
- $mediaFile = trim($data->srcFileSyncLocalPath);
-
- if(!file_exists($mediaFile))
- return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file $mediaFile does not exist", KalturaBatchJobStatus::RETRY);
-
- if(!is_file($mediaFile))
- return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file $mediaFile is not a file", KalturaBatchJobStatus::FAILED);
-
- KalturaLog::debug("mediaFile [$mediaFile]");
- $this->updateJob($job,"Extracting file media info on $mediaFile", KalturaBatchJobStatus::QUEUED, 1);
- }
- catch(Exception $ex)
- {
- return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
- }
-
- $mediaInfo = null;
-
- try
- {
- // if($this->taskConfig->params->useMediaInfo)
- $mediaInfo = $this->extractMediaInfo(realpath($mediaFile));
- }
- catch(Exception $ex)
- {
- KalturaLog::err("Error: " . $ex->getMessage());
- $mediaInfo = null;
- }
-
- try
- {
- // if(is_null($mediaInfo) && $this->taskConfig->params->useFFMpeg)
- // $mediaInfo = $this->extractFfmpegInfo(realpath($mediaFile));
-
- if(is_null($mediaInfo))
- {
- return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::EXTRACT_MEDIA_FAILED, "Failed to extract media info: $mediaFile", KalturaBatchJobStatus::FAILED);
- }
-
- KalturaLog::debug("flavorAssetId [$data->flavorAssetId]");
- $mediaInfo->flavorAssetId = $data->flavorAssetId;
- $createdMediaInfo = $this->getClient()->batch->addMediaInfo($mediaInfo);
-
- // must save the mediaInfoId before reporting that the task is finished
- $this->updateJob($job, "Saving media info id $createdMediaInfo->id", KalturaBatchJobStatus::PROCESSED, 50, $data);
-
- $data->thumbPath = null;
- if(!$data->createThumb)
- return $this->closeJob($job, null, null, "Media info id $createdMediaInfo->id saved", KalturaBatchJobStatus::FINISHED, $data);
-
- // creates a temp file path
- $rootPath = $this->taskConfig->params->localTempPath;
- if(! is_dir($rootPath))
- {
- if(file_exists($rootPath))
- return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::CANNOT_CREATE_DIRECTORY, "Cannot create temp thumbnail directory [$rootPath] due to an error", KalturaBatchJobStatus::FAILED, $data);
-
- KalturaLog::info("Creating temp thumbnail directory [$rootPath]");
- mkdir($rootPath);
- }
-
- // creates the path
- $uniqid = uniqid('thumb_');
- $thumbPath = realpath($rootPath) . "/$uniqid";
-
- $videoDurationSec = floor($mediaInfo->videoDuration / 1000);
- $data->thumbOffset = max(0 ,min($data->thumbOffset, $videoDurationSec));
-
- if($mediaInfo->videoHeight)
- $data->thumbHeight = $mediaInfo->videoHeight;
-
- if($mediaInfo->videoBitRate)
- $data->thumbBitrate = $mediaInfo->videoBitRate;
-
- // $width = $mediaInfo->videoWidth;
- // $height = $mediaInfo->videoHeight;
-
- // generates the thumbnail
- $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $thumbPath, $this->taskConfig->params->FFMpegCmd);
- // $created = $thumbMaker->createThumnail($data->thumbOffset, $width, $height);
- $created = $thumbMaker->createThumnail($data->thumbOffset);
-
- if(!$created || !file_exists($thumbPath))
- {
- $data->createThumb = false;
- return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, 'Thumbnail not created', KalturaBatchJobStatus::FINISHED, $data);
- }
- $data->thumbPath = $thumbPath;
-
- $job = $this->moveFile($job, $data);
-
- if($this->checkFileExists($job->data->thumbPath))
- return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $data);
-
- $data->createThumb = false;
- return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FINISHED, $data);
- }
- catch(Exception $ex)
- {
- return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
- }
- }
-
- /**
- * @param KalturaBatchJob $job
- * @param KalturaPostConvertJobData $data
- * @return KalturaBatchJob
- */
- private function moveFile(KalturaBatchJob $job, KalturaPostConvertJobData $data)
- {
- KalturaLog::debug("moveFile($job->id, $data->thumbPath)");
-
- // creates a temp file path
- $rootPath = $this->taskConfig->params->sharedTempPath;
- if(! is_dir($rootPath))
- {
- if(! file_exists($rootPath))
- {
- KalturaLog::info("Creating temp thumbnail directory [$rootPath]");
- mkdir($rootPath);
- }
- else
- {
- // already exists but not a directory
- $err = "Cannot create temp thumbnail directory [$rootPath] due to an error. Please fix and restart";
- throw new Exception($err, -1);
- }
- }
-
- $uniqid = uniqid('thumb_');
- $sharedFile = realpath($rootPath) . "/$uniqid";
-
- clearstatcache();
- $fileSize = filesize($data->thumbPath);
- rename($data->thumbPath, $sharedFile);
- if(!file_exists($sharedFile) || filesize($sharedFile) != $fileSize)
- {
- $err = 'moving file failed';
- throw new Exception($err, -1);
- }
-
- @chmod($sharedFile, 0777);
- $data->thumbPath = $sharedFile;
- $job->data = $data;
- return $job;
- }
-
- protected function updateExclusiveJob($jobId, KalturaBatchJob $job)
- {
- return $this->kClient->batch->updateExclusivePostConvertJob($jobId, $this->getExclusiveLockKey(), $job);
- }
-
- protected function freeExclusiveJob(KalturaBatchJob $job)
- {
- $resetExecutionAttempts = false;
- if($job->status == KalturaBatchJobStatus::ALMOST_DONE)
- $resetExecutionAttempts = true;
-
- $response = $this->kClient->batch->freeExclusivePostConvertJob($job->id, $this->getExclusiveLockKey(), $resetExecutionAttempts);
-
- KalturaLog::info("Queue size: $response->queueSize sent to scheduler");
- $this->saveSchedulerQueue(self::getType(), $response->queueSize);
-
- return $response->job;
- }
- }
- ?>