PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/package/app/app/plugins/inlet_armada/lib/KOperationEngineInletArmada.php

https://bitbucket.org/pandaos/kaltura
PHP | 243 lines | 175 code | 21 blank | 47 comment | 24 complexity | f686488486f8bf188f3f1ebf68305d65 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. /**
  3. *
  4. * @package Scheduler
  5. * @subpackage Conversion
  6. *
  7. */
  8. class KOperationEngineInletArmada extends KSingleOutputOperationEngine
  9. {
  10. protected $taskConfig = null;
  11. /*
  12. protected $url=null;
  13. protected $login=null;
  14. protected $passw=null;
  15. protected $prio=5;
  16. */
  17. public function __construct($cmd, $outFilePath)
  18. {
  19. parent::__construct($cmd,$outFilePath);
  20. // $this->prio=5;
  21. KalturaLog::info(": cmd($cmd), outFilePath($outFilePath)");
  22. }
  23. /*************************************
  24. *
  25. */
  26. protected function getCmdLine()
  27. {
  28. $exeCmd = parent::getCmdLine();
  29. KalturaLog::info(print_r($this,true));
  30. return $exeCmd;
  31. }
  32. /*************************************
  33. *
  34. */
  35. public function operate(kOperator $operator = null, $inFilePath, $configFilePath = null)
  36. {
  37. KalturaLog::debug("operator==>".print_r($operator,1));
  38. $encodingTemplateId;
  39. $encodingTemplateName;
  40. $cloneAndUpadate=false;
  41. $srcPrefixWindows;
  42. $srcPrefixLinux;
  43. $trgPrefixWindows;
  44. // ---------------------------------
  45. // Evaluate and set various Inlet Armada session params
  46. if($this->taskConfig->params->InletStorageRootWindows) $srcPrefixWindows = $this->taskConfig->params->InletStorageRootWindows;
  47. if($this->taskConfig->params->InletStorageRootLinux) $srcPrefixLinux = $this->taskConfig->params->InletStorageRootLinux;
  48. if($this->taskConfig->params->InletTmpStorageWindows) $trgPrefixWindows = $this->taskConfig->params->InletTmpStorageWindows;
  49. $url = $this->taskConfig->params->InletArmadaUrl;
  50. $login = $this->taskConfig->params->InletArmadaLogin;
  51. $passw = $this->taskConfig->params->InletArmadaPassword;
  52. if($this->taskConfig->params->InletArmadaPriority)
  53. $priority = $this->taskConfig->params->InletArmadaPriority;
  54. else
  55. $priority = 5;
  56. // ----------------------------------
  57. $inlet = new InletAPIWrap($url);
  58. KalturaLog::debug(print_r($inlet,1));
  59. $rvObj=new XmlRpcData;
  60. $rv=$inlet->userLogon($login, $passw, $rvObj);
  61. if(!$rv) {
  62. throw new KOperationEngineException("Inlet failure: login, rv(".(print_r($rvObj,true)).")");
  63. }
  64. KalturaLog::debug("userLogon - ".print_r($rvObj,1));
  65. $paramsMap = KDLUtils::parseParamStr2Map($operator->extra);
  66. foreach($paramsMap as $key=>$param){
  67. switch($key){
  68. case 'encodingTemplate':
  69. case 'encodingTemplateId':
  70. $encodingTemplateId=$param;
  71. break;
  72. case 'encodingTemplateName':
  73. $encodingTemplateId = $this->lookForJobTemplateId($inlet, $param);
  74. $encodingTemplateName=$param;
  75. break;
  76. case 'priority':
  77. $priority=$param;
  78. break;
  79. case 'cloneAndUpadate':
  80. $cloneAndUpadate=$param;
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. // Adjust linux file path to Inlet Armada Windows path
  87. if(isset($srcPrefixWindows) && isset($srcPrefixLinux)) {
  88. $srcPrefixLinux = $this->addLastSlashInFolderPath($srcPrefixLinux, "/");
  89. $srcPrefixWindows = $this->addLastSlashInFolderPath($srcPrefixWindows, "\\");
  90. $srcFileWindows = str_replace($srcPrefixLinux, $srcPrefixWindows, $inFilePath);
  91. }
  92. else
  93. $srcFileWindows = $inFilePath;
  94. if(isset($trgPrefixWindows)){
  95. $trgPrefixLinux = $this->addLastSlashInFolderPath($this->taskConfig->params->localTempPath, "/");
  96. $trgPrefixWindows = $this->addLastSlashInFolderPath($trgPrefixWindows, "\\");
  97. $outFileWindows = str_replace($trgPrefixLinux, $trgPrefixWindows, $this->outFilePath);
  98. }
  99. else
  100. $outFileWindows = $this->outFilePath;
  101. $rv=$inlet->jobAdd(
  102. $encodingTemplateId, // job template id
  103. $srcFileWindows, // String job_source_file,
  104. $outFileWindows, // String job_destination_file,
  105. $priority, // Int priority,
  106. $srcFileWindows, // String description,
  107. array(),"",
  108. $rvObj);
  109. if(!$rv) {
  110. throw new KOperationEngineException("Inlet failure: add job, rv(".print_r($rvObj,1).")");
  111. }
  112. KalturaLog::debug("jobAdd - encodingTemplate($encodingTemplateId), inFile($srcFileWindows), outFile($outFileWindows),rv-".print_r($rvObj,1));
  113. $jobId=$rvObj->job_id;
  114. $attemptCnt=0;
  115. while ($jobId) {
  116. sleep(60);
  117. $rv=$inlet->jobList(array($jobId),$rvObj);
  118. if(!$rv) {
  119. throw new KOperationEngineException("Inlet failure: job list, rv(".print_r($rvObj,1).")");
  120. }
  121. switch($rvObj->job_list[0]->job_state){
  122. case InletArmadaJobStatus::CompletedSuccess:
  123. $jobId=null;
  124. break;
  125. case InletArmadaJobStatus::CompletedUnknown:
  126. case InletArmadaJobStatus::CompletedFailure:
  127. throw new KOperationEngineException("Inlet failure: job, rv(".print_r($rvObj,1).")");
  128. break;
  129. }
  130. if($attemptCnt%10==0) {
  131. KalturaLog::debug("waiting for job completion - ".print_r($rvObj,1));
  132. }
  133. $attemptCnt++;
  134. }
  135. //KalturaLog::debug("XXX taskConfig=>".print_r($this->taskConfig,1));
  136. KalturaLog::debug("Job completed successfully - ".print_r($rvObj,1));
  137. if($trgPrefixWindows) {
  138. $trgPrefixLinux = $this->addLastSlashInFolderPath($this->taskConfig->params->sharedTempPath, "/");
  139. $outFileLinux = str_replace($trgPrefixWindows, $trgPrefixLinux, $rvObj->job_list[0]->job_output_file);
  140. //KalturaLog::debug("XXX str_replace($trgPrefixWindows, ".$trgPrefixLinux.", ".$rvObj->job_list[0]->job_output_file.")==>$outFileLinux");
  141. }
  142. else
  143. $outFileLinux = $rvObj->job_list[0]->job_output_file;
  144. if($outFileLinux!=$this->outFilePath) {
  145. KalturaLog::debug("copy($outFileLinux, ".$this->outFilePath.")");
  146. kFile::moveFile($outFileLinux, $this->outFilePath, true);
  147. //copy($outFileLinux, $this->outFilePath);
  148. }
  149. }
  150. /*************************************
  151. *
  152. */
  153. public function configure(KSchedularTaskConfig $taskConfig, KalturaConvartableJobData $data, KalturaClient $client)
  154. {
  155. parent::configure($taskConfig, $data, $client);
  156. $this->taskConfig = $taskConfig;
  157. $errStr=null;
  158. if(!$taskConfig->params->InletArmadaUrl)
  159. $errStr="InletArmadaUrl";
  160. if(!$taskConfig->params->InletArmadaLogin){
  161. if($errStr)
  162. $errStr.=",InletArmadaLogin";
  163. else
  164. $errStr="InletArmadaLogin";
  165. }
  166. if(!$taskConfig->params->InletArmadaPassword){
  167. if($errStr)
  168. $errStr.=",InletArmadaPassword";
  169. else
  170. $errStr="InletArmadaPassword";
  171. }
  172. if($errStr)
  173. throw new KOperationEngineException("Inlet failure: missing credentials - $errStr");//, url(".$taskConfig->params->InletArmadaUrl."), login(."$taskConfig->params->InletArmadaLogin."),passw(".$taskConfig->params->InletArmadaPassword.")");
  174. /*
  175. $this->url = $taskConfig->params->InletArmadaUrl;
  176. $this->login = $taskConfig->params->InletArmadaLogin;
  177. $this->passw = $taskConfig->params->InletArmadaPassword;
  178. if($taskConfig->params->InletArmadaPriority)
  179. $this->prio = $taskConfig->params->InletArmadaPriority;
  180. else
  181. $this->prio = 5;
  182. */
  183. KalturaLog::info("taskConfig-->".print_r($taskConfig,true)."\ndata->".print_r($data,true));
  184. }
  185. /*************************************
  186. *
  187. */
  188. private function addLastSlashInFolderPath($pathStr, $slashCh)
  189. {
  190. if($pathStr[strlen($pathStr)-1]!=$slashCh)
  191. return $pathStr.$slashCh;
  192. else
  193. return $pathStr;
  194. }
  195. /*************************************
  196. *
  197. */
  198. private function lookForJobTemplateId($inlet, $name)
  199. {
  200. $rvObj=new XmlRpcData;
  201. $rv=$inlet->templateGroupList($rvObj);
  202. if(!$rv) {
  203. throw new KOperationEngineException("Inlet failure: templateGroupList, rv(".print_r($rvObj,1).")");
  204. }
  205. $templateDescObj=$this->templateGroupListToJobTemplate($rvObj->template_group_list, $name);
  206. return $templateDescObj->template_id;
  207. }
  208. /*************************************
  209. *
  210. */
  211. private function templateGroupListToJobTemplate($groupList, $val, $fieldName="template_description")
  212. {
  213. foreach ($groupList as $grp) {
  214. foreach ($grp->templates as $tpl) {
  215. if($tpl->$fieldName==$val) {
  216. return $tpl;
  217. }
  218. }
  219. }
  220. return null;
  221. }
  222. }