PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/package/app/app/batch/batches/Cleanup/KAsyncDirectoryCleanup.class.php

https://bitbucket.org/pandaos/kaltura
PHP | 98 lines | 57 code | 14 blank | 27 comment | 4 complexity | 47b3f9ca15399c9a7cbc36ff725ef83a 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. * @package Scheduler
  4. * @subpackage Cleanup
  5. */
  6. require_once("bootstrap.php");
  7. /**
  8. * Will run periodically and cleanup directories from old files that have a specific pattern (older than x days)
  9. *
  10. * @package Scheduler
  11. * @subpackage Cleanup
  12. */
  13. class KAsyncDirectoryCleanup extends KBatchBase
  14. {
  15. /* (non-PHPdoc)
  16. * @see KBatchBase::getType()
  17. */
  18. public static function getType()
  19. {
  20. return KalturaBatchJobType::CLEANUP;
  21. }
  22. /* (non-PHPdoc)
  23. * @see KBatchBase::getJobType()
  24. */
  25. public function getJobType()
  26. {
  27. return KalturaBatchJobType::CLEANUP;
  28. }
  29. /* (non-PHPdoc)
  30. * @see KBatchBase::exec()
  31. */
  32. protected function exec(KalturaBatchJob $job)
  33. {
  34. return null;
  35. }
  36. // TODO remove run, updateExclusiveJob and freeExclusiveJob
  37. protected function init()
  38. {
  39. }
  40. /**
  41. * @param int $jobId
  42. * @param KalturaBatchJob $job
  43. */
  44. protected function updateExclusiveJob($jobId, KalturaBatchJob $job){}
  45. /**
  46. * @param KalturaBatchJob $job
  47. */
  48. protected function freeExclusiveJob(KalturaBatchJob $job){}
  49. public function run()
  50. {
  51. KalturaLog::info("Directory Cleanup is running");
  52. $path = $this->getAdditionalParams( "path" );
  53. $pattern = $this->getAdditionalParams( "pattern" );
  54. $simulateOnly = $this->getAdditionalParams( "simulateOnly" );
  55. $minutesOld = $this->getAdditionalParams( "minutesOld" );
  56. $secondsOld = $minutesOld * 60;
  57. $path_to_search = $path . $pattern ;
  58. KalturaLog::debug("Searching [$path_to_search]");
  59. $files = glob ( $path_to_search);
  60. KalturaLog::debug("Found [" . count ( $files ) . "] to scan");
  61. $now = time();
  62. KalturaLog::debug("The time now is: " . date('c', $now));
  63. KalturaLog::debug("Deleting files that are " . $secondsOld ." seconds old (modified before " . date('c', $now - $secondsOld) . ")");
  64. $deletedCount = 0;
  65. foreach ( $files as $file )
  66. {
  67. $filemtime = filemtime($file);
  68. if ($filemtime > $now - $secondsOld)
  69. continue;
  70. $deletedCount++;
  71. if ( $simulateOnly )
  72. {
  73. KalturaLog::debug( "Simulating: Deleting file [$file], it's last modification time was " . date('c', $filemtime));
  74. continue;
  75. }
  76. KalturaLog::debug("Deleting file [$file], it's last modification time was " . date('c', $filemtime));
  77. $res = @unlink ( $file );
  78. if ( ! $res )
  79. KalturaLog::debug("Error: problem while deleting [$file]");
  80. }
  81. KalturaLog::debug("Deleted $deletedCount files");
  82. }
  83. }