/** * @param BatchJob $dbBatchJob * @param flavorAsset $currentFlavorAsset * @return BatchJob */ public static function handleConvertFinished(BatchJob $dbBatchJob = null, flavorAsset $currentFlavorAsset) { KalturaLog::debug("entry id [" . $currentFlavorAsset->getEntryId() . "] flavor asset id [" . $currentFlavorAsset->getId() . "]"); $profile = null; try { $profile = myPartnerUtils::getConversionProfile2ForEntry($currentFlavorAsset->getEntryId()); KalturaLog::debug("profile [" . $profile->getId() . "]"); } catch (Exception $e) { KalturaLog::err($e->getMessage()); } $currentReadyBehavior = self::getReadyBehavior($currentFlavorAsset, $profile); KalturaLog::debug("Current ready behavior [{$currentReadyBehavior}]"); if ($currentReadyBehavior == flavorParamsConversionProfile::READY_BEHAVIOR_IGNORE) { return $dbBatchJob; } $rootBatchJob = null; if ($dbBatchJob) { $rootBatchJob = $dbBatchJob->getRootJob(); } if ($rootBatchJob) { KalturaLog::debug("root batch job id [" . $rootBatchJob->getId() . "] type [" . $rootBatchJob->getJobType() . "]"); } // update the root job end exit if ($rootBatchJob && $rootBatchJob->getJobType() == BatchJobType::BULKDOWNLOAD) { $siblingJobs = $rootBatchJob->getChildJobs(); foreach ($siblingJobs as $siblingJob) { // checking only conversion child jobs if ($siblingJob->getJobType() != BatchJobType::CONVERT && $siblingJob->getJobType() != BatchJobType::CONVERT_COLLECTION && $siblingJob->getJobType() != BatchJobType::POSTCONVERT) { continue; } // if not complete leave function if (!in_array($siblingJob->getStatus(), BatchJobPeer::getClosedStatusList())) { KalturaLog::debug("job id [" . $siblingJob->getId() . "] status [" . $siblingJob->getStatus() . "]"); return $dbBatchJob; } } KalturaLog::debug("finish bulk download root job"); // all child jobs completed kJobsManager::updateBatchJob($rootBatchJob, BatchJob::BATCHJOB_STATUS_FINISHED); return $dbBatchJob; } $requiredFlavorParamsIds = array(); $flavorParamsConversionProfileItems = array(); if ($profile) { $flavorParamsConversionProfileItems = flavorParamsConversionProfilePeer::retrieveByConversionProfile($profile->getId()); } foreach ($flavorParamsConversionProfileItems as $flavorParamsConversionProfile) { if ($flavorParamsConversionProfile->getReadyBehavior() == flavorParamsConversionProfile::READY_BEHAVIOR_REQUIRED) { $requiredFlavorParamsIds[$flavorParamsConversionProfile->getFlavorParamsId()] = true; } } KalturaLog::debug("required flavor params ids [" . print_r($requiredFlavorParamsIds, true) . "]"); // go over all the flavor assets of the entry $entry = $currentFlavorAsset->getentry(); $inCompleteFlavorIds = array(); $origianlAssetFlavorId = null; $siblingFlavorAssets = assetPeer::retrieveFlavorsByEntryId($currentFlavorAsset->getEntryId()); foreach ($siblingFlavorAssets as $siblingFlavorAsset) { KalturaLog::debug("sibling flavor asset id [" . $siblingFlavorAsset->getId() . "] flavor params id [" . $siblingFlavorAsset->getFlavorParamsId() . "]"); // don't mark any incomplete flag if ($siblingFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_READY) { KalturaLog::debug("sibling flavor asset id [" . $siblingFlavorAsset->getId() . "] is ready"); if (isset($requiredFlavorParamsIds[$siblingFlavorAsset->getFlavorParamsId()])) { unset($requiredFlavorParamsIds[$siblingFlavorAsset->getFlavorParamsId()]); } continue; } if ($entry->getReplacedEntryId() && $siblingFlavorAsset->getStatus() == flavorAsset::ASSET_STATUS_QUEUED) { KalturaLog::debug("sibling flavor asset id [" . $siblingFlavorAsset->getId() . "] is incomplete and in replacement"); $inCompleteFlavorIds[] = $siblingFlavorAsset->getFlavorParamsId(); } $readyBehavior = self::getReadyBehavior($siblingFlavorAsset, $profile); if ($siblingFlavorAsset->getStatus() == flavorAsset::ASSET_STATUS_EXPORTING) { if ($siblingFlavorAsset->getIsOriginal()) { $origianlAssetFlavorId = $siblingFlavorAsset->getFlavorParamsId(); } else { if ($readyBehavior != flavorParamsConversionProfile::READY_BEHAVIOR_IGNORE) { KalturaLog::debug("sibling flavor asset id [" . $siblingFlavorAsset->getId() . "] is incomplete"); $inCompleteFlavorIds[] = $siblingFlavorAsset->getFlavorParamsId(); } } } if ($readyBehavior == flavorParamsConversionProfile::READY_BEHAVIOR_IGNORE) { KalturaLog::debug("sibling flavor asset id [" . $siblingFlavorAsset->getId() . "] is ignored"); continue; } if ($siblingFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_QUEUED || $siblingFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_CONVERTING || $siblingFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_IMPORTING || $siblingFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_VALIDATING || $siblingFlavorAsset->getStatus() == flavorAsset::ASSET_STATUS_WAIT_FOR_CONVERT) { KalturaLog::debug("sibling flavor asset id [" . $siblingFlavorAsset->getId() . "] is incomplete"); $inCompleteFlavorIds[] = $siblingFlavorAsset->getFlavorParamsId(); } if ($readyBehavior == flavorParamsConversionProfile::READY_BEHAVIOR_REQUIRED) { KalturaLog::debug("sibling flavor asset id [" . $siblingFlavorAsset->getId() . "] is required"); $requiredFlavorParamsIds[$siblingFlavorAsset->getFlavorParamsId()] = true; } } KalturaLog::debug("left required flavor params ids [" . print_r($requiredFlavorParamsIds, true) . "]"); KalturaLog::debug("left incomplete flavor ids [" . print_r($inCompleteFlavorIds, true) . "]"); if (count($requiredFlavorParamsIds)) { $inCompleteRequiredFlavorParamsIds = array_keys($requiredFlavorParamsIds); foreach ($inCompleteRequiredFlavorParamsIds as $inCompleteFlavorId) { $inCompleteFlavorIds[] = $inCompleteFlavorId; } KalturaLog::debug('Convert Finished - has In-Complete Required flavors [[' . print_r($inCompleteRequiredFlavorParamsIds, true) . ']'); } elseif ($currentFlavorAsset->getStatus() == asset::ASSET_STATUS_READY && ($currentReadyBehavior == flavorParamsConversionProfile::READY_BEHAVIOR_OPTIONAL || $currentReadyBehavior == flavorParamsConversionProfile::READY_BEHAVIOR_REQUIRED)) { // mark the entry as ready if all required conversions completed or any of the optionals if ($currentFlavorAsset->getentry()->getReplacedEntryId()) { KalturaLog::debug('Entry is temporary replacement and requires all flavors to complete'); } else { KalturaLog::debug('Mark the entry as ready'); kBatchManager::updateEntry($currentFlavorAsset->getEntryId(), entryStatus::READY); } } if ($origianlAssetFlavorId) { $inCompleteFlavorIds = array_diff($inCompleteFlavorIds, array($origianlAssetFlavorId)); } if (!count($inCompleteFlavorIds)) { KalturaLog::debug('Convert Finished'); if ($origianlAssetFlavorId && $rootBatchJob && $rootBatchJob->getJobType() == BatchJobType::CONVERT_PROFILE) { kStorageExporter::exportSourceAssetFromJob($rootBatchJob); } else { // mark the context root job as finished only if all conversion jobs are completed kBatchManager::updateEntry($currentFlavorAsset->getEntryId(), entryStatus::READY); if ($rootBatchJob && $rootBatchJob->getJobType() == BatchJobType::CONVERT_PROFILE) { kJobsManager::updateBatchJob($rootBatchJob, BatchJob::BATCHJOB_STATUS_FINISHED); } } return $dbBatchJob; } KalturaLog::debug('Convert Finished - has In-Complete flavors [' . print_r($inCompleteFlavorIds, true) . ']'); if (!$rootBatchJob || $rootBatchJob->getJobType() != BatchJobType::CONVERT_PROFILE) { return $dbBatchJob; } $childJobs = $rootBatchJob->getChildJobs(); KalturaLog::debug('Child jobs found [' . count($childJobs) . ']'); $waitingFlavorAssets = assetPeer::retrieveByEntryIdAndStatus($currentFlavorAsset->getEntryId(), flavorAsset::FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT); KalturaLog::debug('Waiting assets found [' . count($waitingFlavorAssets) . ']'); if (count($childJobs) > 1 && count($waitingFlavorAssets) < 1) { $allDone = true; foreach ($childJobs as $childJob) { if ($childJob->getId() != $rootBatchJob->getId() && $childJob->getStatus() != BatchJob::BATCHJOB_STATUS_FINISHED) { KalturaLog::debug('Child job id [' . $childJob->getId() . '] status [' . $childJob->getStatus() . ']'); $allDone = false; } } if ($allDone) { KalturaLog::debug('All child jobs done, closing profile'); kJobsManager::updateBatchJob($rootBatchJob, BatchJob::BATCHJOB_STATUS_FINISHED); } } return $dbBatchJob; }
private function fixIsmManifestForReplacedEntry($path) { $fileData = file_get_contents($path); $xml = new SimpleXMLElement($fileData); $ismcFileName = $xml->head->meta['content']; list($ismcObjectId, $version, $subType, $isAsset, $entryId) = $this->parseObjectId($ismcFileName); if ($entryId != $this->entry->getId()) { //replacement flow $flavorAssets = assetPeer::retrieveByEntryIdAndStatus($this->entry->getId(), asset::ASSET_STATUS_READY); foreach ($flavorAssets as $asset) { if ($asset->hasTag(assetParams::TAG_ISM_MANIFEST)) { list($replacingFileName, $fileName) = $this->getReplacedAndReplacingFileNames($asset, flavorAsset::FILE_SYNC_ASSET_SUB_TYPE_ISMC); if ($replacingFileName && $fileName) { $fileData = str_replace("content=\"{$replacingFileName}\"", "content=\"{$fileName}\"", $fileData); } } else { list($replacingFileName, $fileName) = $this->getReplacedAndReplacingFileNames($asset, flavorAsset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); if ($replacingFileName && $fileName) { $fileData = str_replace("src=\"{$replacingFileName}\"", "src=\"{$fileName}\"", $fileData); } } } return $fileData; } else { return $fileData; } }
/** * @param BatchJob $dbBatchJob * @param kImportJobData $data * @return BatchJob */ public static function handleImportFinished(BatchJob $dbBatchJob, kImportJobData $data) { if ($dbBatchJob->getExecutionStatus() == BatchJobExecutionStatus::ABORTED) { return $dbBatchJob; } if (!file_exists($data->getDestFileLocalPath())) { throw new APIException(APIErrors::INVALID_FILE_NAME, $data->getDestFileLocalPath()); } // get entry $entryId = $dbBatchJob->getEntryId(); $dbEntry = entryPeer::retrieveByPKNoFilter($entryId); // IMAGE media entries if ($dbEntry->getType() == entryType::MEDIA_CLIP && $dbEntry->getMediaType() == entry::ENTRY_MEDIA_TYPE_IMAGE) { //setting the entry's data so it can be used for creating file-syncs' file-path version & extension - in kFileSyncUtils::moveFromFile //without saving - the updated entry object exists in the instance pool $dbEntry->setData(".jpg"); $syncKey = $dbEntry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DATA); try { kFileSyncUtils::moveFromFile($data->getDestFileLocalPath(), $syncKey, true, false, $data->getCacheOnly()); } catch (Exception $e) { if ($dbEntry->getStatus() == entryStatus::NO_CONTENT) { $dbEntry->setStatus(entryStatus::ERROR_CONVERTING); $dbEntry->save(); } throw $e; } $dbEntry->setStatus(entryStatus::READY); $dbEntry->save(); return $dbBatchJob; } $flavorAsset = null; if ($data->getFlavorAssetId()) { $flavorAsset = assetPeer::retrieveById($data->getFlavorAssetId()); } $isNewFlavor = false; if (!$flavorAsset) { $msg = null; $flavorAsset = kFlowHelper::createOriginalFlavorAsset($dbBatchJob->getPartnerId(), $dbBatchJob->getEntryId(), $msg); if (!$flavorAsset) { KalturaLog::err("Flavor asset not created for entry [" . $dbBatchJob->getEntryId() . "]"); kBatchManager::updateEntry($dbBatchJob->getEntryId(), entryStatus::ERROR_CONVERTING); $dbBatchJob->setMessage($msg); $dbBatchJob->setDescription($dbBatchJob->getDescription() . "\n" . $msg); return $dbBatchJob; } $isNewFlavor = true; } $isNewContent = true; $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); if (kFileSyncUtils::fileSync_exists($syncKey)) { $isNewContent = false; } $ext = pathinfo($data->getDestFileLocalPath(), PATHINFO_EXTENSION); KalturaLog::info("Imported file extension: {$ext}"); if (!$flavorAsset->getVersion()) { $flavorAsset->incrementVersion(); } if ($ext) { $flavorAsset->setFileExt($ext); } if ($flavorAsset instanceof thumbAsset) { list($width, $height, $type, $attr) = getimagesize($data->getDestFileLocalPath()); $flavorAsset->setWidth($width); $flavorAsset->setHeight($height); $flavorAsset->setSize(filesize($data->getDestFileLocalPath())); } $flavorAsset->save(); $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($data->getDestFileLocalPath(), $syncKey, true, false, $data->getCacheOnly()); // set the path in the job data $localFilePath = kFileSyncUtils::getLocalFilePathForKey($syncKey); $data->setDestFileLocalPath($localFilePath); $data->setFlavorAssetId($flavorAsset->getId()); $dbBatchJob->setData($data); $dbBatchJob->save(); $convertProfileExist = self::activateConvertProfileJob($dbBatchJob->getEntryId(), $localFilePath); if (($isNewContent || $dbEntry->getStatus() == entryStatus::IMPORT) && !$convertProfileExist) { // check if status == import for importing file of type url (filesync exists, and we want to raise event for conversion profile to start) kEventsManager::raiseEvent(new kObjectAddedEvent($flavorAsset, $dbBatchJob)); } if (!$isNewFlavor) { $entryFlavors = assetPeer::retrieveByEntryIdAndStatus($flavorAsset->getEntryId(), flavorAsset::FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT); $originalFlavorAsset = assetPeer::retrieveOriginalByEntryId($flavorAsset->getEntryId()); foreach ($entryFlavors as $entryFlavor) { /* @var $entryFlavor flavorAsset */ if ($entryFlavor->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT && $entryFlavor->getFlavorParamsId()) { $flavor = assetParamsOutputPeer::retrieveByAsset($entryFlavor); kBusinessPreConvertDL::decideFlavorConvert($entryFlavor, $flavor, $originalFlavorAsset, null, null, $dbBatchJob); } } $entryThumbnails = assetPeer::retrieveThumbnailsByEntryId($flavorAsset->getEntryId()); foreach ($entryThumbnails as $entryThumbnail) { /* @var $entryThumbnail thumbAsset */ if ($entryThumbnail->getStatus() != asset::ASSET_STATUS_WAIT_FOR_CONVERT || !$entryThumbnail->getFlavorParamsId()) { continue; } $thumbParamsOutput = assetParamsOutputPeer::retrieveByAssetId($entryThumbnail->getId()); /* @var $thumbParamsOutput thumbParamsOutput */ if ($thumbParamsOutput->getSourceParamsId() != $flavorAsset->getFlavorParamsId()) { continue; } $srcSyncKey = $flavorAsset->getSyncKey(asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); $srcAssetType = $flavorAsset->getType(); kJobsManager::addCapturaThumbJob($entryThumbnail->getPartnerId(), $entryThumbnail->getEntryId(), $entryThumbnail->getId(), $srcSyncKey, $flavorAsset->getId(), $srcAssetType, $thumbParamsOutput); } } return $dbBatchJob; }