/package/app/app/batch/batches/Storage/KAsyncStorageDelete.class.php

https://github.com/richhl/kalturaCE · PHP · 149 lines · 101 code · 25 blank · 23 comment · 11 complexity · 5bfc222e46b50c44fe7e20143e5707d0 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Scheduler
  4. * @subpackage Storage
  5. */
  6. require_once("bootstrap.php");
  7. /**
  8. * Will delete a single file to ftp or scp server
  9. *
  10. * @package Scheduler
  11. * @subpackage Storage
  12. */
  13. class KAsyncStorageDelete extends KBatchBase
  14. {
  15. /**
  16. * @return number
  17. */
  18. public static function getType()
  19. {
  20. return KalturaBatchJobType::STORAGE_DELETE;
  21. }
  22. protected function init()
  23. {
  24. $this->saveQueueFilter(self::getType());
  25. }
  26. protected function getFilter()
  27. {
  28. $filter = parent::getFilter();
  29. $filter->jobSubTypeIn = $this->getSupportedProtocols();
  30. if($this->taskConfig->params->minFileSize && is_numeric($this->taskConfig->params->minFileSize))
  31. $filter->fileSizeGreaterThan = $this->taskConfig->params->minFileSize;
  32. if($this->taskConfig->params->maxFileSize && is_numeric($this->taskConfig->params->maxFileSize))
  33. $filter->fileSizeLessThan = $this->taskConfig->params->maxFileSize;
  34. return $filter;
  35. }
  36. public function run($jobs = null)
  37. {
  38. KalturaLog::info("Net-Storage Delete batch is running");
  39. if($this->taskConfig->isInitOnly())
  40. return $this->init();
  41. if(is_null($jobs))
  42. $jobs = $this->kClient->batch->getExclusiveStorageDeleteJobs($this->getExclusiveLockKey(), $this->taskConfig->maximumExecutionTime, 1, $this->getFilter());
  43. KalturaLog::info(count($jobs) . " delete jobs to perform");
  44. if(! count($jobs))
  45. {
  46. KalturaLog::info("Queue size: 0 sent to scheduler");
  47. $this->saveSchedulerQueue(self::getType());
  48. return null;
  49. }
  50. foreach($jobs as &$job)
  51. {
  52. try
  53. {
  54. $job = $this->delete($job, $job->data);
  55. }
  56. catch(KalturaException $kex)
  57. {
  58. return $this->closeJob($job, KalturaBatchJobErrorTypes::KALTURA_API, $kex->getCode(), "Error: " . $kex->getMessage(), KalturaBatchJobStatus::FAILED);
  59. }
  60. catch(KalturaClientException $kcex)
  61. {
  62. return $this->closeJob($job, KalturaBatchJobErrorTypes::KALTURA_CLIENT, $kcex->getCode(), "Error: " . $kcex->getMessage(), KalturaBatchJobStatus::RETRY);
  63. }
  64. catch(Exception $ex)
  65. {
  66. return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED);
  67. }
  68. }
  69. return $jobs;
  70. }
  71. /**
  72. * Will take a single KalturaBatchJob and delete the given file
  73. *
  74. * @param KalturaBatchJob $job
  75. * @param KalturaStorageDeleteJobData $data
  76. * @return KalturaBatchJob
  77. */
  78. private function delete(KalturaBatchJob $job, KalturaStorageDeleteJobData $data)
  79. {
  80. KalturaLog::debug("delete($job->id)");
  81. $srcFile = str_replace('//', '/', trim($data->srcFileSyncLocalPath));
  82. $destFile = str_replace('//', '/', trim($data->destFileSyncStoredPath));
  83. $this->updateJob($job, "Deleteing $srcFile to $destFile", KalturaBatchJobStatus::QUEUED, 1);
  84. $engine = kFileTransferMgr::getInstance($job->jobSubType);
  85. try{
  86. $engine->login($data->serverUrl, $data->serverUsername, $data->serverPassword, null, $data->ftpPassiveMode);
  87. $engine->delFile($srcFile);
  88. }
  89. catch(kFileTransferMgrException $ke)
  90. {
  91. return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, $ke->getCode(), $ke->getMessage(), KalturaBatchJobStatus::FAILED);
  92. }
  93. catch(Exception $e)
  94. {
  95. return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $e->getCode(), $e->getMessage(), KalturaBatchJobStatus::FAILED);
  96. }
  97. return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED);
  98. }
  99. protected function updateExclusiveJob($jobId, KalturaBatchJob $job)
  100. {
  101. return $this->kClient->batch->updateExclusiveStorageDeleteJob($jobId, $this->getExclusiveLockKey(), $job);
  102. }
  103. protected function freeExclusiveJob(KalturaBatchJob $job)
  104. {
  105. $resetExecutionAttempts = false;
  106. if($job->status == KalturaBatchJobStatus::ALMOST_DONE)
  107. $resetExecutionAttempts = true;
  108. $response = $this->kClient->batch->freeExclusiveStorageDeleteJob($job->id, $this->getExclusiveLockKey(), $resetExecutionAttempts);
  109. KalturaLog::info("Queue size: $response->queueSize sent to scheduler");
  110. $this->saveSchedulerQueue(self::getType(), $response->queueSize);
  111. return $response->job;
  112. }
  113. /*
  114. * @return string
  115. */
  116. protected function getSupportedProtocols()
  117. {
  118. $supported_engines_arr = array();
  119. if ( $this->taskConfig->params->useFTP ) $supported_engines_arr[] = KalturaDeleteProtocol::FTP;
  120. if ( $this->taskConfig->params->useSCP ) $supported_engines_arr[] = KalturaDeleteProtocol::SCP;
  121. return join(',', $supported_engines_arr);
  122. }
  123. }
  124. ?>