PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/package/app/app/alpha/batch/batchWatch.php

https://bitbucket.org/pandaos/kaltura
PHP | 88 lines | 28 code | 4 blank | 56 comment | 1 complexity | f05dc64990d3207e9f82c9168c74ccb6 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. #!/usr/bin/php
  2. <?php
  3. /*
  4. * Created on Nov 25, 2006
  5. *
  6. * To change the template for this generated file go to
  7. * Window - Preferences - PHPeclipse - PHP - Code Templates
  8. */
  9. require_once(realpath(dirname(__FILE__)).'/../config/sfrootdir.php');
  10. define('SF_APP', 'kaltura');
  11. define('SF_ENVIRONMENT', 'batch');
  12. define('SF_DEBUG', true);
  13. $PHP_CMD = "php" ;
  14. require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
  15. $batchwatch_job_list = array ( "batchConvertClient" ,
  16. "batchConvertServer",
  17. "batchBulkUpload",
  18. "batchDownloadVideoServer",
  19. "batchEmailServer",
  20. "batchEntryDeleteServer",
  21. "batchMobileUploadServer",
  22. "batchFlattenServer",
  23. "batchImportServer",
  24. "batchNotificationServer",
  25. "newBatchCommercialConvertServer",
  26. "newBatchConvertClient",
  27. "newBatchConvertServer" , );
  28. $count = 0;
  29. $sleep_time = 1;
  30. $write_to_the_log_seconds = 120;
  31. while ( 1 )
  32. {
  33. try
  34. {
  35. $files = glob ( myBatchBase::getBatchwatchPath() . "/*");
  36. SET_CONTEXT ("batchWatch" );
  37. // the files in this directory are supposed to be batch names to be started/stopped or restarted
  38. foreach ( $files as $file )
  39. {
  40. $batch_name = pathinfo( $file , PATHINFO_FILENAME );
  41. if ( $batch_name == myBatchBase::REGISTERED_BATCHS ) continue; // skip if the special file holding the registered batchs
  42. if ( $batch_name[0] == "_" ) continue; //myBatchBase::IGNORE_PREFIX ) continue;
  43. if ( in_array ( $batch_name , $batchwatch_job_list ))
  44. {
  45. $command = file_get_contents( $file );
  46. // TODO - change to fit the way we start / stop / restart
  47. // $cmd_line = "service $batch_name $command";
  48. $cmd_line = "$PHP_CMD runBatch.php $command $batch_name ";
  49. TRACE ( "$cmd_line");
  50. $output = array();
  51. exec ( $cmd_line , $output , $return_var );
  52. TRACE ( "Result: [$return_var]\n" . print_r ( $output , true ));
  53. if ( $command == "stop")
  54. {
  55. $path = batchStatus::batchEnd( $batch_name );
  56. }
  57. }
  58. else
  59. {
  60. TRACE ( "VERY BAD!!: tying to manipulate invalid batch [$batch_name]. Ignoring..." );
  61. }
  62. // remove the file
  63. unlink ( $file );
  64. }
  65. if ( $count > $write_to_the_log_seconds )
  66. {
  67. $count = 0;
  68. }
  69. if ( $count == 0 )
  70. TRACE ( "Sleeping for [$sleep_time] second. Will write to the log in ($write_to_the_log_seconds]" );
  71. sleep ( $sleep_time );
  72. $count ++;
  73. }
  74. catch ( Exception $ex )
  75. {
  76. TRACE ( "Error:" . $ex->getTrace() );
  77. }
  78. }
  79. ?>