cron.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. public static $lockfile = "";
  26. public static $keeplock = false;
  27. }
  28. // We use this function to handle (unexpected) shutdowns
  29. function handleUnexpectedShutdown() {
  30. // Delete lockfile
  31. if( !my_temporary_cron_class::$keeplock && file_exists( my_temporary_cron_class::$lockfile )) {
  32. unlink( my_temporary_cron_class::$lockfile );
  33. }
  34. // Say goodbye if the app did not shutdown properly
  35. if( !my_temporary_cron_class::$sent ) {
  36. if( OC::$CLI ) {
  37. echo 'Unexpected error!'.PHP_EOL;
  38. }
  39. else{
  40. OC_JSON::error( array( 'data' => array( 'message' => 'Unexpected error!')));
  41. }
  42. }
  43. }
  44. $RUNTIME_NOSETUPFS = true;
  45. require_once 'lib/base.php';
  46. // Don't do anything if ownCloud has not been installed
  47. if( !OC_Config::getValue( 'installed', false )) {
  48. exit( 0 );
  49. }
  50. // Handle unexpected errors
  51. register_shutdown_function('handleUnexpectedShutdown');
  52. // Exit if background jobs are disabled!
  53. $appmode = OC_BackgroundJob::getExecutionType();
  54. if( $appmode == 'none' ) {
  55. my_temporary_cron_class::$sent = true;
  56. if( OC::$CLI ) {
  57. echo 'Background Jobs are disabled!'.PHP_EOL;
  58. }
  59. else{
  60. OC_JSON::error( array( 'data' => array( 'message' => 'Background jobs disabled!')));
  61. }
  62. exit( 1 );
  63. }
  64. if( OC::$CLI ) {
  65. // Create lock file first
  66. my_temporary_cron_class::$lockfile = OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ).'/cron.lock';
  67. // We call ownCloud from the CLI (aka cron)
  68. if( $appmode != 'cron' ) {
  69. // Use cron in feature!
  70. OC_BackgroundJob::setExecutionType('cron' );
  71. }
  72. // check if backgroundjobs is still running
  73. if( file_exists( my_temporary_cron_class::$lockfile )) {
  74. my_temporary_cron_class::$keeplock = true;
  75. my_temporary_cron_class::$sent = true;
  76. echo "Another instance of cron.php is still running!";
  77. exit( 1 );
  78. }
  79. // Create a lock file
  80. touch( my_temporary_cron_class::$lockfile );
  81. // Work
  82. OC_BackgroundJob_Worker::doAllSteps();
  83. }
  84. else{
  85. // We call cron.php from some website
  86. if( $appmode == 'cron' ) {
  87. // Cron is cron :-P
  88. OC_JSON::error( array( 'data' => array( 'message' => 'Backgroundjobs are using system cron!')));
  89. }
  90. else{
  91. // Work and success :-)
  92. OC_BackgroundJob_Worker::doNextStep();
  93. OC_JSON::success();
  94. }
  95. }
  96. // done!
  97. my_temporary_cron_class::$sent = true;
  98. exit();