cron.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2012 Jakob Sack owncloud@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // Unfortunately we need this class for shutdown function
  23. class my_temporary_cron_class {
  24. public static $sent = false;
  25. }
  26. function handleUnexpectedShutdown() {
  27. if( !my_temporary_cron_class::$sent ){
  28. if( OC::$CLI ){
  29. echo 'Unexpected error!'.PHP_EOL;
  30. }
  31. else{
  32. OC_JSON::error( array( 'data' => array( 'message' => 'Unexpected error!')));
  33. }
  34. }
  35. }
  36. $RUNTIME_NOSETUPFS = true;
  37. require_once('lib/base.php');
  38. // Don't do anything if ownCloud has not been installed
  39. if( !OC_Config::getValue( 'installed', false )){
  40. exit( 0 );
  41. }
  42. // Handle unexpected errors
  43. register_shutdown_function('handleUnexpectedShutdown');
  44. // Exit if background jobs are disabled!
  45. $appmode = OC_Appconfig::getValue( 'core', 'backgroundjobs_mode', 'ajax' );
  46. if( $appmode == 'none' ){
  47. my_temporary_cron_class::$sent = true;
  48. if( OC::$CLI ){
  49. echo 'Background Jobs are disabled!'.PHP_EOL;
  50. }
  51. else{
  52. OC_JSON::error( array( 'data' => array( 'message' => 'Background jobs disabled!')));
  53. }
  54. exit( 1 );
  55. }
  56. if( OC::$CLI ){
  57. if( $appmode != 'cron' ){
  58. OC_Appconfig::setValue( 'core', 'backgroundjobs_mode', 'cron' );
  59. }
  60. // check if backgroundjobs is still running
  61. $pid = OC_Appconfig::getValue( 'core', 'backgroundjobs_pid', false );
  62. if( $pid !== false ){
  63. // FIXME: check if $pid is still alive (*nix/mswin). if so then exit
  64. }
  65. // save pid
  66. OC_Appconfig::setValue( 'core', 'backgroundjobs_pid', getmypid());
  67. // Work
  68. OC_BackgroundJob_Worker::doAllSteps();
  69. }
  70. else{
  71. if( $appmode == 'cron' ){
  72. OC_JSON::error( array( 'data' => array( 'message' => 'Backgroundjobs are using system cron!')));
  73. }
  74. else{
  75. OC_BackgroundJob_Worker::doNextStep();
  76. OC_JSON::success();
  77. }
  78. }
  79. my_temporary_cron_class::$sent = true;
  80. exit();